Skip to content

Conversation

@bastien-phi
Copy link
Contributor

#51998 and #52184 added PHPStan assertions for Collection::first() in Collection::isEmpty() and Collection::isNotEmpty()

This PR adds the same for Collection::last() as it would be very useful

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

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

@taylorotwell taylorotwell merged commit 34f5ef6 into laravel:11.x Oct 10, 2024
33 checks passed
@bastien-phi bastien-phi deleted the phpstan_collection_empty branch October 10, 2024 19:23
@bastien-phi
Copy link
Contributor Author

Thanks !

timacdonald pushed a commit to timacdonald/framework that referenced this pull request Oct 15, 2024
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.

2 participants