Skip to content

Commit

Permalink
fix: prevent loading state if loaded previously
Browse files Browse the repository at this point in the history
Co-authored-by: SychO9 <[email protected]>
  • Loading branch information
rafaucau and SychO9 committed Jul 27, 2024
1 parent 4069731 commit 50dba27
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions extensions/tags/src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,18 @@ public function state()
* @param User $user
* @return TagState
*/
public function stateFor(User $user)
public function stateFor(User $user): TagState
{
$state = $this->state()->where('user_id', $user->id)->first();
// Use the loaded state if the relation is loaded, and either:
// 1. The state is null, or
// 2. The state belongs to the given user.
// This ensures that if a non-null state is loaded, it belongs to the correct user.
// If these conditions are not met, we query the database for the user's state.
if ($this->relationLoaded('state') && (! $this->state || $this->state->user_id === $user->id)) {

Check failure on line 173 in extensions/tags/src/Tag.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.3

Negated boolean expression is always false.

Check failure on line 173 in extensions/tags/src/Tag.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Negated boolean expression is always false.

Check failure on line 173 in extensions/tags/src/Tag.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Negated boolean expression is always false.

Check failure on line 173 in extensions/tags/src/Tag.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Negated boolean expression is always false.

Check failure on line 173 in extensions/tags/src/Tag.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

Negated boolean expression is always false.

Check failure on line 173 in extensions/tags/src/Tag.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Negated boolean expression is always false.
$state = $this->state;
} else {
$state = $this->state()->where('user_id', $user->id)->first();
}

if (! $state) {
$state = new TagState;
Expand Down

0 comments on commit 50dba27

Please sign in to comment.