Skip to content

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
@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.

3 participants