Skip to content

Commit c2d6065

Browse files
committed
bug #61014 [TypeInfo] Reuse CollectionType::mergeCollectionValueTypes for ConstFetchNode (norkunas)
This PR was merged into the 7.3 branch. Discussion ---------- [TypeInfo] Reuse `CollectionType::mergeCollectionValueTypes` for `ConstFetchNode` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | #60820 (comment) | License | MIT Commits ------- 4187201 [TypeInfo] Reuse `CollectionType::mergeCollectionValueTypes` for `ConstFetchNode`
2 parents 42171a1 + 4187201 commit c2d6065

File tree

2 files changed

+6
-24
lines changed

2 files changed

+6
-24
lines changed

src/Symfony/Component/TypeInfo/Tests/TypeResolver/StringTypeResolverTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ public static function resolveDataProvider(): iterable
103103
yield [Type::int(), DummyWithConstants::class.'::DUMMY_INT_*'];
104104
yield [Type::int(), DummyWithConstants::class.'::DUMMY_INT_A'];
105105
yield [Type::float(), DummyWithConstants::class.'::DUMMY_FLOAT_*'];
106-
yield [Type::bool(), DummyWithConstants::class.'::DUMMY_TRUE_*'];
107-
yield [Type::bool(), DummyWithConstants::class.'::DUMMY_FALSE_*'];
106+
yield [Type::true(), DummyWithConstants::class.'::DUMMY_TRUE_*'];
107+
yield [Type::false(), DummyWithConstants::class.'::DUMMY_FALSE_*'];
108108
yield [Type::null(), DummyWithConstants::class.'::DUMMY_NULL_*'];
109-
yield [Type::array(), DummyWithConstants::class.'::DUMMY_ARRAY_*'];
109+
yield [Type::array(null, Type::union(Type::int(), Type::string())), DummyWithConstants::class.'::DUMMY_ARRAY_*'];
110110
yield [Type::enum(DummyEnum::class, Type::string()), DummyWithConstants::class.'::DUMMY_ENUM_*'];
111-
yield [Type::union(Type::string(), Type::int(), Type::float(), Type::bool(), Type::null(), Type::array(), Type::enum(DummyEnum::class, Type::string())), DummyWithConstants::class.'::DUMMY_MIX_*'];
111+
yield [Type::union(Type::enum(DummyEnum::class, Type::string()), Type::array(Type::mixed(), Type::union(Type::int(), Type::string())), Type::string(), Type::int(), Type::float(), Type::bool(), Type::null()), DummyWithConstants::class.'::DUMMY_MIX_*'];
112112

113113
// identifiers
114114
yield [Type::bool(), 'bool'];

src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,11 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ
149149

150150
foreach ((new \ReflectionClass($className))->getReflectionConstants() as $const) {
151151
if (preg_match('/^'.str_replace('\*', '.*', preg_quote($node->constExpr->name, '/')).'$/', $const->getName())) {
152-
$constValue = $const->getValue();
153-
154-
$types[] = match (true) {
155-
true === $constValue,
156-
false === $constValue => Type::bool(),
157-
null === $constValue => Type::null(),
158-
\is_string($constValue) => Type::string(),
159-
\is_int($constValue) => Type::int(),
160-
\is_float($constValue) => Type::float(),
161-
\is_array($constValue) => Type::array(),
162-
$constValue instanceof \UnitEnum => Type::enum($constValue::class),
163-
default => Type::mixed(),
164-
};
152+
$types[] = Type::fromValue($const->getValue());
165153
}
166154
}
167155

168-
$types = array_unique($types);
169-
170-
if (\count($types) > 2) {
171-
return Type::union(...$types);
172-
}
173-
174-
return $types[0] ?? Type::null();
156+
return CollectionType::mergeCollectionValueTypes($types);
175157
}
176158

177159
return match ($node->constExpr::class) {

0 commit comments

Comments
 (0)