From a7f4fbe68b63a8428c4b6d071c30d26a027bd41f Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Thu, 19 Sep 2024 16:59:23 +0200 Subject: [PATCH] Added rector and basic rules --- .ddev/commands/web/rector | 1 - .gitattributes | 2 + .github/labeler.yml | 15 +++++--- .github/workflows/check-files.yml | 1 + .github/workflows/rector.yml | 37 +++++++++++++++++++ .github/workflows/workflow.yml | 8 ++++ composer.json | 9 ++++- composer.lock | 61 ++++++++++++++++++++++++++++++- rector.php | 42 +++++++++++++++++++++ 9 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/rector.yml create mode 100644 rector.php diff --git a/.ddev/commands/web/rector b/.ddev/commands/web/rector index 05ec4689d58..1189ce12807 100755 --- a/.ddev/commands/web/rector +++ b/.ddev/commands/web/rector @@ -4,5 +4,4 @@ ## Usage: rector ## Example: ddev rector -cp -n vendor/sreichel/openmage-rector/rector.php rector.php php vendor/bin/rector process "$@" diff --git a/.gitattributes b/.gitattributes index 33a3b1e2c65..e000b8e4c36 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,6 +3,7 @@ /.github export-ignore /dev export-ignore /docs export-ignore +/tests export-ignore /.all-contributorsrc export-ignore /.gitattributes export-ignore @@ -16,6 +17,7 @@ /.phpmd.dist.xml export-ignore /.phpstan.dist.baseline.neon export-ignore /.phpstan.dist.neon export-ignore +/rector.php export-ignore /README.md export-ignore diff --git a/.github/labeler.yml b/.github/labeler.yml index a1b1d9fad16..876fc5fcf0b 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -895,11 +895,9 @@ 'phpunit': - changed-files: - any-glob-to-any-file: [ - dev/test/*, - dev/phpunit*, - dev/sonar*, - .github/workflows/phpunit.yml, - .github/workflows/sonar.yml + phpunit*, + tests/*, + .github/workflows/phpunit.yml ] 'ddev': @@ -908,3 +906,10 @@ .ddev/*, .ddev/**/* ] + +'rector': + - changed-files: + - any-glob-to-any-file: [ + rector.php, + .github/workflows/rector.yml + ] \ No newline at end of file diff --git a/.github/workflows/check-files.yml b/.github/workflows/check-files.yml index a83d663a780..4478b2f810e 100644 --- a/.github/workflows/check-files.yml +++ b/.github/workflows/check-files.yml @@ -83,6 +83,7 @@ jobs: **phpcs** **php-cs-fixer** **phpstan** + rector.php tests/ phpunit* diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml new file mode 100644 index 00000000000..9314962d057 --- /dev/null +++ b/.github/workflows/rector.yml @@ -0,0 +1,37 @@ +name: Rector + +on: + workflow_call: + # Allow manually triggering the workflow. + workflow_dispatch: + +jobs: + rector: + name: Validation + runs-on: [ubuntu-latest] + + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 7.4 + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --ignore-platform-req=ext-* + + - name: Rector + run: php vendor/bin/rector process --dry-run diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 669207a35ec..4ac1ddb8806 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -106,6 +106,14 @@ jobs: if: needs.check.outputs.xml > 0 uses: ./.github/workflows/syntax-xml.yml + rector: + name: Rector + needs: [check, phpcs, php-cs-fixer] + if: | + needs.check.outputs.php > 0 || + needs.check.outputs.workflow > 0 + uses: ./.github/workflows/rector.yml + # DOES NOT run by default # runs on schedule or when workflow or unit tests changed unit_tests: diff --git a/composer.json b/composer.json index b6e834d82e3..4a17a660bd4 100644 --- a/composer.json +++ b/composer.json @@ -47,6 +47,7 @@ "phpmd/phpmd": "^2.13", "phpstan/phpstan": "^1.12.1", "phpunit/phpunit": "^9.5", + "rector/rector": "^1.2", "squizlabs/php_codesniffer": "^3.7", "symplify/vendor-patches": "^11.1" }, @@ -134,7 +135,9 @@ "phpstan": "vendor/bin/phpstan analyze", "phpunit:test": "XDEBUG_MODE=off php vendor/bin/phpunit --no-coverage --testdox", "phpunit:coverage": "XDEBUG_MODE=coverage php vendor/bin/phpunit --testdox", - "phpunit:coverage-local": "XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html build/coverage --testdox" + "phpunit:coverage-local": "XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html build/coverage --testdox", + "rector:test": "vendor/bin/rector process --dry-run", + "rector:fix": "vendor/bin/rector process" }, "scripts-descriptions": { "php-cs-fixer:test": "Run php-cs-fixer", @@ -142,6 +145,8 @@ "phpstan": "Run phpstan", "phpunit:test": "Run PHPUnit", "phpunit:coverage": "Run PHPUnit with code coverage", - "phpunit:coverage-local": "Run PHPUnit with local HTML code coverage" + "phpunit:coverage-local": "Run PHPUnit with local HTML code coverage", + "rector:test": "Run rector", + "rector:fix": "Run rector and fix issues" } } diff --git a/composer.lock b/composer.lock index fbcf00a91db..ccc9b1b7a0f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9a0f70859a6272156d00c0b32fc48cff", + "content-hash": "e922be8b6a32ba8038db76d87c2d7bd2", "packages": [ { "name": "colinmollenhour/cache-backend-redis", @@ -4594,6 +4594,65 @@ ], "time": "2024-06-11T12:45:25+00:00" }, + { + "name": "rector/rector", + "version": "1.2.5", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "e98aa793ca3fcd17e893cfaf9103ac049775d339" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/e98aa793ca3fcd17e893cfaf9103ac049775d339", + "reference": "e98aa793ca3fcd17e893cfaf9103ac049775d339", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0", + "phpstan/phpstan": "^1.12.2" + }, + "conflict": { + "rector/rector-doctrine": "*", + "rector/rector-downgrade-php": "*", + "rector/rector-phpunit": "*", + "rector/rector-symfony": "*" + }, + "suggest": { + "ext-dom": "To manipulate phpunit.xml via the custom-rule command" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Instant Upgrade and Automated Refactoring of any PHP code", + "keywords": [ + "automation", + "dev", + "migration", + "refactoring" + ], + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/1.2.5" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2024-09-08T17:43:24+00:00" + }, { "name": "sebastian/cli-parser", "version": "1.0.2", diff --git a/rector.php b/rector.php new file mode 100644 index 00000000000..4e6ea2466e7 --- /dev/null +++ b/rector.php @@ -0,0 +1,42 @@ +withPaths([ + __DIR__ . '/app', + __DIR__ . '/dev', + __DIR__ . '/errors', + __DIR__ . '/lib', + __DIR__ . '/pub', + __DIR__ . '/shell', + __DIR__ . '/tests', + ]) + ->withSkipPath(__DIR__ . '/vendor') + ->withSkip([ + __DIR__ . '/shell/translations.php', + __DIR__ . '/shell/update-copyright.php.php' + ]) + ->withRules([ +// CodeQuality\BooleanNot\SimplifyDeMorganBinaryRector::class, # wait for https://github.com/rectorphp/rector/issues/8781 + CodeQuality\BooleanNot\ReplaceMultipleBooleanNotRector::class, + CodeQuality\Foreach_\UnusedForeachValueToArrayKeysRector::class, + CodeQuality\FuncCall\ChangeArrayPushToArrayAssignRector::class, + CodeQuality\FuncCall\CompactToVariablesRector::class, + CodeQuality\Identical\SimplifyArraySearchRector::class, + CodeQuality\Identical\SimplifyConditionsRector::class, + CodeQuality\Identical\StrlenZeroToIdenticalEmptyStringRector::class, +// CodeQuality\If_\SimplifyIfReturnBoolRector::class, + CodeQuality\NotEqual\CommonNotEqualRector::class, + CodeQuality\LogicalAnd\LogicalToBooleanRector::class, + CodeQuality\Ternary\SimplifyTautologyTernaryRector::class, + DeadCode\ClassMethod\RemoveUselessParamTagRector::class, + DeadCode\ClassMethod\RemoveUselessReturnTagRector::class, + DeadCode\Property\RemoveUselessVarTagRector::class, + TypeDeclaration\ClassMethod\ReturnNeverTypeRector::class, + ]); \ No newline at end of file