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

Fix return types of firstWhere and first of BelongsToMany and HasManyThrough #51219

Merged
merged 2 commits into from
Apr 26, 2024
Merged

Fix return types of firstWhere and first of BelongsToMany and HasManyThrough #51219

merged 2 commits into from
Apr 26, 2024

Conversation

SanderMuller
Copy link
Contributor

The current return types of firstWhere does not include null, while it ends up calling ->first() which can return null (when there's no results).

This change improves the IDE intellisense and fixes false positives static analyser errors, for example this Larastan error:

Using nullsafe property access on non-nullable type                                                                               
         Illuminate\Database\Eloquent\Model|Illuminate\Database\Eloquent\Relations\BelongsToMany<Illuminate\Database\Eloquent\Model>.  
         Use -> instead.   

@SanderMuller SanderMuller changed the title Fix return types of firstWhere and first of BelongsToMany and HasManyThrough @SanderMuller Fix return types of firstWhere and first of BelongsToMany and HasManyThrough Apr 26, 2024
@SanderMuller
Copy link
Contributor Author

We might want to target this to 10.x instead

@@ -784,7 +784,7 @@ public function findOr($id, $columns = ['*'], ?Closure $callback = null)
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return \Illuminate\Database\Eloquent\Model|static
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also not sure why this returns |static, in which case does it return an instance of BelongsToMany?

Copy link
Contributor Author

@SanderMuller SanderMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop static as return types

@@ -784,7 +784,7 @@ public function findOr($id, $columns = ['*'], ?Closure $callback = null)
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return \Illuminate\Database\Eloquent\Model|static
* @return \Illuminate\Database\Eloquent\Model|static|null
Copy link
Contributor Author

@SanderMuller SanderMuller Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return \Illuminate\Database\Eloquent\Model|static|null
* @return \Illuminate\Database\Eloquent\Model|null

Unless I'm mistaken, this should be correct

@@ -795,7 +795,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
* Execute the query and get the first result.
*
* @param array $columns
* @return mixed
* @return \Illuminate\Database\Eloquent\Model|static|null
Copy link
Contributor Author

@SanderMuller SanderMuller Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return \Illuminate\Database\Eloquent\Model|static|null
* @return \Illuminate\Database\Eloquent\Model|null

@@ -320,7 +320,7 @@ public function updateOrCreate(array $attributes, array $values = [])
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return \Illuminate\Database\Eloquent\Model|static
* @return \Illuminate\Database\Eloquent\Model|static|null
Copy link
Contributor Author

@SanderMuller SanderMuller Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return \Illuminate\Database\Eloquent\Model|static|null
* @return \Illuminate\Database\Eloquent\Model|null

@@ -331,7 +331,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
* Execute the query and get the first related model.
*
* @param array $columns
* @return mixed
* @return \Illuminate\Database\Eloquent\Model|static|null
Copy link
Contributor Author

@SanderMuller SanderMuller Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return \Illuminate\Database\Eloquent\Model|static|null
* @return \Illuminate\Database\Eloquent\Model|null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the ? in docblocks itself. null is what we use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the ? in docblocks itself. null is what we use.

Updated!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints can you confirm in static can be dropped? Then I can create a follow-up PR for it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just leave things be for now. Thanks

@taylorotwell taylorotwell merged commit f993b9c into laravel:11.x Apr 26, 2024
30 checks passed
# 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