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

Include enum cases in const wildcards #7010

Merged
merged 1 commit into from Nov 28, 2021
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
11 changes: 4 additions & 7 deletions src/Psalm/Internal/Type/TypeExpander.php
Expand Up @@ -215,9 +215,10 @@ public static function expandAtomic(
$class_storage = $codebase->classlike_storage_provider->get($return_type->fq_classlike_name);

if (strpos($return_type->const_name, '*') !== false) {
$class_storage = $codebase->classlike_storage_provider->get($return_type->fq_classlike_name);

$matching_constants = \array_keys($class_storage->constants);
$matching_constants = \array_merge(
\array_keys($class_storage->constants),
\array_keys($class_storage->enum_cases)
);

$const_name_part = \substr($return_type->const_name, 0, -1);

Expand All @@ -231,10 +232,6 @@ function ($constant_name) use ($const_name_part): bool {
);
}
} else {
if ($class_storage->is_enum) {
return new Type\Atomic\TEnumCase($return_type->fq_classlike_name, $return_type->const_name);
}
Comment on lines -234 to -236
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the more special handling for enums I drop the better it works 😄


$matching_constants = [$return_type->const_name];
}

Expand Down
17 changes: 16 additions & 1 deletion tests/EnumTest.php
Expand Up @@ -154,7 +154,7 @@ public function get(): string
[],
'8.1'
],
'SKIPPED-wildcardEnum' => [
'wildcardEnumAsParam' => [
'<?php
enum A {
case C_1;
Expand All @@ -174,6 +174,21 @@ public static function foo(self $i) : void {}
[],
'8.1',
],
'wildcardEnumAsReturn' => [
'<?php
enum E {
const A = 1;
case B;
}

/** @return E::* */
function f(): mixed {
return E::B;
}',
'assertions' => [],
[],
'8.1',
],
'wildcardConstantsOnEnum' => [
'<?php
enum A {
Expand Down