Skip to content

Conversation

@imliam
Copy link
Contributor

@imliam imliam commented Oct 4, 2025

- $team->user_id === $user->id;
+ $team->user()->is($user);

Comment on lines +57 to +59
if (! $node instanceof Equal && ! $node instanceof Identical) {
return null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might be a redundant check, since we're always passing Equal or Identical.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is. The docblock removes any PHPStan ambiguity.

Comment on lines +116 to +124
private function isForeignKeyToIdPattern(string $leftProperty, string $rightProperty): bool
{
return str_ends_with($leftProperty, '_id') && $rightProperty === 'id';
}

private function extractRelationshipName(string $foreignKeyProperty): string
{
return substr($foreignKeyProperty, 0, -3);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm afraid this part here won't work well with a lot of cases.

It assumes that the key is always 'id' or follows the standard '_id'. I'm not sure if we can find the key name from the model's getKeyName() method or the $primaryKey property.

Also, it fails when relationship names are complex (nationalTeam() with national_team_id key), or don't exist (external_team_id, but no externalTeam() relationship).

The only way this could work well would be to check the relationships.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree. In theory we can build better analysers for this, to look at the model, then the relationship methods on the model, then to determine with default or custom keys are used. It's possible but complicated. Larastan project might have something like this already to base the analyser off of.

/**
* @param Equal|Identical $node
*/
public function refactor(Node $node): ?Node
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
public function refactor(Node $node): ?Node
public function refactor(Node $node): Equal|Identical|null

Comment on lines +5 to +40
class User
{
public $id;
}

class Post
{
public $author_id;
public $editor_id;

public function author()
{
return $this->belongsTo(User::class);
}

public function editor()
{
return $this->belongsTo(User::class);
}
}

class Comment
{
public $post_id;
public $author_id;

public function post()
{
return $this->belongsTo(Post::class);
}

public function author()
{
return $this->belongsTo(User::class);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't typically define all this in the fixtures. Instead in the test folder we add a Source folder and inside that define classes we can reference in the fixtures. Leads to less to read and human error when updating fixtures.

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