Skip to content

Commit ec4c21d

Browse files
committed
fix: skip more scope methods that aren't scopes based on types
1 parent 10b6882 commit ec4c21d

File tree

5 files changed

+75
-17
lines changed

5 files changed

+75
-17
lines changed

src/Rector/ClassMethod/MakeModelAttributesAndScopesProtectedRector.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace RectorLaravel\Rector\ClassMethod;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Identifier;
89
use PhpParser\Node\Stmt\ClassMethod;
910
use PHPStan\Analyser\Scope;
1011
use PHPStan\Reflection\ClassReflection;
@@ -142,12 +143,28 @@ private function isAttributeMethod(ClassMethod $classMethod): bool
142143

143144
private function isScopeMethod(ClassMethod $classMethod): bool
144145
{
146+
if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, 'Illuminate\Database\Eloquent\Attributes\Scope')) {
147+
return true;
148+
}
149+
145150
$name = $this->getName($classMethod);
146151

147-
if ((bool) preg_match('/^scope.+$/', $name)) {
152+
if (! (bool) preg_match('/^scope.+$/', $name)) {
153+
return false;
154+
}
155+
156+
if (! isset($classMethod->params[0]) || ! $classMethod->params[0] instanceof Node) {
157+
return false;
158+
}
159+
160+
if (! $this->isObjectType($classMethod->params[0], new ObjectType('Illuminate\Database\Eloquent\Builder'))) {
161+
return false;
162+
}
163+
164+
if ($classMethod->returnType instanceof Identifier && $classMethod->returnType->toString() === 'void') {
148165
return true;
149166
}
150167

151-
return $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, 'Illuminate\Database\Eloquent\Attributes\Scope');
168+
return $classMethod->returnType instanceof Node && $this->isObjectType($classMethod->returnType, new ObjectType('Illuminate\Database\Eloquent\Builder'));
152169
}
153170
}

tests/Rector/ClassMethod/MakeModelAttributesAndScopesProtectedRector/Fixture/final_model.php.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ final class User extends Model
2323
return $query;
2424
}
2525

26+
private function scopeInactive(Builder $query): void
27+
{
28+
$query->where('active', false);
29+
}
30+
2631
#[Scope]
2732
private function verified(Builder $query): Builder
2833
{
@@ -57,6 +62,11 @@ final class User extends Model
5762
return $query;
5863
}
5964

65+
protected function scopeInactive(Builder $query): void
66+
{
67+
$query->where('active', false);
68+
}
69+
6070
#[Scope]
6171
protected function verified(Builder $query): Builder
6272
{

tests/Rector/ClassMethod/MakeModelAttributesAndScopesProtectedRector/Fixture/fixture.php.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class User extends Model
2323
return $query;
2424
}
2525

26+
public function scopeInactive(Builder $query): void
27+
{
28+
$query->where('active', false);
29+
}
30+
2631
#[Scope]
2732
public function verified(Builder $query): Builder
2833
{
@@ -64,6 +69,11 @@ class User extends Model
6469
return $query;
6570
}
6671

72+
protected function scopeInactive(Builder $query): void
73+
{
74+
$query->where('active', false);
75+
}
76+
6777
#[Scope]
6878
protected function verified(Builder $query): Builder
6979
{

tests/Rector/ClassMethod/MakeModelAttributesAndScopesProtectedRector/Fixture/skip_scope_method_not_scope.php.inc

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector\Fixture;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
9+
class SkipScopeMethodsNotScope extends Model
10+
{
11+
public function scope(): string
12+
{
13+
return 'foo';
14+
}
15+
16+
public function scopedItems(): HasMany
17+
{
18+
return $this->hasMany(ScopedItem::class);
19+
}
20+
21+
public function scopeQuery(): Builder
22+
{
23+
return $this->where('foo', true);
24+
}
25+
26+
public function scopeString(Builder $builder): string
27+
{
28+
return 'foo';
29+
}
30+
31+
public function scopeVoid(): void {}
32+
33+
public function scopeNoReturn(Builder $builder) {}
34+
}
35+
36+
?>

0 commit comments

Comments
 (0)