1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #10634 from weirdan/10334-global-constants-as-case-values

This commit is contained in:
Bruce Weirdan 2024-02-01 19:10:21 -04:00 committed by GitHub
commit fc88ef2f67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 76 additions and 5 deletions

View File

@ -283,23 +283,28 @@ final class SimpleTypeInferer
}
if ($stmt instanceof PhpParser\Node\Expr\ConstFetch) {
$name = strtolower($stmt->name->getFirst());
if ($name === 'false') {
$name = $stmt->name->getFirst();
$name_lowercase = strtolower($name);
if ($name_lowercase === 'false') {
return Type::getFalse();
}
if ($name === 'true') {
if ($name_lowercase === 'true') {
return Type::getTrue();
}
if ($name === 'null') {
if ($name_lowercase === 'null') {
return Type::getNull();
}
if ($stmt->name->getFirst() === '__NAMESPACE__') {
if ($name === '__NAMESPACE__') {
return Type::getString($aliases->namespace);
}
if ($type = ConstFetchAnalyzer::getGlobalConstType($codebase, $name, $name)) {
return $type;
}
return null;
}

View File

@ -657,6 +657,28 @@ class EnumTest extends TestCase
'ignored_issues' => [],
'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' => [],
'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',
],
];
}
}