Skip to content

Commit fa6683b

Browse files
authored
Merge branch 'main' into feature/declare-type-configs
2 parents 9ffe12a + dea1359 commit fa6683b

33 files changed

+1028
-16
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Rector Rules for Laravel
2+
[![Tests](https://github.com/driftingly/rector-laravel/actions/workflows/tests.yaml/badge.svg)](https://github.com/driftingly/rector-laravel/actions/workflows/tests.yaml)
3+
[![Code Analysis](https://github.com/driftingly/rector-laravel/actions/workflows/code_analysis.yaml/badge.svg)](https://github.com/driftingly/rector-laravel/actions/workflows/code_analysis.yaml)
4+
[![Packagist Downloads](https://img.shields.io/packagist/dm/driftingly/rector-laravel)](https://packagist.org/packages/driftingly/rector-laravel/stats)
5+
[![Packagist Version](https://img.shields.io/packagist/v/driftingly/rector-laravel)](https://packagist.org/packages/driftingly/rector-laravel)
26

37
See available [Laravel rules](/docs/rector_rules_overview.md)
48

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
"rector/rector": "^1.0.2"
99
},
1010
"require-dev": {
11-
"phpunit/phpunit": "^10.5",
11+
"nikic/php-parser": "^4.18",
12+
"phpstan/extension-installer": "^1.3",
1213
"phpstan/phpstan": "^1.10",
13-
"symplify/phpstan-rules": "^12.4",
14+
"phpstan/phpstan-strict-rules": "^1.5",
15+
"phpstan/phpstan-webmozart-assert": "^1.2",
16+
"phpunit/phpunit": "^10.5",
1417
"symplify/phpstan-extensions": "^11.4",
18+
"symplify/phpstan-rules": "^12.4",
1519
"symplify/rule-doc-generator": "^12.1",
16-
"phpstan/extension-installer": "^1.3",
17-
"phpstan/phpstan-webmozart-assert": "^1.2",
18-
"phpstan/phpstan-strict-rules": "^1.5",
1920
"tightenco/duster": "^2.7"
2021
},
2122
"autoload": {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpParser\Node\Expr\ClassConstFetch;
6+
use PhpParser\Node\Name\FullyQualified;
7+
use Rector\Config\RectorConfig;
8+
use RectorLaravel\Rector\MethodCall\ReplaceServiceContainerCallArgRector;
9+
use RectorLaravel\ValueObject\ReplaceServiceContainerCallArg;
10+
11+
return static function (RectorConfig $rectorConfig): void {
12+
$rectorConfig->import(__DIR__ . '/../config.php');
13+
14+
$servicesMap = [
15+
'events' => 'Illuminate\Contracts\Events\Dispatcher',
16+
'log' => 'Psr\Log\LoggerInterface',
17+
'router' => 'Illuminate\Routing\Router',
18+
'url' => 'Illuminate\Contracts\Routing\UrlGenerator',
19+
'redirect' => 'Illuminate\Routing\Redirector',
20+
'auth' => 'Illuminate\Contracts\Auth\Factory',
21+
'auth.driver' => 'Illuminate\Auth\SessionGuard',
22+
'cookie' => 'Illuminate\Cookie\CookieJar',
23+
'db.factory' => 'Illuminate\Database\Connectors\ConnectionFactory',
24+
'db' => 'Illuminate\Database\ConnectionResolverInterface',
25+
'db.connection' => 'Illuminate\Database\ConnectionInterface',
26+
'db.schema' => 'Illuminate\Database\Schema\SQLiteBuilder',
27+
'db.transactions' => 'Illuminate\Database\DatabaseTransactionsManager',
28+
'encrypter' => 'Illuminate\Encryption\Encrypter',
29+
'files' => 'Illuminate\Filesystem\Filesystem',
30+
'filesystem' => 'Illuminate\Contracts\Filesystem\Factory',
31+
'session' => 'Illuminate\Session\SessionManager',
32+
'session.store' => 'Illuminate\Contracts\Session\Session',
33+
'view' => 'Illuminate\Contracts\View\Factory',
34+
'view.finder' => 'Illuminate\View\ViewFinderInterface',
35+
'blade.compiler' => 'Illuminate\View\Compilers\CompilerInterface',
36+
'view.engine.resolver' => 'Illuminate\View\Engines\EngineResolver',
37+
'flare.logger' => 'Monolog\Logger',
38+
'cache' => 'Illuminate\Contracts\Cache\Factory',
39+
'cache.store' => 'Illuminate\Cache\Repository',
40+
'memcached.connector' => 'Illuminate\Cache\MemcachedConnector',
41+
'queue' => 'Illuminate\Queue\QueueManager',
42+
'queue.connection' => 'Illuminate\Contracts\Queue\Queue',
43+
'queue.worker' => 'Illuminate\Queue\Worker',
44+
'queue.listener' => 'Illuminate\Queue\Listener',
45+
'queue.failer' => 'Illuminate\Queue\Failed\DatabaseUuidFailedJobProvider',
46+
'migration.repository' => 'Illuminate\Database\Migrations\MigrationRepositoryInterface',
47+
'migrator' => 'Illuminate\Database\Migrations\Migrator',
48+
'migration.creator' => 'Illuminate\Database\Migrations\MigrationCreator',
49+
'composer' => 'Illuminate\Support\Composer',
50+
'hash' => 'Illuminate\Contracts\Hashing\Hasher',
51+
'hash.driver' => 'Illuminate\Contracts\Hashing\Hasher',
52+
'mail.manager' => 'Illuminate\Contracts\Mail\Factory',
53+
'mailer' => 'Illuminate\Mail\Mailer',
54+
'auth.password' => 'Illuminate\Contracts\Auth\PasswordBrokerFactory',
55+
'auth.password.broker' => 'Illuminate\Contracts\Auth\PasswordBroker',
56+
'pipeline' => 'Illuminate\Contracts\Pipeline\Pipeline',
57+
'redis' => 'Illuminate\Contracts\Redis\Factory',
58+
'translation.loader' => 'Illuminate\Contracts\Translation\Loader',
59+
'translator' => 'Illuminate\Contracts\Translation\Translator',
60+
'validation.presence' => 'Illuminate\Validation\DatabasePresenceVerifier',
61+
'validator' => 'Illuminate\Contracts\Validation\Factory',
62+
'command.tinker' => 'Laravel\Tinker\Console\TinkerCommand',
63+
];
64+
65+
$ruleConfig = array_map(
66+
fn (string $service, string $interface) => new ReplaceServiceContainerCallArg(
67+
$service,
68+
new ClassConstFetch(
69+
new FullyQualified($interface),
70+
'class'
71+
)
72+
),
73+
array_keys($servicesMap),
74+
$servicesMap,
75+
);
76+
77+
$rectorConfig->ruleWithConfiguration(
78+
ReplaceServiceContainerCallArgRector::class,
79+
$ruleConfig
80+
);
81+
};

config/sets/laravel-if-helpers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
use Rector\Config\RectorConfig;
5+
use RectorLaravel\Rector\If_\AbortIfRector;
6+
use RectorLaravel\Rector\If_\ReportIfRector;
7+
use RectorLaravel\Rector\If_\ThrowIfRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->import(__DIR__ . '/../config.php');
11+
12+
$rectorConfig->rule(AbortIfRector::class);
13+
$rectorConfig->rule(ReportIfRector::class);
14+
$rectorConfig->rule(ThrowIfRector::class);
15+
};

docs/rector_rules_overview.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
# 57 Rules Overview
1+
# 60 Rules Overview
2+
3+
## AbortIfRector
4+
5+
Change if abort to abort_if
6+
7+
- class: [`RectorLaravel\Rector\If_\AbortIfRector`](../src/Rector/If_/AbortIfRector.php)
8+
9+
```diff
10+
-if ($condition) {
11+
- abort(404);
12+
-}
13+
-if (!$condition) {
14+
- abort(404);
15+
-}
16+
+abort_if($condition, 404);
17+
+abort_unless($condition, 404);
18+
```
19+
20+
<br>
221

322
## AddArgumentDefaultValueRector
423

@@ -450,6 +469,26 @@ Dispatch non ShouldQueue jobs to dispatchSync
450469

451470
<br>
452471

472+
## DispatchToHelperFunctionsRector
473+
474+
Use the event or dispatch helpers instead of the static dispatch method.
475+
476+
- class: [`RectorLaravel\Rector\StaticCall\DispatchToHelperFunctionsRector`](../src/Rector/StaticCall/DispatchToHelperFunctionsRector.php)
477+
478+
```diff
479+
-ExampleEvent::dispatch($email);
480+
+event(new ExampleEvent($email));
481+
```
482+
483+
<br>
484+
485+
```diff
486+
-ExampleJob::dispatch($email);
487+
+dispatch(new ExampleJob($email));
488+
```
489+
490+
<br>
491+
453492
## EloquentMagicMethodToQueryBuilderRector
454493

455494
The EloquentMagicMethodToQueryBuilderRule is designed to automatically transform certain magic method calls on Eloquent Models into corresponding Query Builder method calls.
@@ -1049,6 +1088,25 @@ Replace `withoutJobs`, `withoutEvents` and `withoutNotifications` with Facade `f
10491088

10501089
<br>
10511090

1091+
## ReportIfRector
1092+
1093+
Change if report to report_if
1094+
1095+
- class: [`RectorLaravel\Rector\If_\ReportIfRector`](../src/Rector/If_/ReportIfRector.php)
1096+
1097+
```diff
1098+
-if ($condition) {
1099+
- report(new Exception());
1100+
-}
1101+
-if (!$condition) {
1102+
- report(new Exception());
1103+
-}
1104+
+report_if($condition, new Exception());
1105+
+report_unless($condition, new Exception());
1106+
```
1107+
1108+
<br>
1109+
10521110
## RequestStaticValidateToInjectRector
10531111

10541112
Change static `validate()` method to `$request->validate()`

rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
->withPaths([
1111
__DIR__ . '/src',
1212
__DIR__ . '/tests',
13+
__DIR__ . '/config',
1314
])
1415
->withSkip([
1516
// for tests

src/Rector/FuncCall/RemoveRedundantValueCallsRector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr\FuncCall;
77
use PhpParser\Node\Name;
8-
use PHPStan\Analyser\Scope;
98
use PHPStan\Type\ClosureType;
109
use PHPStan\Type\MixedType;
11-
use Rector\Rector\AbstractScopeAwareRector;
10+
use Rector\Rector\AbstractRector;
1211
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1312
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1413

1514
/**
1615
* @see \RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantValueCallsRector\RemoveRedundantValueCallsRectorTest
1716
*/
18-
class RemoveRedundantValueCallsRector extends AbstractScopeAwareRector
17+
class RemoveRedundantValueCallsRector extends AbstractRector
1918
{
2019
public function getRuleDefinition(): RuleDefinition
2120
{
@@ -32,7 +31,7 @@ public function getNodeTypes(): array
3231
return [FuncCall::class];
3332
}
3433

35-
public function refactorWithScope(Node $node, Scope $scope): ?Node
34+
public function refactor(Node $node): ?Node
3635
{
3736
if (! $node instanceof FuncCall) {
3837
return null;
@@ -52,7 +51,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
5251
return null;
5352
}
5453

55-
if ($scope->getType($args[0]->value)->isSuperTypeOf(new ClosureType([], new MixedType, true))->no() === false) {
54+
if ($this->getType($args[0]->value)->isSuperTypeOf(new ClosureType([], new MixedType, true))->no() === false) {
5655
return null;
5756
}
5857

src/Rector/FuncCall/RemoveRedundantWithCallsRector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr\FuncCall;
77
use PhpParser\Node\Name;
8-
use PHPStan\Analyser\Scope;
9-
use Rector\Rector\AbstractScopeAwareRector;
8+
use Rector\Rector\AbstractRector;
109
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1110
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1211

1312
/**
1413
* @see \RectorLaravel\Tests\Rector\FuncCall\RemoveRedundantWithCallsRector\RemoveRedundantWithCallsRectorTest
1514
*/
16-
class RemoveRedundantWithCallsRector extends AbstractScopeAwareRector
15+
class RemoveRedundantWithCallsRector extends AbstractRector
1716
{
1817
public function getRuleDefinition(): RuleDefinition
1918
{
@@ -30,7 +29,7 @@ public function getNodeTypes(): array
3029
return [FuncCall::class];
3130
}
3231

33-
public function refactorWithScope(Node $node, Scope $scope): ?Node
32+
public function refactor(Node $node): ?Node
3433
{
3534
if (! $node instanceof FuncCall) {
3635
return null;
@@ -51,7 +50,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
5150
}
5251

5352
if (count($args) === 2) {
54-
$secondArgumentType = $scope->getType($args[1]->value);
53+
$secondArgumentType = $this->getType($args[1]->value);
5554

5655
if ($secondArgumentType->isCallable()->no() === false) {
5756
return null;

0 commit comments

Comments
 (0)