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

Fix #9824 const enum self reference #9833

Merged
merged 1 commit into from May 28, 2023
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
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