Skip to content

Commit 4bf9ecb

Browse files
authored
Merge branch 'driftingly:main' into feat/array-to-data_get-rule
2 parents 8e09b67 + d459ec1 commit 4bf9ecb

File tree

7 files changed

+84
-2
lines changed

7 files changed

+84
-2
lines changed

src/Rector/ClassMethod/MakeModelAttributesAndScopesProtectedRector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ private function shouldSkipNode(ClassMethod $classMethod, Scope $scope): bool
113113
return true;
114114
}
115115

116+
if (
117+
$classReflection->getParentClass() instanceof ClassReflection &&
118+
$classReflection->getParentClass()->hasMethod($this->getName($classMethod)) &&
119+
$classReflection->getParentClass()->getMethod($this->getName($classMethod), $scope)->isPublic()
120+
) {
121+
return true;
122+
}
123+
116124
return ! $classReflection->isTrait()
117125
&& ! $classReflection->is('Illuminate\Database\Eloquent\Model');
118126
}
@@ -136,7 +144,7 @@ private function isScopeMethod(ClassMethod $classMethod): bool
136144
{
137145
$name = $this->getName($classMethod);
138146

139-
if (str_starts_with((string) $name, 'scope')) {
147+
if ((bool) preg_match('/^scope.+$/', $name)) {
140148
return true;
141149
}
142150

src/Rector/MethodCall/ContainerBindConcreteWithClosureOnlyRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Const_;
77
use PhpParser\Node\Expr\Closure;
88
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\Variable;
910
use PHPStan\Type\ObjectType;
1011
use PHPStan\Type\Type;
1112
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
@@ -74,6 +75,10 @@ public function refactor(Node $node): ?MethodCall
7475
$classString = $node->getArgs()[0]->value;
7576
$concreteNode = $node->getArgs()[1]->value;
7677

78+
if ($classString instanceof Variable) {
79+
return null;
80+
}
81+
7782
if (! $concreteNode instanceof Closure) {
7883
return null;
7984
}

src/Rector/MethodCall/ConvertEnumerableToArrayToAllRector.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Identifier;
1111
use PHPStan\Type\ObjectType;
1212
use PHPStan\Type\TypeCombinator;
13+
use Rector\PHPStan\ScopeFetcher;
1314
use RectorLaravel\AbstractRector;
1415
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1516
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -62,7 +63,12 @@ public function refactor(Node $node): ?Node
6263
return null;
6364
}
6465

65-
$type = TypeCombinator::removeNull($this->getType($node->var));
66+
$scope = ScopeFetcher::fetch($node);
67+
$type = TypeCombinator::removeNull(
68+
// This HAS to use the $scope->getType() as opposed to $this->getType()
69+
// because it's not getting the proper type from the Larastan extensions
70+
$scope->getType($node->var)
71+
);
6672
$valueType = $type->getTemplateType('Illuminate\Support\Enumerable', 'TValue');
6773

6874
if (! (new ObjectType('Illuminate\Contracts\Support\Arrayable'))->isSuperTypeOf($valueType)->no()) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector\Fixture;
4+
5+
use Illuminate\Database\Eloquent\Casts\Attribute;
6+
use Illuminate\Database\Eloquent\Builder;
7+
use RectorLaravel\Tests\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector\Source\ParentUser;
8+
9+
class User extends ParentUser
10+
{
11+
protected function firstName(): Attribute
12+
{
13+
return Attribute::get(fn () => ucfirst($this->first_name));
14+
}
15+
16+
public function scopeActive(Builder $query): Builder
17+
{
18+
return $query->where('active', true);
19+
}
20+
}
21+
22+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector\Fixture;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class SkipScopeMethodNotScope extends Model
8+
{
9+
public function scope(): string
10+
{
11+
return 'foo';
12+
}
13+
}
14+
15+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\ClassMethod\MakeModelAttributesAndScopesProtectedRector\Source;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class ParentUser extends Model
9+
{
10+
public function scopeActive(Builder $query): Builder
11+
{
12+
return $query->where('active', true);
13+
}
14+
}

tests/Rector/MethodCall/ContainerBindConcreteWithClosureOnlyRector/Fixture/fixture.php.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ $app->singleton(SomeInterface::class, function () {
1919
return new SomeClass();
2020
});
2121

22+
foreach ([SomeClass::class] as $item) {
23+
$app->singleton($item, function () use ($item) {
24+
return new $item();
25+
});
26+
}
27+
2228
?>
2329
-----
2430
<?php
@@ -42,4 +48,10 @@ $app->singleton(function (): \RectorLaravel\Tests\Rector\MethodCall\ContainerBin
4248
return new SomeClass();
4349
});
4450

51+
foreach ([SomeClass::class] as $item) {
52+
$app->singleton($item, function () use ($item) {
53+
return new $item();
54+
});
55+
}
56+
4557
?>

0 commit comments

Comments
 (0)