Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid overriding built-in enum methods #9280

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@ private function getFunctionInformation(
MethodAnalyzer::checkMethodSignatureMustOmitReturnType($storage, $codeLocation);

if ($appearing_class_storage->is_enum) {
MethodAnalyzer::checkForbiddenEnumMethod($storage);
MethodAnalyzer::checkForbiddenEnumMethod($storage, $appearing_class_storage);
}

if (!$context->calling_method_id || !$context->collect_initializations) {
Expand Down
19 changes: 18 additions & 1 deletion src/Psalm/Internal/Analyzer/MethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psalm\Issue\UndefinedMethod;
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Storage\MethodStorage;
use UnexpectedValueException;

Expand Down Expand Up @@ -310,7 +311,7 @@ public function getMethodId(?string $context_self = null): MethodIdentifier
);
}

public static function checkForbiddenEnumMethod(MethodStorage $method_storage): void
public static function checkForbiddenEnumMethod(MethodStorage $method_storage, ClassLikeStorage $enum_storage): void
{
if ($method_storage->cased_name === null || $method_storage->location === null) {
return;
Expand All @@ -324,5 +325,21 @@ public static function checkForbiddenEnumMethod(MethodStorage $method_storage):
$method_storage->defining_fqcln . '::' . $method_storage->cased_name,
));
}

if ($method_name_lc === 'cases') {
IssueBuffer::maybeAdd(new InvalidEnumMethod(
'Enums cannot define ' . $method_storage->cased_name,
$method_storage->location,
$method_storage->defining_fqcln . '::' . $method_storage->cased_name,
));
}

if ($enum_storage->enum_type && ($method_name_lc === 'from' || $method_name_lc === 'tryfrom')) {
IssueBuffer::maybeAdd(new InvalidEnumMethod(
'Enums cannot define ' . $method_storage->cased_name,
$method_storage->location,
$method_storage->defining_fqcln . '::' . $method_storage->cased_name,
));
}
}
}
56 changes: 56 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,62 @@ public static function tryFrom(int|string $value): ?static
'ignored_issues' => [],
'php_version' => '8.1',
],
'forbiddenUnitEnumCasesMethod' => [
'code' => '<?php
enum Foo {
case A;
public static function cases(): array
{
return [];
}
}
',
'error_message' => 'InvalidEnumMethod',
'ignored_issues' => [],
'php_version' => '8.1',
],
'forbiddenBackedEnumCasesMethod' => [
'code' => '<?php
enum Status: string {
case Open = "open";
public static function cases(): array
{
return [];
}
}
',
'error_message' => 'InvalidEnumMethod',
'ignored_issues' => [],
'php_version' => '8.1',
],
'forbiddenBackedEnumFromMethod' => [
'code' => '<?php
enum Status: string {
case Open = "open";
public static function from(string $value): self
{
throw new Exception;
}
}
',
'error_message' => 'InvalidEnumMethod',
'ignored_issues' => [],
'php_version' => '8.1',
],
'forbiddenBackedEnumTryFromMethod' => [
'code' => '<?php
enum Status: string {
case Open = "open";
public static function tryFrom(string $value): ?self
{
return null;
}
}
',
'error_message' => 'InvalidEnumMethod',
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
}