Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add phpstan assertions to Collection isEmpty and isNotEmpty #51998

Merged
merged 2 commits into from
Jul 2, 2024
Merged

add phpstan assertions to Collection isEmpty and isNotEmpty #51998

merged 2 commits into from
Jul 2, 2024

Conversation

johanrosenson
Copy link
Contributor

This PR will add @phpstan-assert-if-true / @phpstan-assert-if-false to the Collection methods isEmpty and isNotEmpty.

This will allow phpstan to know if first() will return null or not (when not passing any filter arguments).

Example

/**
 * @var Collection<int, string>
 */
$collection = new Collection(['laravel']);

if ($collection->isNotEmpty()) {
    // before this PR:
    \PHPStan\dumpType($collection->first()); // Dumped type: string|null
    
    // after this PR:
    \PHPStan\dumpType($collection->first()); // Dumped type: string
}

@taylorotwell taylorotwell merged commit 821346d into laravel:11.x Jul 2, 2024
28 checks passed
@johanrosenson johanrosenson deleted the fix/phpstan-assert-Collection-isEmpty-isNotEmpty branch July 3, 2024 14:55
@spawnia
Copy link
Contributor

spawnia commented Jul 10, 2024

This is wrong if items in the Collection are null.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants