Skip to content

Commit 595fd2a

Browse files
committed
[TypeInfo] Fix resolving class const type
1 parent 4855cee commit 595fd2a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Tests/TypeResolver/StringTypeResolverTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public static function resolveDataProvider(): iterable
8686
yield [Type::array(Type::template('TValue', Type::mixed()), Type::template('TKey', Type::union(Type::int(), Type::string()))), 'array<TKey, TValue>', $typeContextFactory->createFromClassName(DummyCollection::class)];
8787
yield [Type::array(Type::template('TValue', Type::mixed()), Type::arrayKey()), 'array<array-key, TValue>', $typeContextFactory->createFromClassName(DummyCollection::class)];
8888
yield [Type::array(Type::bool(), Type::union(Type::template('TFoo', Type::int()), Type::template('TBar', Type::string()))), 'array<TFoo|TBar, bool>', $typeContextFactory->createFromClassName($dummyTemplateKeyUnion::class)];
89-
yield [Type::array(Type::bool(), Type::arrayKey()), 'array<'.DummyWithConstants::class.'::DUMMY_STRING_A|'.DummyWithConstants::class.'::DUMMY_INT_A, bool>', $typeContextFactory->createFromClassName(DummyWithConstants::class)];
89+
// explicitly test both with fully qualified class name and with imported short class name
90+
yield [Type::array(Type::bool(), Type::arrayKey()), 'array<\\'.DummyWithConstants::class.'::DUMMY_STRING_A|DummyWithConstants::DUMMY_INT_A, bool>', $typeContextFactory->createFromClassName(DummyWithConstants::class)];
9091

9192
// list
9293
yield [Type::list(Type::bool()), 'list<bool>'];
@@ -119,6 +120,7 @@ public static function resolveDataProvider(): iterable
119120
// const fetch
120121
yield [Type::string(), DummyWithConstants::class.'::DUMMY_STRING_*'];
121122
yield [Type::string(), DummyWithConstants::class.'::DUMMY_STRING_A'];
123+
yield [Type::string(), 'DummyWithConstants::DUMMY_STRING_A', $typeContextFactory->createFromClassName(DummyWithConstants::class)];
122124
yield [Type::int(), DummyWithConstants::class.'::DUMMY_INT_*'];
123125
yield [Type::int(), DummyWithConstants::class.'::DUMMY_INT_A'];
124126
yield [Type::float(), DummyWithConstants::class.'::DUMMY_FLOAT_*'];

TypeResolver/StringTypeResolver.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Symfony\Component\TypeInfo\Type\BuiltinType;
4444
use Symfony\Component\TypeInfo\Type\CollectionType;
4545
use Symfony\Component\TypeInfo\Type\GenericType;
46+
use Symfony\Component\TypeInfo\Type\ObjectType;
4647
use Symfony\Component\TypeInfo\TypeContext\TypeContext;
4748
use Symfony\Component\TypeInfo\TypeIdentifier;
4849

@@ -144,9 +145,18 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
144145
'self' => $typeContext->getDeclaringClass(),
145146
'static' => $typeContext->getCalledClass(),
146147
'parent' => $typeContext->getParentClass(),
147-
default => $node->constExpr->className,
148+
default => null,
148149
};
149150

151+
if (null === $className) {
152+
$classType = $this->resolveCustomIdentifier($node->constExpr->className, $typeContext);
153+
if (!$classType instanceof ObjectType) {
154+
return Type::mixed();
155+
}
156+
157+
$className = $classType->getClassName();
158+
}
159+
150160
if (!class_exists($className)) {
151161
return Type::mixed();
152162
}

0 commit comments

Comments
 (0)