Skip to content

[PropertyInfo] Fix typed collections in PHP 7.4 #38041

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

Merged
merged 1 commit into from
Sep 4, 2020
Merged
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 @@ -139,18 +139,6 @@ public function getProperties($class, array $context = []): ?array
*/
public function getTypes($class, $property, array $context = []): ?array
{
if (\PHP_VERSION_ID >= 70400) {
try {
$reflectionProperty = new \ReflectionProperty($class, $property);
$type = $reflectionProperty->getType();
if (null !== $type) {
return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass());
}
} catch (\ReflectionException $e) {
// noop
}
}

if ($fromMutator = $this->extractFromMutator($class, $property)) {
return $fromMutator;
}
Expand All @@ -170,6 +158,18 @@ public function getTypes($class, $property, array $context = []): ?array
return $fromDefaultValue;
}

if (\PHP_VERSION_ID >= 70400) {
Copy link
Contributor

@ogizanagi ogizanagi Sep 3, 2020

Choose a reason for hiding this comment

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

I think it'll make more sense to have precedence over constructor and default value extraction (the two if above), right?

Copy link
Contributor Author

@ndench ndench Sep 3, 2020

Choose a reason for hiding this comment

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

@ogizanagi yeah it might make more sense in the fact that if there is constructor args or default values as well as a php7.4 property type it might be more simple to just extract from the property type? I'm not sure it makes much of a difference though. Happy to move it if you like?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like moving this block has been changing the previous behaviour quite a bit.
#38416

Maybe you could have a look, if this could be reverted (while still fixing #38041)

try {
$reflectionProperty = new \ReflectionProperty($class, $property);
$type = $reflectionProperty->getType();
if (null !== $type) {
return $this->extractFromReflectionType($type, $reflectionProperty->getDeclaringClass());
}
} catch (\ReflectionException $e) {
// noop
}
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,5 +398,6 @@ public function testTypedProperties(): void
{
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)], $this->extractor->getTypes(Php74Dummy::class, 'dummy'));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_BOOL, true)], $this->extractor->getTypes(Php74Dummy::class, 'nullableBoolProp'));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))], $this->extractor->getTypes(Php74Dummy::class, 'stringCollection'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ class Php74Dummy
{
public Dummy $dummy;
private ?bool $nullableBoolProp;
/** @var string[] */
private array $stringCollection;

public function addStringCollection(string $string): void
{
}

public function removeStringCollection(string $string): void
{
}
}