Skip to content

Commit 4187201

Browse files
committed
[TypeInfo] Reuse CollectionType::mergeCollectionValueTypes for ConstFetchNode
1 parent 1477f6d commit 4187201

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy