Skip to content

Commit

Permalink
bug #38446 [PropertyInfo] Extract from default value doesn't set coll…
Browse files Browse the repository at this point in the history
…ection boolean (Korbeil)

This PR was merged into the 5.1 branch.

Discussion
----------

[PropertyInfo] Extract from default value doesn't set collection boolean

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #38445
| License       | MIT
| Doc PR        | N/A

This PR will fix the issue by checking if the type is an array or not and setting the collection boolean based on this check.

Commits
-------

33d37e5 Fix no collection in extract default value
  • Loading branch information
fabpot committed Oct 7, 2020
2 parents 2b206ff + 33d37e5 commit 392f2e0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
Expand Up @@ -477,8 +477,9 @@ private function extractFromDefaultValue(string $class, string $property): ?arra
}

$type = \gettype($defaultValue);
$type = static::MAP_TYPES[$type] ?? $type;

return [new Type(static::MAP_TYPES[$type] ?? $type)];
return [new Type($type, false, null, Type::BUILTIN_TYPE_ARRAY === $type)];
}

private function extractFromReflectionType(\ReflectionType $reflectionType, \ReflectionClass $declaringClass): array
Expand Down
Expand Up @@ -257,7 +257,7 @@ public function defaultValueProvider()
['defaultInt', [new Type(Type::BUILTIN_TYPE_INT, false)]],
['defaultFloat', [new Type(Type::BUILTIN_TYPE_FLOAT, false)]],
['defaultString', [new Type(Type::BUILTIN_TYPE_STRING, false)]],
['defaultArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false)]],
['defaultArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
['defaultNull', null],
];
}
Expand Down Expand Up @@ -415,6 +415,7 @@ 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'));
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)], $this->extractor->getTypes(Php74Dummy::class, 'collection'));
}

/**
Expand Down
Expand Up @@ -20,6 +20,7 @@ class Php74Dummy
private ?bool $nullableBoolProp;
/** @var string[] */
private array $stringCollection;
public array $collection = [];

public function addStringCollection(string $string): void
{
Expand Down

0 comments on commit 392f2e0

Please sign in to comment.