Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Open
wants to merge 1 commit into
base: 7.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ public static function unionTypesProvider(): iterable
Type::object(ParentDummy::class),
Type::null(),
)];
yield ['f', null];
yield ['f', Type::union(Type::string(), Type::null())];
yield ['g', Type::array(Type::union(Type::string(), Type::int()))];
}

Expand Down
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
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyBackedEnum;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyCollection;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyEnum;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithConstants;
use Symfony\Component\TypeInfo\Tests\Fixtures\DummyWithTemplates;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeContext\TypeContext;
Expand Down Expand Up @@ -90,6 +91,19 @@ public static function resolveDataProvider(): iterable
yield [Type::string(), '"string"'];
yield [Type::true(), 'true'];

// const fetch
yield [Type::string(), DummyWithConstants::class.'::DUMMY_STRING_*'];
yield [Type::string(), DummyWithConstants::class.'::DUMMY_STRING_A'];
yield [Type::int(), DummyWithConstants::class.'::DUMMY_INT_*'];
yield [Type::int(), DummyWithConstants::class.'::DUMMY_INT_A'];
yield [Type::float(), DummyWithConstants::class.'::DUMMY_FLOAT_*'];
yield [Type::bool(), DummyWithConstants::class.'::DUMMY_TRUE_*'];
yield [Type::bool(), DummyWithConstants::class.'::DUMMY_FALSE_*'];
yield [Type::null(), DummyWithConstants::class.'::DUMMY_NULL_*'];
yield [Type::array(), DummyWithConstants::class.'::DUMMY_ARRAY_*'];
yield [Type::enum(DummyEnum::class, Type\BuiltinType::string()), DummyWithConstants::class.'::DUMMY_ENUM_*'];
yield [Type::union(Type::string(), Type::int(), Type::float(), Type::bool(), Type::null(), Type::array(), Type::enum(DummyEnum::class, Type\BuiltinType::string())), DummyWithConstants::class.'::DUMMY_MIX_*'];

// identifiers
yield [Type::bool(), 'bool'];
yield [Type::bool(), 'boolean'];
Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Copy link
Contributor

@mtarld mtarld Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can use Type::fromValue() instead? And then merge types with CollectionType::mergeCollectionValueTypes() helper method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted, but it's not available in 7.2

Copy link
Contributor

Choose a reason for hiding this comment

The 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),
default => Type::mixed(),
};
}
}

$types = array_unique($types);

if (\count($types) > 2) {
return Type::union(...$types);
}

return $types[0] ?? Type::null();
}

return match ($node->constExpr::class) {
ConstExprArrayNode::class => Type::array(),
ConstExprFalseNode::class => Type::false(),
Expand Down
Loading