Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyInfo] Fixed type extraction for nullable collections of non-nullable elements #34165

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

Conversation

happyproff
Copy link
Contributor

@happyproff happyproff commented Oct 29, 2019

Q A
Branch? 4.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

When an annotation is declared as int[]|null, it is handled like (int|null)[]|null. So array values are also nullable.

Now this behavior is fixed that int[]|null is either a collection of integers only or null.

How to reproduce:

class Dummy
{
    /** @var int[]|null */
    public $nullableCollectionOfNonNullableElements;
}

/** @var Type[] $types */
$types = (new PhpDocExtractor())->getTypes(Dummy::class, 'nullableCollectionOfNonNullableElements');

$collectionType = $types[0];
assert($collectionType->isCollection() === true); // OK
assert($collectionType->isNullable() === true); // OK
assert($collectionType->getCollectionValueType()->getBuiltinType() === Type::BUILTIN_TYPE_INT); // OK
assert($collectionType->getCollectionValueType()->isNullable() === false); // FAILED

@happyproff happyproff force-pushed the property-info-fix-nullable-collections-of-non-nullable-elements branch from 1447154 to dbbea0a Compare October 30, 2019 14:26
@fabpot fabpot force-pushed the property-info-fix-nullable-collections-of-non-nullable-elements branch from dbbea0a to 5e394c4 Compare November 3, 2019 12:19
@fabpot
Copy link
Member

fabpot commented Nov 3, 2019

Thank you @happyproff.

fabpot added a commit that referenced this pull request Nov 3, 2019
…ons of non-nullable elements (happyproff)

This PR was squashed before being merged into the 4.3 branch (closes #34165).

Discussion
----------

[PropertyInfo] Fixed type extraction for nullable collections of non-nullable elements

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

When an annotation is declared as `int[]|null`, it is handled like `(int|null)[]|null`. So array values are also nullable.

Now this behavior is fixed that `int[]|null` is either a collection of integers only or null.

How to reproduce:
```php
class Dummy
{
    /** @var int[]|null */
    public $nullableCollectionOfNonNullableElements;
}

/** @var Type[] $types */
$types = (new PhpDocExtractor())->getTypes(Dummy::class, 'nullableCollectionOfNonNullableElements');

$collectionType = $types[0];
assert($collectionType->isCollection() === true); // OK
assert($collectionType->isNullable() === true); // OK
assert($collectionType->getCollectionValueType()->getBuiltinType() === Type::BUILTIN_TYPE_INT); // OK
assert($collectionType->getCollectionValueType()->isNullable() === false); // FAILED
```

Commits
-------

5e394c4 [PropertyInfo] Fixed type extraction for nullable collections of non-nullable elements
@fabpot fabpot merged commit 5e394c4 into symfony:4.3 Nov 3, 2019
@fabpot fabpot mentioned this pull request Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants