Skip to content

Commit b548d7e

Browse files
committed
feat(rector): Add driftingly-laravel configuration
- Introduce a new configuration file for driftingly-laravel - Implement rules for renaming functions and classes in Laravel - Ensure compatibility with the Laravel framework by checking class existence
1 parent 5ae0d9b commit b548d7e

File tree

8 files changed

+106
-9
lines changed

8 files changed

+106
-9
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"bamarni/composer-bin-plugin": "^1.9",
5050
"brainmaestro/composer-git-hooks": "^2.8 || ^3.0",
5151
"composer/composer": "^2.9",
52+
"driftingly/rector-laravel": "^2.1",
5253
"ergebnis/composer-normalize": "^2.49",
5354
"ergebnis/license": "^2.7",
5455
"ergebnis/php-cs-fixer-config": "^6.59",

config/set/all.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
$rectorConfig->import(__DIR__.'/../config.php');
2121
$rectorConfig->sets([
2222
SetList::COMMON,
23+
SetList::DRIFTINGLY_LARAVEL,
2324
SetList::GUZZLE,
2425
SetList::LARAVEL,
2526
SetList::PEST,

config/set/driftingly-laravel.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/** @noinspection PhpInternalEntityUsedInspection */
4+
5+
declare(strict_types=1);
6+
7+
/**
8+
* Copyright (c) 2025-2026 guanguans<ityaozm@gmail.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*
13+
* @see https://github.com/guanguans/rector-rules
14+
*/
15+
16+
use Illuminate\Foundation\Application;
17+
use Rector\Config\RectorConfig;
18+
use RectorLaravel\Set\LaravelSetList;
19+
use function Guanguans\RectorRules\Support\classes;
20+
21+
return static function (RectorConfig $rectorConfig): void {
22+
/**
23+
* @required driftingly/rector-laravel
24+
*/
25+
if (!class_exists(LaravelSetList::class) || !class_exists(Application::class)) {
26+
return;
27+
}
28+
29+
$rectorConfig->import(__DIR__.'/../config.php');
30+
31+
$rectorConfig->sets(
32+
collect((new ReflectionClass(LaravelSetList::class))->getConstants())
33+
->reject(
34+
static fn (string $_, string $name): bool => \in_array(
35+
$name,
36+
['LARAVEL_STATIC_TO_INJECTION', 'LUMEN'],
37+
true
38+
) || preg_match('/^LARAVEL_\d{2,3}$/', $name)
39+
)
40+
// ->dd()
41+
->all()
42+
);
43+
44+
$rectorConfig->rules(
45+
classes(static fn (string $class): bool => str_starts_with($class, 'RectorLaravel\Rector'))
46+
->filter(static fn (ReflectionClass $reflectionClass): bool => $reflectionClass->isInstantiable())
47+
->keys()
48+
// ->dd()
49+
->all()
50+
);
51+
};

src/Set/SetList.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ final class SetList
2020
{
2121
public const ALL = __DIR__.'/../../config/set/all.php';
2222
public const COMMON = __DIR__.'/../../config/set/common.php';
23+
public const DRIFTINGLY_LARAVEL = __DIR__.'/../../config/set/driftingly-laravel.php';
2324
public const GUZZLE = __DIR__.'/../../config/set/guzzle.php';
2425
public const LARAVEL = __DIR__.'/../../config/set/laravel.php';
2526
public const PEST = __DIR__.'/../../config/set/pest.php';

tests/Rector/AbstractRectorTestCase.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ final public function test(string $filePath): void
9090

9191
final public static function provideCases(): iterable
9292
{
93-
return self::yieldFilesFromDirectory(static::directory().'/Fixture/');
93+
yield from self::yieldFilesFromDirectory(static::directory().'/Fixture/');
94+
95+
foreach ((array) glob(static::directory().'/Fixture[0-9]*/', \GLOB_ONLYDIR) as $directory) {
96+
if ((int) (\PHP_MAJOR_VERSION.\PHP_MINOR_VERSION) >= (int) (string) Str::of($directory)->basename()->substr(7)) {
97+
yield from self::yieldFilesFromDirectory($directory);
98+
}
99+
}
94100
}
95101

96102
abstract protected static function directory(): string;

tests/Rector/FunctionLike/RenameGarbageParamNameRector/Fixture/skips.php.inc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ interface Bar
1313
public function run($param): void;
1414
}
1515

16-
class Baz
17-
{
18-
// Skip promoted property parameter.
19-
public function __construct(private Bar $bar)
20-
{
21-
}
22-
}
23-
2416
use PhpParser\Node;
2517
use Rector\Rector\AbstractRector;
2618

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
/** @noinspection ALL */
4+
class Baz
5+
{
6+
// Skip promoted property parameter.
7+
public function __construct(private Bar $bar)
8+
{
9+
}
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/** @noinspection ALL */
4+
// @formatter:off
5+
// phpcs:ignoreFile
6+
7+
enum Suit: string
8+
{
9+
case Heart = 'Heart';
10+
case Diamond = 'Diamond';
11+
case Club = 'Club';
12+
case Spade = 'Spade';
13+
}
14+
15+
Suit::Heart;
16+
17+
?>
18+
-----
19+
<?php
20+
21+
/** @noinspection ALL */
22+
// @formatter:off
23+
// phpcs:ignoreFile
24+
25+
enum Suit: string
26+
{
27+
case Heart = 'Heart';
28+
case Diamond = 'Diamond';
29+
case Club = 'Club';
30+
case Spade = 'Spade';
31+
}
32+
33+
Suit::Heart;
34+
35+
?>

0 commit comments

Comments
 (0)