Skip to content

Commit 5105e76

Browse files
Added Rector
1 parent 19e60cf commit 5105e76

7 files changed

Lines changed: 244 additions & 3 deletions

File tree

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You can install the package using [Composer](https://getcomposer.org):
2121
```bash
2222
composer require dragon-code/codestyler --dev
2323

24-
composer config scripts.style "vendor/bin/pint --parallel"
24+
composer config scripts.style "vendor/bin/rector && vendor/bin/pint --parallel"
2525
```
2626

2727
It is also possible to establish dependence in the global area of visibility:
@@ -44,6 +44,7 @@ and `biome.json` file for [Biome Linter](https://biomejs.dev):
4444
"scripts": {
4545
"post-update-cmd": [
4646
"vendor/bin/codestyle pint 8.4",
47+
"vendor/bin/codestyle rector laravel",
4748
"vendor/bin/codestyle editorconfig",
4849
"vendor/bin/codestyle npm",
4950
"composer normalize"
@@ -59,6 +60,7 @@ When using a globally established dependence, the call must be replaced with the
5960
"scripts": {
6061
"post-update-cmd": [
6162
"codestyle pint 8.4",
63+
"codestyle rector laravel",
6264
"codestyle editorconfig",
6365
"codestyle npm",
6466
"composer normalize"
@@ -100,6 +102,35 @@ The linter is invoked by a console command:
100102
composer style
101103
```
102104

105+
### Rector
106+
107+
[`Rector`](https://getrector.com) is uses as the code rector for PHP.
108+
109+
The Rector is invoked by a console command:
110+
111+
```bash
112+
composer style
113+
```
114+
115+
To do this, make sure the file is in the root of the project.
116+
You can also automate this process by adding a call to the file copy function in the `scripts.post-update-cmd`
117+
section of the `composer.json` file.
118+
119+
```JSON
120+
{
121+
"scripts": {
122+
"post-update-cmd": [
123+
"vendor/bin/codestyle rector laravel"
124+
]
125+
}
126+
}
127+
```
128+
129+
Available presets:
130+
131+
- `laravel`
132+
- `default`
133+
103134
### Node Linter
104135

105136
[Biome](https://biomejs.dev) is used as the linter for JS, CSS and JSON.
@@ -183,11 +214,15 @@ After completing all the steps, the `composer.json` file will have the following
183214
"scripts": {
184215
"post-update-cmd": [
185216
"vendor/bin/codestyle pint 8.4",
217+
"vendor/bin/codestyle rector laravel",
186218
"vendor/bin/codestyle editorconfig",
187219
"vendor/bin/codestyle npm",
188220
"composer normalize"
189221
],
190-
"style": "vendor/bin/pint --parallel"
222+
"style": [
223+
"vendor/bin/pint --parallel",
224+
"vendor/bin/rector"
225+
]
191226
}
192227
}
193228
```

bin/codestyle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ declare(strict_types=1);
66
use DragonCode\Codestyler\Console\EditorConfigCommand;
77
use DragonCode\Codestyler\Console\NpmCommand;
88
use DragonCode\Codestyler\Console\PintCommand;
9+
use DragonCode\Codestyler\Console\RectorCommand;
910
use Symfony\Component\Console\Application;
1011

1112
if (PHP_SAPI !== 'cli' || (PHP_MAJOR_VERSION < 8 && PHP_MINOR_VERSION < 2)) {
@@ -44,6 +45,7 @@ $application = new Application('The Dragon Code: Styler', '6.x');
4445

4546
$application->add(new EditorConfigCommand);
4647
$application->add(new PintCommand);
48+
$application->add(new RectorCommand);
4749
$application->add(new NpmCommand);
4850

4951
$application->run();

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"require": {
4444
"php": "^8.2",
4545
"ext-json": "*",
46+
"driftingly/rector-laravel": "^2.1",
4647
"ergebnis/composer-normalize": "^2.48",
4748
"laravel/pint": "^1.24",
4849
"symfony/console": "^7.3"
@@ -74,6 +75,9 @@
7475
"php bin/codestyle pint 8.2",
7576
"composer normalize"
7677
],
77-
"style": "vendor/bin/pint ./bin/codestyle ./src/ ./tests"
78+
"style": [
79+
"vendor/bin/rector",
80+
"vendor/bin/pint ./bin/codestyle ./src/ ./tests"
81+
]
7882
}
7983
}

presets/rector/default.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;
7+
8+
$paths = static fn (array $paths): array => array_filter($paths, fn (string $path): bool => realpath($path) !== false);
9+
10+
return RectorConfig::configure()
11+
->withPaths(
12+
$paths([
13+
'app',
14+
'config',
15+
'database',
16+
'public/index.php',
17+
'resources',
18+
'src',
19+
'tests',
20+
])
21+
)
22+
->withFileExtensions(['php'])
23+
->withParallel()
24+
->withPreparedSets(
25+
deadCode : true,
26+
typeDeclarations: true,
27+
)
28+
->withPhpSets()
29+
->withImportNames(
30+
removeUnusedImports: true,
31+
)
32+
->withComposerBased(
33+
phpunit: true
34+
)
35+
->withAttributesSets(
36+
phpunit: true,
37+
)
38+
->withConfiguredRule(RemoveDumpDataDeadCodeRector::class, [
39+
'dd',
40+
'dump',
41+
'var_dump',
42+
'print_r',
43+
'echo',
44+
]);

presets/rector/laravel.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\Class_\RemoveModelPropertyFromFactoriesRector;
7+
use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;
8+
use RectorLaravel\Rector\FuncCall\TypeHintTappableCallRector;
9+
use RectorLaravel\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector;
10+
use RectorLaravel\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector;
11+
use RectorLaravel\Rector\MethodCall\UseComponentPropertyWithinCommandsRector;
12+
use RectorLaravel\Rector\MethodCall\WhereToWhereLikeRector;
13+
use RectorLaravel\Set\LaravelSetList;
14+
use RectorLaravel\Set\LaravelSetProvider;
15+
16+
$paths = static fn (array $paths): array => array_filter($paths, fn (string $path): bool => realpath($path) !== false);
17+
18+
return RectorConfig::configure()
19+
->withPaths(
20+
$paths([
21+
'app',
22+
'bin',
23+
'bootstrap',
24+
'config',
25+
'database',
26+
'lang',
27+
'operations',
28+
'public/index.php',
29+
'resources',
30+
'routes',
31+
'src',
32+
'tests',
33+
])
34+
)
35+
->withSkip(
36+
$paths(['bootstrap/cache'])
37+
)
38+
->withFileExtensions(['php'])
39+
->withParallel()
40+
->withPreparedSets(
41+
deadCode : true,
42+
typeDeclarations: true,
43+
)
44+
->withPhpSets()
45+
->withSetProviders(LaravelSetProvider::class)
46+
->withImportNames(
47+
removeUnusedImports: true,
48+
)
49+
->withComposerBased(
50+
phpunit: true,
51+
laravel: true
52+
)
53+
->withAttributesSets(
54+
phpunit: true,
55+
)
56+
->withSets([
57+
LaravelSetList::LARAVEL_COLLECTION,
58+
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES,
59+
LaravelSetList::LARAVEL_TESTING,
60+
])
61+
->withConfiguredRule(RemoveDumpDataDeadCodeRector::class, [
62+
'dd',
63+
'dump',
64+
'var_dump',
65+
'print_r',
66+
'echo',
67+
])
68+
->withConfiguredRule(WhereToWhereLikeRector::class, [
69+
WhereToWhereLikeRector::USING_POSTGRES_DRIVER => true,
70+
])
71+
->withRules([
72+
RemoveModelPropertyFromFactoriesRector::class,
73+
UseComponentPropertyWithinCommandsRector::class,
74+
TypeHintTappableCallRector::class,
75+
EloquentWhereRelationTypeHintingParameterRector::class,
76+
EloquentWhereTypeHintClosureParameterRector::class,
77+
]);

rector.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+
5+
use Rector\Configuration\RectorConfigBuilder;
6+
7+
/** @var RectorConfigBuilder $config */
8+
$config = require __DIR__ . '/presets/rector/laravel.php';
9+
10+
return $config->withPaths([
11+
'bin',
12+
'presets',
13+
'src',
14+
'rector.php',
15+
]);

src/Console/RectorCommand.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Codestyler\Console;
6+
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
class RectorCommand extends Command
14+
{
15+
protected function configure(): Command
16+
{
17+
return $this
18+
->setName('rector')
19+
->setDescription('Publishes presets for the Rector')
20+
->addArgument('preset', InputArgument::REQUIRED, 'The name of the preset')
21+
->addOption('path', 'p', InputOption::VALUE_OPTIONAL, 'Path to publish files', realpath('.'));
22+
}
23+
24+
protected function execute(InputInterface $input, OutputInterface $output): int
25+
{
26+
$version = $input->getArgument('preset');
27+
$path = $input->getOption('path');
28+
29+
if (! $this->validateVersion($version)) {
30+
$output->writeln("<error>Preset \"$version\" not found for Rector.</error>");
31+
32+
return static::FAILURE;
33+
}
34+
35+
if (! $this->validateDirectory($path)) {
36+
$output->writeln("<error>Directory \"$path\" not found.</error>");
37+
38+
return static::FAILURE;
39+
}
40+
41+
copy($this->presetPath($version), $path . '/rector.php');
42+
43+
$output->writeln("<info>Preset \"$version\" published successfully.</info>");
44+
45+
return static::SUCCESS;
46+
}
47+
48+
protected function validateVersion(string $version): bool
49+
{
50+
return file_exists(
51+
$this->presetPath($version)
52+
);
53+
}
54+
55+
protected function validateDirectory(string $path): bool
56+
{
57+
return file_exists($path) && is_dir($path);
58+
}
59+
60+
protected function presetPath(string $version): string
61+
{
62+
return __DIR__ . "/../../presets/rector/$version.php";
63+
}
64+
}

0 commit comments

Comments
 (0)