-
Notifications
You must be signed in to change notification settings - Fork 111
Fix Scope detection false positives #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
calebdw
merged 9 commits into
driftingly:main
from
nikspyratos:fix/scope-detection-false-positives
Apr 20, 2026
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e5f39f3
Feat: Update local scope analyzing rules to check the scope is not a
nikspyratos 910bea6
Update fixtures
nikspyratos 3d64e2b
Simplify ScopeAnalyzer
nikspyratos 2a97275
Cover edge cases from #409
nikspyratos f1807b7
Feedback: directly call scopeAnalyzer service from
nikspyratos d0e83ce
Feedback: Simplify named scope check to suggested regex, ditch return
nikspyratos 19a1523
Update tests per feedback cases
nikspyratos afb33fa
Fix PHPStan notice
nikspyratos ac64d32
Merge branch 'main' into fix/scope-detection-false-positives
calebdw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace RectorLaravel\NodeAnalyzer; | ||
|
|
||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\Stmt\ClassMethod; | ||
| use PHPStan\Type\ObjectType; | ||
| use Rector\NodeNameResolver\NodeNameResolver; | ||
| use Rector\NodeTypeResolver\NodeTypeResolver; | ||
| use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; | ||
|
|
||
| final readonly class ScopeAnalyzer | ||
| { | ||
| private const string SCOPE_ATTRIBUTE = 'Illuminate\Database\Eloquent\Attributes\Scope'; | ||
|
|
||
| private const string ELOQUENT_BUILDER = 'Illuminate\Database\Eloquent\Builder'; | ||
|
|
||
| public function __construct( | ||
| private NodeNameResolver $nodeNameResolver, | ||
| private NodeTypeResolver $nodeTypeResolver, | ||
| private PhpAttributeAnalyzer $phpAttributeAnalyzer, | ||
| ) {} | ||
|
|
||
| /** | ||
| * Checks for the "scope" + uppercase char naming convention, a Builder-typed | ||
| * first parameter, and a void/Builder/untyped return. | ||
| */ | ||
| public function isNamedScope(ClassMethod $classMethod): bool | ||
| { | ||
| $name = $this->nodeNameResolver->getName($classMethod); | ||
|
|
||
| if ($name === null || ! str_starts_with($name, 'scope') || strlen($name) <= 5 || ! ctype_upper($name[5])) { | ||
| return false; | ||
| } | ||
|
calebdw marked this conversation as resolved.
Outdated
|
||
|
|
||
| return $this->hasBuilderFirstParameter($classMethod) && $this->hasScopeReturnType($classMethod); | ||
| } | ||
|
|
||
| /** | ||
| * Named scope OR #[Scope] attribute. | ||
| */ | ||
| public function isScopeMethod(ClassMethod $classMethod): bool | ||
| { | ||
| return $this->isNamedScope($classMethod) | ||
| || $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::SCOPE_ATTRIBUTE); | ||
| } | ||
|
|
||
| private function hasBuilderFirstParameter(ClassMethod $classMethod): bool | ||
| { | ||
| if ($classMethod->params === []) { | ||
| return false; | ||
| } | ||
|
|
||
| $firstParam = $classMethod->params[0]; | ||
|
|
||
| if ($firstParam->type === null) { | ||
| return true; | ||
| } | ||
|
|
||
| return $this->nodeTypeResolver->isObjectType($firstParam->type, new ObjectType(self::ELOQUENT_BUILDER)); | ||
| } | ||
|
|
||
| private function hasScopeReturnType(ClassMethod $classMethod): bool | ||
| { | ||
| if ($classMethod->returnType === null) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($classMethod->returnType instanceof Identifier && $classMethod->returnType->toString() === 'void') { | ||
| return true; | ||
| } | ||
|
calebdw marked this conversation as resolved.
Outdated
|
||
|
|
||
| return $this->nodeTypeResolver->isObjectType($classMethod->returnType, new ObjectType(self::ELOQUENT_BUILDER)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...hod/MakeModelAttributesAndScopesProtectedRector/Fixture/skip_non_scope_signatures.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
|
||
| class SkipNonScopeSignatures extends Model | ||
| { | ||
| public function scopeString(Builder $query): string | ||
| { | ||
| return 'foo'; | ||
| } | ||
|
|
||
| public function scopeParam(string $param): Builder | ||
| { | ||
| return $this->where('foo', true); | ||
| } | ||
|
|
||
| public function scopeVoid(): void {} | ||
|
|
||
| public function scopeItem(): BelongsTo | ||
| { | ||
| return $this->belongsTo(ScopeItem::class); | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...sMethod/MakeModelAttributesAndScopesProtectedRector/Fixture/skip_relation_methods.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
|
||
| class SkipRelationMethods extends Model | ||
| { | ||
| public function scopeTypes(): HasMany | ||
| { | ||
| return $this->hasMany(ScopeType::class); | ||
| } | ||
| } | ||
|
|
||
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ClassMethodToScopeAttributedClassMethodRector/Fixture/scope_with_extra_parameters.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ScopeNamedClassMethodToScopeAttributedClassMethodRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| class ScopeWithExtraParameters extends Model | ||
| { | ||
| public function scopeOfType(Builder $query, string $type) | ||
| { | ||
| return $query->where('type', $type); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ScopeNamedClassMethodToScopeAttributedClassMethodRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| class ScopeWithExtraParameters extends Model | ||
| { | ||
| #[\Illuminate\Database\Eloquent\Attributes\Scope] | ||
| protected function ofType(Builder $query, string $type) | ||
| { | ||
| return $query->where('type', $type); | ||
| } | ||
| } | ||
|
|
||
| ?> |
14 changes: 14 additions & 0 deletions
14
...lassMethodToScopeAttributedClassMethodRector/Fixture/skip_non_builder_first_param.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ScopeNamedClassMethodToScopeAttributedClassMethodRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| class SkipNonBuilderFirstParam extends Model | ||
| { | ||
| public function scopeCategory(string $category): void | ||
| { | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...dClassMethodToScopeAttributedClassMethodRector/Fixture/skip_non_scope_return_type.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ScopeNamedClassMethodToScopeAttributedClassMethodRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| class SkipNonScopeReturnType extends Model | ||
| { | ||
| public function scopeString(Builder $query): string | ||
| { | ||
| return 'foo'; | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...eNamedClassMethodToScopeAttributedClassMethodRector/Fixture/skip_relation_methods.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ScopeNamedClassMethodToScopeAttributedClassMethodRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
|
||
| class SkipRelationMethods extends Model | ||
| { | ||
| public function scopeTypes(): HasMany | ||
| { | ||
| return $this->hasMany(ScopeType::class); | ||
| } | ||
| } | ||
|
|
||
| ?> |
34 changes: 34 additions & 0 deletions
34
...amedClassMethodToScopeAttributedClassMethodRector/Fixture/typed_builder_parameter.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ScopeNamedClassMethodToScopeAttributedClassMethodRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| class TypedBuilderParameter extends Model | ||
| { | ||
| public function scopeActive(Builder $query) | ||
| { | ||
| return $query->where('active', 1); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace RectorLaravel\Tests\Rector\ScopeNamedClassMethodToScopeAttributedClassMethodRector\Fixture; | ||
|
|
||
| use Illuminate\Database\Eloquent\Builder; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| class TypedBuilderParameter extends Model | ||
| { | ||
| #[\Illuminate\Database\Eloquent\Attributes\Scope] | ||
| protected function active(Builder $query) | ||
| { | ||
| return $query->where('active', 1); | ||
| } | ||
| } | ||
|
|
||
| ?> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.