Skip to content

Commit

Permalink
Merge pull request #9833 from ygottschalk/fix/9824-enum-const-str-concat
Browse files Browse the repository at this point in the history
Fix #9824 const enum self reference
  • Loading branch information
orklah committed May 28, 2023
2 parents d788bcd + a41eb35 commit 106b986
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/Psalm/Internal/PhpVisitor/Reflector/ExpressionResolver.php
Expand Up @@ -305,15 +305,28 @@ public static function getUnresolvedClassConstExpr(
&& $stmt->var->class instanceof PhpParser\Node\Name
&& $stmt->var->name instanceof PhpParser\Node\Identifier
&& $stmt->name instanceof PhpParser\Node\Identifier
&& in_array($stmt->name->name, ['name', 'value', true])
&& in_array($stmt->name->name, ['name', 'value'], true)
&& ($stmt->var->class->parts !== ['self'] || $fq_classlike_name !== null)
&& $stmt->var->class->parts !== ['static']
&& ($stmt->var->class->parts !== ['parent'] || $parent_fq_class_name !== null)
) {
$enum_fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
$stmt->var->class,
$aliases,
);
if ($stmt->var->class->parts === ['self']) {
assert($fq_classlike_name !== null);
$enum_fq_class_name = $fq_classlike_name;
} else {
if ($stmt->var->class->parts === ['parent']) {
assert($parent_fq_class_name !== null);
$enum_fq_class_name = $parent_fq_class_name;
} else {
$enum_fq_class_name = ClassLikeAnalyzer::getFQCLNFromNameObject(
$stmt->var->class,
$aliases,
);
}
}
if ($stmt->name->name === 'value') {
return new EnumValueFetch($enum_fq_class_name, $stmt->var->name->name);
} elseif ($stmt->name->name === 'name') {
} else /*if ($stmt->name->name === 'name')*/ {
return new EnumNameFetch($enum_fq_class_name, $stmt->var->name->name);
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ConstantTest.php
Expand Up @@ -1655,6 +1655,22 @@ enum BES: string {
'ignored_issues' => [],
'php_version' => '8.2',
],
'constantEnumSelfReference' => [
'code' => '<?php<?php
enum Bar: string {
case A = "a";
case B = "b";
public const STR = self::A->value . self::B->value;
}
class Foo {
public const CONCAT_STR = "a" . Bar::STR . "e";
}
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.2',
],
'classConstWithParamOut' => [
'code' => '<?php
Expand Down

0 comments on commit 106b986

Please sign in to comment.