Skip to content
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

add phpstan assertions to Collection isEmpty and isNotEmpty #51998

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.

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

Successfully merging this pull request may close these issues.

None yet

3 participants