Skip to content

[11.x] Add phpstan assertions for last in Collection isEmpty and isNotEmpty#53107

Merged
taylorotwell merged 1 commit intolaravel:11.xfrom
bastien-phi:phpstan_collection_empty
Oct 10, 2024
Merged

[11.x] Add phpstan assertions for last in Collection isEmpty and isNotEmpty#53107
taylorotwell merged 1 commit intolaravel:11.xfrom
bastien-phi:phpstan_collection_empty

Conversation

@bastien-phi
Copy link
Copy Markdown
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
@bastien-phi bastien-phi deleted the phpstan_collection_empty branch October 10, 2024 19:23
@bastien-phi
Copy link
Copy Markdown
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