-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TypeInfo] Fix handling ConstFetchNode
#60820
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
base: 7.2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\TypeInfo\Tests\Fixtures; | ||
|
||
final class DummyWithConstants | ||
{ | ||
public const DUMMY_STRING_A = 'a'; | ||
public const DUMMY_INT_A = 1; | ||
public const DUMMY_FLOAT_A = 1.23; | ||
public const DUMMY_TRUE_A = true; | ||
public const DUMMY_FALSE_A = false; | ||
public const DUMMY_NULL_A = null; | ||
public const DUMMY_ARRAY_A = []; | ||
public const DUMMY_ENUM_A = DummyEnum::ONE; | ||
|
||
public const DUMMY_MIX_1 = self::DUMMY_STRING_A; | ||
public const DUMMY_MIX_2 = self::DUMMY_INT_A; | ||
public const DUMMY_MIX_3 = self::DUMMY_FLOAT_A; | ||
public const DUMMY_MIX_4 = self::DUMMY_TRUE_A; | ||
public const DUMMY_MIX_5 = self::DUMMY_FALSE_A; | ||
public const DUMMY_MIX_6 = self::DUMMY_NULL_A; | ||
public const DUMMY_MIX_7 = self::DUMMY_ARRAY_A; | ||
public const DUMMY_MIX_8 = self::DUMMY_ENUM_A; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNullNode; | ||
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode; | ||
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode; | ||
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode; | ||
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; | ||
use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode; | ||
|
@@ -119,6 +120,47 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ | |
} | ||
|
||
if ($node instanceof ConstTypeNode) { | ||
if ($node->constExpr instanceof ConstFetchNode) { | ||
$className = match (strtolower($node->constExpr->className)) { | ||
'self' => $typeContext->getDeclaringClass(), | ||
'static' => $typeContext->getCalledClass(), | ||
'parent' => $typeContext->getParentClass(), | ||
default => $node->constExpr->className, | ||
}; | ||
|
||
if (!class_exists($className)) { | ||
return Type::mixed(); | ||
} | ||
|
||
$types = []; | ||
|
||
foreach ((new \ReflectionClass($className))->getReflectionConstants() as $const) { | ||
if (preg_match('/^'.str_replace('\*', '.*', preg_quote($node->constExpr->name, '/')).'$/', $const->getName())) { | ||
$constValue = $const->getValue(); | ||
|
||
$types[] = match (true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted, but it's not available in 7.2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh indeed, then it might be great to use that starting from 7.3 I guess. |
||
true === $constValue, | ||
false === $constValue => Type::bool(), | ||
null === $constValue => Type::null(), | ||
\is_string($constValue) => Type::string(), | ||
\is_int($constValue) => Type::int(), | ||
\is_float($constValue) => Type::float(), | ||
\is_array($constValue) => Type::array(), | ||
$constValue instanceof \UnitEnum => Type::enum($constValue::class), | ||
norkunas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default => Type::mixed(), | ||
}; | ||
} | ||
} | ||
|
||
$types = array_unique($types); | ||
|
||
if (\count($types) > 2) { | ||
norkunas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Type::union(...$types); | ||
} | ||
|
||
return $types[0] ?? Type::null(); | ||
} | ||
|
||
return match ($node->constExpr::class) { | ||
ConstExprArrayNode::class => Type::array(), | ||
ConstExprFalseNode::class => Type::false(), | ||
|
Uh oh!
There was an error while loading. Please reload this page.