Skip to content

Commit

Permalink
[PropertyInfo] Fix nullable value returned from extractFromMutator on…
Browse files Browse the repository at this point in the history
… CollectionType

Signed-off-by: Benjamin Lebon <[email protected]>
  • Loading branch information
benjilebon committed Jul 22, 2024
1 parent e7202a5 commit 825aa93
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Extractor/ReflectionExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ private function extractFromMutator(string $class, string $property): ?array
$type = $this->extractFromReflectionType($reflectionType, $reflectionMethod->getDeclaringClass());

if (1 === \count($type) && \in_array($prefix, $this->arrayMutatorPrefixes)) {
$type = [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), $type[0])];
$type = [new Type(Type::BUILTIN_TYPE_ARRAY, $this->isNullableProperty($class, $property), null, true, new Type(Type::BUILTIN_TYPE_INT), $type[0])];
}

return $type;
Expand Down
1 change: 1 addition & 0 deletions Tests/Extractor/ReflectionExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ public function testTypedProperties()
$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'));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_INT, true)], $this->extractor->getTypes(Php74Dummy::class, 'nullableWithDefault'));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)], $this->extractor->getTypes(Php74Dummy::class, 'collection'));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class))], $this->extractor->getTypes(Php74Dummy::class, 'nullableTypedCollection'));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions Tests/Fixtures/Php74Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ class Php74Dummy
private ?int $nullableWithDefault = 1;
public array $collection = [];

/** @var Dummy[]|null */
public ?array $nullableTypedCollection = null;

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

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

public function addNullableTypedCollection(Dummy $dummy): void
{
}
}

0 comments on commit 825aa93

Please sign in to comment.