Skip to content

Commit

Permalink
Add Rector for code quality enhancements (#26)
Browse files Browse the repository at this point in the history
A new PHP file, `rector.php`, has been introduced for using the Rector tool, which is set up to apply a variety of automated refactoring and code quality improvement rules. In addition, Rector has been added as a dependency in the `composer.json` file and included in the project's testing scripts and GitHub workflow. This tool helps in improving the codebase and maintaining a robust code quality standard.
  • Loading branch information
MarjovanLier authored Mar 5, 2024
1 parent 99587f9 commit e062884
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ jobs:
if: steps.phan.outcome == 'success'
run: composer test:psalm

# # This step runs Rector for code quality.
# - name: Run rector for code quality
# id: rector
# if: steps.psalm.outcome == 'success'
# run: composer test:rector
# This step runs Rector for code quality.
- name: Run rector for code quality
id: rector
if: steps.psalm.outcome == 'success'
run: composer test:rector

release:
needs: build
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"phpunit/php-code-coverage": "^10|^11",
"phpunit/phpunit": "^10|^11",
"psalm/plugin-phpunit": ">=0.18.4",
"rector/rector": ">=1.0.2",
"vimeo/psalm": ">=5.22.2"
},
"scripts": {
Expand All @@ -77,7 +78,8 @@
"@test:infection",
"@test:phpstan",
"@test:phan",
"@test:psalm"
"@test:psalm",
"@test:rector"
],
"test:code-style": "pint --test",
"test:infection": "infection --min-msi=100 --min-covered-msi=100 --threads=4 --show-mutations --only-covered --formatter=progress",
Expand All @@ -87,6 +89,7 @@
"test:phpstan": "php -d memory_limit=-1 ./vendor/bin/phpstan analyse --no-progress --no-interaction",
"test:phpunit": "phpunit --no-coverage --no-logging",
"test:psalm": "psalm --no-cache --no-progress --show-info=false",
"test:rector": "rector --dry-run",
"test:vulnerabilities-check": "php -d memory_limit=-1 ./vendor/bin/security-checker security:check"
},
"scripts-descriptions": {
Expand All @@ -98,6 +101,7 @@
"test:phpstan": "Conduct PHPStan static analysis for identifying code quality issues.",
"test:phpunit": "Execute PHPUnit tests to verify code functionality.",
"test:psalm": "Run Psalm to find errors and improve code quality.",
"test:rector": "Apply automated code quality enhancements with Rector.",
"test:vulnerabilities-check": "Scan dependencies for known security vulnerabilities."
}
}
3 changes: 1 addition & 2 deletions localTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ for version in {81..83}; do
rm -f composer.lock && \
$DOCKER_CMD composer install && \
$DOCKER_CMD composer update --with-all-dependencies && \
$DOCKER_CMD composer require --dev --with-all-dependencies "vimeo/psalm":">=5.22.2" && \
$DOCKER_CMD composer require --dev --with-all-dependencies "psalm/plugin-phpunit":">=0.18.4"
$DOCKER_CMD composer require --dev --with-all-dependencies "rector/rector":">=1.0.2"

# Check for errors immediately after Composer commands
if [ $? -ne 0 ]; then
Expand Down
71 changes: 71 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* @noinspection DevelopmentDependenciesUsageInspection

Check notice on line 4 in rector.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Unknown inspection suppression

\[EA\] Unknown inspection: .
*/

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector;
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\CodingStyle\Rector\Use_\SeparateMultiUseImportsRector;
use Rector\Config\RectorConfig;
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector;
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchMethodCallReturnTypeRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->bootstrapFiles([__DIR__ . '/vendor/autoload.php']);

$rectorConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
$rectorConfig->rule(RenameForeachValueVariableToMatchExprVariableRector::class);
$rectorConfig->rule(RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class);
$rectorConfig->rule(TypedPropertyFromStrictConstructorRector::class);
$rectorConfig->rule(NullableCompareToNullRector::class);
$rectorConfig->rule(EncapsedStringsToSprintfRector::class);
$rectorConfig->rule(NewlineAfterStatementRector::class);
$rectorConfig->rule(NewlineBeforeNewAssignSetRector::class);
$rectorConfig->rule(PostIncDecToPreIncDecRector::class);
$rectorConfig->rule(SeparateMultiUseImportsRector::class);
$rectorConfig->rule(SplitDoubleAssignRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_81);

// define sets of rules
$rectorConfig->sets(
[
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::EARLY_RETURN,
SetList::PHP_81,
SetList::TYPE_DECLARATION,
SetList::NAMING,
SetList::PRIVATIZATION,
SetList::STRICT_BOOLEANS,
SetList::INSTANCEOF,
]
);

$rectorConfig->importNames();
$rectorConfig->importShortClasses(false);

$rectorConfig->skip(
[
FlipTypeControlToUseExclusiveTypeRector::class,
]
);
};

0 comments on commit e062884

Please sign in to comment.