1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00

Allow enum cases to be global constants

This commit is contained in:
robchett 2023-11-02 11:59:42 +00:00
parent 147505c806
commit 97a7cf8452
2 changed files with 77 additions and 5 deletions

View File

@ -12,6 +12,7 @@ use Psalm\Exception\CircularReferenceException;
use Psalm\FileSource; use Psalm\FileSource;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer; use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\BinaryOp\ArithmeticOpAnalyzer; use Psalm\Internal\Analyzer\Statements\Expression\BinaryOp\ArithmeticOpAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\ConstFetchAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer; use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Provider\NodeDataProvider; use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Internal\Type\TypeCombiner; use Psalm\Internal\Type\TypeCombiner;
@ -261,23 +262,28 @@ final class SimpleTypeInferer
} }
if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) { if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
$name = strtolower($stmt->name->getFirst()); $name = $stmt->name->getFirst();
if ($name === 'false') { $name_lowercase = strtolower($name);
if ($name_lowercase === 'false') {
return Type::getFalse(); return Type::getFalse();
} }
if ($name === 'true') { if ($name_lowercase === 'true') {
return Type::getTrue(); return Type::getTrue();
} }
if ($name === 'null') { if ($name_lowercase === 'null') {
return Type::getNull(); return Type::getNull();
} }
if ($stmt->name->getFirst() === '__NAMESPACE__') { if ($name === '__NAMESPACE__') {
return Type::getString($aliases->namespace); return Type::getString($aliases->namespace);
} }
if ($type = ConstFetchAnalyzer::getGlobalConstType($codebase, $name, $name)) {
return $type;
}
return null; return null;
} }

View File

@ -657,6 +657,28 @@ class EnumTest extends TestCase
'ignored_issues' => [], 'ignored_issues' => [],
'php_version' => '8.1', 'php_version' => '8.1',
], ],
'stringBackedEnumCaseValueFromStringGlobalConstant' => [
'code' => '<?php
enum Bar: string
{
case Foo = \DATE_ATOM;
}
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1',
],
'intBackedEnumCaseValueFromIntGlobalConstant' => [
'code' => '<?php
enum Bar: int
{
case Foo = \UPLOAD_ERR_OK;
}
',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.1',
],
]; ];
} }
@ -1107,6 +1129,50 @@ class EnumTest extends TestCase
'ignored_issues' => [], 'ignored_issues' => [],
'php_version' => '8.1', 'php_version' => '8.1',
], ],
'invalidStringBackedEnumCaseValueFromStringGlobalConstant' => [
'code' => '<?php
enum Bar: string
{
case Foo = \PHP_VERSION_ID;
}
',
'error_message' => 'InvalidEnumCaseValue',
'ignored_issues' => [],
'php_version' => '8.1',
],
'invalidIntBackedEnumCaseValueFromIntGlobalConstant' => [
'code' => '<?php
enum Bar: int
{
case Foo = \PHP_BINARY;
}
',
'error_message' => 'InvalidEnumCaseValue',
'ignored_issues' => [],
'php_version' => '8.1',
],
'invalidStringBackedEnumCaseValueFromIntGlobalConstant' => [
'code' => '<?php
enum Bar: string
{
case Foo = \PHP_BINARY;
}
',
'error_message' => 'InvalidEnumCaseValue',
'ignored_issues' => [],
'php_version' => '8.1',
],
'invalidIntBackedEnumCaseValueFromStringGlobalConstant' => [
'code' => '<?php
enum Bar: int
{
case Foo = \PHP_VERSION_ID;
}
',
'error_message' => 'InvalidEnumCaseValue',
'ignored_issues' => [],
'php_version' => '8.1',
],
]; ];
} }
} }