Skip to content

[TypeInfo] Reuse CollectionType::mergeCollectionValueTypes for ConstFetchNode #61014

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.3
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 @@ -103,12 +103,12 @@ public static function resolveDataProvider(): iterable
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::true(), DummyWithConstants::class.'::DUMMY_TRUE_*'];
yield [Type::false(), DummyWithConstants::class.'::DUMMY_FALSE_*'];
Comment on lines +106 to +107
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be backported to 7.2 IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, will do that afterwards :)

yield [Type::null(), DummyWithConstants::class.'::DUMMY_NULL_*'];
yield [Type::array(), DummyWithConstants::class.'::DUMMY_ARRAY_*'];
yield [Type::array(null, Type::union(Type::int(), Type::string())), DummyWithConstants::class.'::DUMMY_ARRAY_*'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
yield [Type::array(null, Type::union(Type::int(), Type::string())), DummyWithConstants::class.'::DUMMY_ARRAY_*'];
yield [Type::array(key: Type::union(Type::int(), Type::string())), DummyWithConstants::class.'::DUMMY_ARRAY_*'];

Is a bit more explicit to me 🙂

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 was done like that before, but something popped in my mind that symfony does not use named params 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

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

named parameters are not covered by our BC promise (except in attribute constructors). And we want our tests to use our API covered by BC guarantees, as it allows to catch mistakes where we break BC (if we have to update tests, it is a good indication that something changed that might not be intended).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, then you can keep null!

yield [Type::enum(DummyEnum::class, Type::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::string())), DummyWithConstants::class.'::DUMMY_MIX_*'];
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_*'];

// identifiers
yield [Type::bool(), 'bool'];
Expand Down
22 changes: 2 additions & 20 deletions src/Symfony/Component/TypeInfo/TypeResolver/StringTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,11 @@ private function getTypeFromNode(TypeNode $node, ?TypeContext $typeContext): Typ

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) {
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[] = Type::fromValue($const->getValue());
}
}

$types = array_unique($types);

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

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

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