Skip to content

Commit 0ae6227

Browse files
authored
Update docs (#143)
Update docs
1 parent 2960c46 commit 0ae6227

20 files changed

+158
-149
lines changed

docs/rector_rules_overview.md

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 41 Rules Overview
1+
# 45 Rules Overview
22

33
## AddArgumentDefaultValueRector
44

@@ -478,10 +478,11 @@ The EloquentMagicMethodToQueryBuilderRule is designed to automatically transform
478478
- class: [`RectorLaravel\Rector\StaticCall\EloquentMagicMethodToQueryBuilderRector`](../src/Rector/StaticCall/EloquentMagicMethodToQueryBuilderRector.php)
479479

480480
```diff
481-
-User::find(1);
482-
-User::where('email', '[email protected]')->first();
483-
+User::query()->find(1);
484-
+User::query()->where('email', '[email protected]')->first();
481+
use App\Models\User;
482+
483+
-$user = User::find(1);
484+
+$user = User::query()->find(1);
485+
```
485486

486487
<br>
487488

@@ -500,31 +501,10 @@ Changes `orderBy()` to `latest()` or `oldest()`
500501
+$builder->latest();
501502
+$builder->oldest();
502503
+$builder->latest('deleted_at');
503-
504-
use App\Models\User;
505-
506-
-$user = User::find(1);
507-
+$user = User::query()->find(1);
508504
```
509505

510506
<br>
511507

512-
## EloquentWhereTypeHintClosureParameterRector
513-
514-
Change typehint of closure parameter in where method of Eloquent Builder
515-
516-
- class: [`RectorLaravel\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector`](../src/Rector/MethodCall/EloquentWhereTypeHintClosureParameterRector.php)
517-
518-
```diff
519-
-$query->where(function ($query) {
520-
+$query->where(function (\Illuminate\Contracts\Database\Eloquent\Builder $query) {
521-
$query->where('id', 1);
522-
});
523-
```
524-
525-
<br>
526-
527-
528508
## EloquentWhereRelationTypeHintingParameterRector
529509

530510
Add type hinting to where relation has methods e.g. `whereHas`, `orWhereHas`, `whereDoesntHave`, `orWhereDoesntHave`, `whereHasMorph`, `orWhereHasMorph`, `whereDoesntHaveMorph`, `orWhereDoesntHaveMorph`
@@ -545,6 +525,21 @@ Add type hinting to where relation has methods e.g. `whereHas`, `orWhereHas`, `w
545525

546526
<br>
547527

528+
## EloquentWhereTypeHintClosureParameterRector
529+
530+
Change typehint of closure parameter in where method of Eloquent Builder
531+
532+
- class: [`RectorLaravel\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector`](../src/Rector/MethodCall/EloquentWhereTypeHintClosureParameterRector.php)
533+
534+
```diff
535+
-$query->where(function ($query) {
536+
+$query->where(function (\Illuminate\Contracts\Database\Eloquent\Builder $query) {
537+
$query->where('id', 1);
538+
});
539+
```
540+
541+
<br>
542+
548543
## EmptyToBlankAndFilledFuncRector
549544

550545
Replace use of the unsafe `empty()` function with Laravel's safer `blank()` & `filled()` functions.

src/Rector/Assign/CallOnAppArrayAccessToStandaloneAssignRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PhpParser\Node\Expr\Variable;
1313
use PhpParser\Node\Stmt\Expression;
14+
use PhpParser\Node\Stmt\Nop;
1415
use PHPStan\Type\ObjectType;
1516
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1617
use Rector\Core\Rector\AbstractRector;
@@ -96,7 +97,7 @@ public function refactor(Node $node): Node|array|int|null
9697

9798
// the nop is a workaround because the docs of the first node are somehow stripped away
9899
// this will add a newline but the docs will be preserved
99-
return [new Node\Stmt\Nop(), $assignExpression, $node];
100+
return [new Nop(), $assignExpression, $node];
100101
}
101102

102103
return null;

src/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,7 @@ private function shouldSkipNode(ClassMethod $classMethod, Scope $scope): bool
315315
if (! $classReflection instanceof ClassReflection || $classReflection->isAnonymous()) {
316316
return true;
317317
}
318-
319-
if (! $classReflection->isTrait() && ! $classReflection->isSubclassOf('Illuminate\Database\Eloquent\Model')) {
320-
return true;
321-
}
322-
323-
return false;
318+
return ! $classReflection->isTrait() && ! $classReflection->isSubclassOf('Illuminate\Database\Eloquent\Model');
324319
}
325320

326321
/**

src/Rector/ClassMethod/MigrateToSimplifiedAttributeRector.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ private function refactorClassMethod(ClassMethod $classMethod, array $allClassMe
129129
}
130130

131131
// Skip if the new attribute name is already used
132-
foreach ($allClassMethods as $method) {
133-
if ($this->isName($method, $attributeName)) {
132+
foreach ($allClassMethods as $allClassMethod) {
133+
if ($this->isName($allClassMethod, $attributeName)) {
134134
return null;
135135
}
136136
}
@@ -276,9 +276,9 @@ private function isMutator(string $nodeName): bool
276276
*/
277277
private function findPossibleAccessor(array $allClassMethods, string $attributeName): ?ClassMethod
278278
{
279-
foreach ($allClassMethods as $classMethod) {
280-
if ($classMethod->name->toString() === 'get' . ucfirst($attributeName) . 'Attribute') {
281-
return $classMethod;
279+
foreach ($allClassMethods as $allClassMethod) {
280+
if ($allClassMethod->name->toString() === 'get' . ucfirst($attributeName) . 'Attribute') {
281+
return $allClassMethod;
282282
}
283283
}
284284

@@ -290,9 +290,9 @@ private function findPossibleAccessor(array $allClassMethods, string $attributeN
290290
*/
291291
private function findPossibleMutator(array $allClassMethods, string $attributeName): ?ClassMethod
292292
{
293-
foreach ($allClassMethods as $classMethod) {
294-
if ($classMethod->name->toString() === 'set' . ucfirst($attributeName) . 'Attribute') {
295-
return $classMethod;
293+
foreach ($allClassMethods as $allClassMethod) {
294+
if ($allClassMethod->name->toString() === 'set' . ucfirst($attributeName) . 'Attribute') {
295+
return $allClassMethod;
296296
}
297297
}
298298

src/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ public function addExtendsPhpDocTag(Node $node, Property $property): void
130130
$phpDocInfo->addPhpDocTagNode($phpDocTagNode);
131131
}
132132

133-
private function getModelName(?Expr $defaultProp): ?string
133+
private function getModelName(?Expr $expr): ?string
134134
{
135-
if ($defaultProp instanceof ClassConstFetch) {
136-
return $this->getName($defaultProp->class);
135+
if ($expr instanceof ClassConstFetch) {
136+
return $this->getName($expr->class);
137137
}
138138

139-
if ($defaultProp instanceof String_) {
140-
return $defaultProp->value;
139+
if ($expr instanceof String_) {
140+
return $expr->value;
141141
}
142142

143143
return null;

src/Rector/Class_/CashierStripeOptionsToStripeRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
7777
}
7878

7979
/** @var Class_ $node */
80-
$stripeOptionsMethod = $node->getMethod('stripeOptions');
80+
$classMethod = $node->getMethod('stripeOptions');
8181

82-
if (! $stripeOptionsMethod instanceof ClassMethod) {
82+
if (! $classMethod instanceof ClassMethod) {
8383
return null;
8484
}
8585

86-
$stripeOptionsMethod->name = new Identifier('stripe');
86+
$classMethod->name = new Identifier('stripe');
8787

8888
return $node;
8989
}

src/Rector/Expr/AppEnvironmentComparisonToParameterRector.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
namespace RectorLaravel\Rector\Expr;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
89
use PhpParser\Node\Expr;
910
use PhpParser\Node\Expr\BinaryOp\Equal;
1011
use PhpParser\Node\Expr\BinaryOp\Identical;
12+
use PhpParser\Node\Expr\MethodCall;
13+
use PhpParser\Node\Expr\StaticCall;
14+
use PhpParser\Node\Scalar\String_;
1115
use PHPStan\Type\ObjectType;
1216
use Rector\Core\Rector\AbstractRector;
1317
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -41,7 +45,7 @@ public function getNodeTypes(): array
4145
return [Expr::class];
4246
}
4347

44-
public function refactor(Node $node): Expr\MethodCall|Expr\StaticCall|null
48+
public function refactor(Node $node): MethodCall|StaticCall|null
4549
{
4650
if (! $node instanceof Identical && ! $node instanceof Equal) {
4751
return null;
@@ -51,7 +55,7 @@ public function refactor(Node $node): Expr\MethodCall|Expr\StaticCall|null
5155
$methodCall = array_values(
5256
array_filter(
5357
[$node->left, $node->right],
54-
fn ($node) => ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\StaticCall) && $this->isName(
58+
fn ($node) => ($node instanceof MethodCall || $node instanceof StaticCall) && $this->isName(
5559
$node->name,
5660
'environment'
5761
)
@@ -67,7 +71,7 @@ public function refactor(Node $node): Expr\MethodCall|Expr\StaticCall|null
6771
array_filter([$node->left, $node->right], static fn ($node) => $node !== $methodCall)
6872
)[0] ?? null;
6973

70-
if (! $otherNode instanceof Node\Scalar\String_) {
74+
if (! $otherNode instanceof String_) {
7175
return null;
7276
}
7377

@@ -76,23 +80,23 @@ public function refactor(Node $node): Expr\MethodCall|Expr\StaticCall|null
7680
return null;
7781
}
7882

79-
$methodCall->args[] = new Node\Arg($otherNode);
83+
$methodCall->args[] = new Arg($otherNode);
8084

8185
return $methodCall;
8286
}
8387

84-
private function validMethodCall(Expr\MethodCall|Expr\StaticCall $methodCall): bool
88+
private function validMethodCall(MethodCall|StaticCall $methodCall): bool
8589
{
8690
return match (true) {
87-
$methodCall instanceof Node\Expr\MethodCall && $this->isObjectType(
91+
$methodCall instanceof MethodCall && $this->isObjectType(
8892
$methodCall->var,
8993
new ObjectType('Illuminate\Contracts\Foundation\Application')
9094
) => true,
91-
$methodCall instanceof Node\Expr\StaticCall && $this->isObjectType(
95+
$methodCall instanceof StaticCall && $this->isObjectType(
9296
$methodCall->class,
9397
new ObjectType('Illuminate\Support\Facades\App')
9498
) => true,
95-
$methodCall instanceof Node\Expr\StaticCall && $this->isObjectType(
99+
$methodCall instanceof StaticCall && $this->isObjectType(
96100
$methodCall->class,
97101
new ObjectType('App')
98102
) => true,

src/Rector/FuncCall/ArgumentFuncCallToMethodCallRector.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,21 @@ public function configure(array $configuration): void
170170
}
171171

172172
public function refactorFuncCallToMethodCall(
173-
FuncCall $node,
173+
FuncCall $funcCall,
174174
ArgumentFuncCallToMethodCall $argumentFuncCallToMethodCall,
175175
MethodCall|PropertyFetch|Variable $expr
176176
): MethodCall|PropertyFetch|Variable {
177-
if ($node->args === []) {
177+
if ($funcCall->args === []) {
178178
return $this->refactorEmptyFuncCallArgs($argumentFuncCallToMethodCall, $expr);
179179
}
180180

181-
$methodName = $argumentFuncCallToMethodCall->getMethodIfArgs();
181+
$methodIfArgs = $argumentFuncCallToMethodCall->getMethodIfArgs();
182182

183-
if (! is_string($methodName)) {
183+
if (! is_string($methodIfArgs)) {
184184
throw new ShouldNotHappenException();
185185
}
186186

187-
return $this->nodeFactory->createMethodCall($expr, $methodName, $node->args);
187+
return $this->nodeFactory->createMethodCall($expr, $methodIfArgs, $funcCall->args);
188188
}
189189

190190
private function refactorArrayFunctionToMethodCall(

src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php

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

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\FuncCall;
9+
use PhpParser\Node\Stmt\Expression;
810
use Rector\Core\Rector\AbstractRector;
911
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1012
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -35,15 +37,15 @@ public function getRuleDefinition(): RuleDefinition
3537

3638
public function getNodeTypes(): array
3739
{
38-
return [Node\Stmt\Expression::class];
40+
return [Expression::class];
3941
}
4042

4143
/**
4244
* @param Node\Stmt\Expression $node
4345
*/
4446
public function refactor(Node $node): ?Node
4547
{
46-
if (! $node->expr instanceof Node\Expr\FuncCall) {
48+
if (! $node->expr instanceof FuncCall) {
4749
return null;
4850
}
4951

src/Rector/MethodCall/EloquentOrderByToLatestOrOldestRector.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
namespace RectorLaravel\Rector\MethodCall;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr;
810
use PhpParser\Node\Expr\MethodCall;
11+
use PhpParser\Node\Identifier;
12+
use PhpParser\Node\Scalar\String_;
13+
use PhpParser\Node\VariadicPlaceholder;
914
use PHPStan\Type\ObjectType;
1015
use Rector\Core\Rector\AbstractRector;
1116
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -66,19 +71,19 @@ private function isOrderByMethodCall(MethodCall $methodCall): bool
6671
// Check if it's a method call to `orderBy`
6772

6873
return $this->isObjectType($methodCall->var, new ObjectType('Illuminate\Database\Query\Builder'))
69-
&& $methodCall->name instanceof Node\Identifier
74+
&& $methodCall->name instanceof Identifier
7075
&& ($methodCall->name->name === 'orderBy' || $methodCall->name->name === 'orderByDesc')
71-
&& count($methodCall->args) > 0;
76+
&& $methodCall->args !== [];
7277
}
7378

7479
private function convertOrderByToLatest(MethodCall $methodCall): MethodCall
7580
{
76-
if (! isset($methodCall->args[0]) && ! $methodCall->args[0] instanceof Node\VariadicPlaceholder) {
81+
if (! isset($methodCall->args[0]) && ! $methodCall->args[0] instanceof VariadicPlaceholder) {
7782
return $methodCall;
7883
}
7984

8085
$columnVar = $methodCall->args[0]->value ?? null;
81-
if ($columnVar === null) {
86+
if (! $columnVar instanceof Expr) {
8287
return $methodCall;
8388
}
8489

@@ -88,22 +93,22 @@ private function convertOrderByToLatest(MethodCall $methodCall): MethodCall
8893
} else {
8994
$newMethod = $direction === 'asc' ? 'latest' : 'oldest';
9095
}
91-
if ($columnVar instanceof Node\Scalar\String_ && $columnVar->value === 'created_at') {
92-
$methodCall->name = new Node\Identifier($newMethod);
96+
if ($columnVar instanceof String_ && $columnVar->value === 'created_at') {
97+
$methodCall->name = new Identifier($newMethod);
9398
$methodCall->args = [];
9499

95100
return $methodCall;
96101
}
97102

98-
if ($columnVar instanceof Node\Scalar\String_) {
99-
$methodCall->name = new Node\Identifier($newMethod);
100-
$methodCall->args = [new Node\Arg(new Node\Scalar\String_($columnVar->value))];
103+
if ($columnVar instanceof String_) {
104+
$methodCall->name = new Identifier($newMethod);
105+
$methodCall->args = [new Arg(new String_($columnVar->value))];
101106

102107
return $methodCall;
103108
}
104109

105-
$methodCall->name = new Node\Identifier($newMethod);
106-
$methodCall->args = [new Node\Arg($columnVar)];
110+
$methodCall->name = new Identifier($newMethod);
111+
$methodCall->args = [new Arg($columnVar)];
107112

108113
return $methodCall;
109114
}

0 commit comments

Comments
 (0)