Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You can install the package using [Composer](https://getcomposer.org):
```bash
composer require dragon-code/codestyler --dev

composer config scripts.style "vendor/bin/pint --parallel"
composer config scripts.style "vendor/bin/rector && vendor/bin/pint --parallel"
```

It is also possible to establish dependence in the global area of visibility:
Expand All @@ -44,6 +44,7 @@ and `biome.json` file for [Biome Linter](https://biomejs.dev):
"scripts": {
"post-update-cmd": [
"vendor/bin/codestyle pint 8.4",
"vendor/bin/codestyle rector laravel",
"vendor/bin/codestyle editorconfig",
"vendor/bin/codestyle npm",
"composer normalize"
Expand All @@ -59,6 +60,7 @@ When using a globally established dependence, the call must be replaced with the
"scripts": {
"post-update-cmd": [
"codestyle pint 8.4",
"codestyle rector laravel",
"codestyle editorconfig",
"codestyle npm",
"composer normalize"
Expand Down Expand Up @@ -100,6 +102,35 @@ The linter is invoked by a console command:
composer style
```

### Rector

[`Rector`](https://getrector.com) is uses as the code rector for PHP.

The Rector is invoked by a console command:

```bash
composer style
```

To do this, make sure the file is in the root of the project.
You can also automate this process by adding a call to the file copy function in the `scripts.post-update-cmd`
section of the `composer.json` file.

```JSON
{
"scripts": {
"post-update-cmd": [
"vendor/bin/codestyle rector laravel"
]
}
}
```

Available presets:

- `laravel`
- `default`

### Node Linter

[Biome](https://biomejs.dev) is used as the linter for JS, CSS and JSON.
Expand Down Expand Up @@ -183,11 +214,15 @@ After completing all the steps, the `composer.json` file will have the following
"scripts": {
"post-update-cmd": [
"vendor/bin/codestyle pint 8.4",
"vendor/bin/codestyle rector laravel",
"vendor/bin/codestyle editorconfig",
"vendor/bin/codestyle npm",
"composer normalize"
],
"style": "vendor/bin/pint --parallel"
"style": [
"vendor/bin/pint --parallel",
"vendor/bin/rector"
]
}
}
```
Expand Down
2 changes: 2 additions & 0 deletions bin/codestyle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare(strict_types=1);
use DragonCode\Codestyler\Console\EditorConfigCommand;
use DragonCode\Codestyler\Console\NpmCommand;
use DragonCode\Codestyler\Console\PintCommand;
use DragonCode\Codestyler\Console\RectorCommand;
use Symfony\Component\Console\Application;

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

$application->add(new EditorConfigCommand);
$application->add(new PintCommand);
$application->add(new RectorCommand);
$application->add(new NpmCommand);

$application->run();
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"require": {
"php": "^8.2",
"ext-json": "*",
"driftingly/rector-laravel": "^2.1",
"ergebnis/composer-normalize": "^2.48",
"laravel/pint": "^1.24",
"symfony/console": "^7.3"
Expand Down Expand Up @@ -74,6 +75,9 @@
"php bin/codestyle pint 8.2",
"composer normalize"
],
"style": "vendor/bin/pint ./bin/codestyle ./src/ ./tests"
"style": [
"vendor/bin/rector",
"vendor/bin/pint ./bin/codestyle ./src/ ./tests"
]
}
}
44 changes: 44 additions & 0 deletions presets/rector/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;

$paths = static fn (array $paths): array => array_filter($paths, fn (string $path): bool => realpath($path) !== false);

return RectorConfig::configure()
->withPaths(
$paths([
'app',
'config',
'database',
'public/index.php',
'resources',
'src',
'tests',
])
)
->withFileExtensions(['php'])
->withParallel()
->withPreparedSets(
deadCode : true,
typeDeclarations: true,
)
->withPhpSets()
->withImportNames(
removeUnusedImports: true,
)
->withComposerBased(
phpunit: true
)
->withAttributesSets(
phpunit: true,
)
->withConfiguredRule(RemoveDumpDataDeadCodeRector::class, [
'dd',
'dump',
'var_dump',
'print_r',
'echo',
]);
77 changes: 77 additions & 0 deletions presets/rector/laravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\Class_\RemoveModelPropertyFromFactoriesRector;
use RectorLaravel\Rector\FuncCall\RemoveDumpDataDeadCodeRector;
use RectorLaravel\Rector\FuncCall\TypeHintTappableCallRector;
use RectorLaravel\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector;
use RectorLaravel\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector;
use RectorLaravel\Rector\MethodCall\UseComponentPropertyWithinCommandsRector;
use RectorLaravel\Rector\MethodCall\WhereToWhereLikeRector;
use RectorLaravel\Set\LaravelSetList;
use RectorLaravel\Set\LaravelSetProvider;

$paths = static fn (array $paths): array => array_filter($paths, fn (string $path): bool => realpath($path) !== false);

return RectorConfig::configure()
->withPaths(
$paths([
'app',
'bin',
'bootstrap',
'config',
'database',
'lang',
'operations',
'public/index.php',
'resources',
'routes',
'src',
'tests',
])
)
->withSkip(
$paths(['bootstrap/cache'])
)
->withFileExtensions(['php'])
->withParallel()
->withPreparedSets(
deadCode : true,
typeDeclarations: true,
)
->withPhpSets()
->withSetProviders(LaravelSetProvider::class)
->withImportNames(
removeUnusedImports: true,
)
->withComposerBased(
phpunit: true,
laravel: true
)
->withAttributesSets(
phpunit: true,
)
->withSets([
LaravelSetList::LARAVEL_COLLECTION,
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES,
LaravelSetList::LARAVEL_TESTING,
])
->withConfiguredRule(RemoveDumpDataDeadCodeRector::class, [
'dd',
'dump',
'var_dump',
'print_r',
'echo',
])
->withConfiguredRule(WhereToWhereLikeRector::class, [
WhereToWhereLikeRector::USING_POSTGRES_DRIVER => true,
])
->withRules([
RemoveModelPropertyFromFactoriesRector::class,
UseComponentPropertyWithinCommandsRector::class,
TypeHintTappableCallRector::class,
EloquentWhereRelationTypeHintingParameterRector::class,
EloquentWhereTypeHintClosureParameterRector::class,
]);
15 changes: 15 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\Configuration\RectorConfigBuilder;

/** @var RectorConfigBuilder $config */
$config = require __DIR__ . '/presets/rector/laravel.php';

return $config->withPaths([
'bin',
'presets',
'src',
'rector.php',
]);
64 changes: 64 additions & 0 deletions src/Console/RectorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace DragonCode\Codestyler\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RectorCommand extends Command
{
protected function configure(): Command
{
return $this
->setName('rector')
->setDescription('Publishes presets for the Rector')
->addArgument('preset', InputArgument::REQUIRED, 'The name of the preset')
->addOption('path', 'p', InputOption::VALUE_OPTIONAL, 'Path to publish files', realpath('.'));
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$version = $input->getArgument('preset');
$path = $input->getOption('path');

if (! $this->validateVersion($version)) {
$output->writeln("<error>Preset \"$version\" not found for Rector.</error>");

return static::FAILURE;
}

if (! $this->validateDirectory($path)) {
$output->writeln("<error>Directory \"$path\" not found.</error>");

return static::FAILURE;
}

copy($this->presetPath($version), $path . '/rector.php');

$output->writeln("<info>Preset \"$version\" published successfully.</info>");

return static::SUCCESS;
}

protected function validateVersion(string $version): bool
{
return file_exists(
$this->presetPath($version)
);
}

protected function validateDirectory(string $path): bool
{
return file_exists($path) && is_dir($path);
}

protected function presetPath(string $version): string
{
return __DIR__ . "/../../presets/rector/$version.php";
}
}