diff --git a/.github/workflows/test-rector.yml b/.github/workflows/test-rector.yml new file mode 100644 index 0000000..8eb4483 --- /dev/null +++ b/.github/workflows/test-rector.yml @@ -0,0 +1,78 @@ +# When a PR is opened or a push is made, perform +# a static analysis check on the code using Rector. +name: Rector + +on: + pull_request: + paths: + - 'app/**.php' + - 'tests/**.php' + - '.github/workflows/test-rector.yml' + - composer.json + - rector.php + - '**.neon.dist' + + push: + paths: + - 'app/**.php' + - 'tests/**.php' + - '.github/workflows/test-rector.yml' + - composer.json + - rector.php + - '**.neon.dist' + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + build: + name: PHP ${{ matrix.php-versions }} Analyze code (Rector) + runs-on: ubuntu-22.04 + strategy: + fail-fast: false + matrix: + php-versions: ['8.1', '8.2', '8.3'] + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: intl + + - name: Use latest Composer + run: composer self-update + + - name: Validate composer.json + run: composer validate --strict + + - name: Get composer cache directory + run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> $GITHUB_ENV + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: ${{ env.COMPOSER_CACHE_FILES_DIR }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer update --ansi --no-interaction + + - name: Rector Cache + uses: actions/cache@v4 + with: + path: /tmp/rector + key: ${{ runner.os }}-rector-${{ github.run_id }} + restore-keys: ${{ runner.os }}-rector- + + - run: mkdir -p /tmp/rector + + - name: Run static analysis + run: vendor/bin/rector process --dry-run --no-progress-bar diff --git a/composer.json b/composer.json index c7b0232..d3511b0 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require-dev": { "fakerphp/faker": "^1.9", "mikey179/vfsstream": "^1.6", + "phpdevsr/rector-codeigniter4": "dev-main", "phpunit/phpunit": "^9.1" }, "autoload": { diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..cfc7a7f --- /dev/null +++ b/rector.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +use PHPDevsr\Rector\Codeigniter4\Set\CodeigniterSetList; +use Rector\Caching\ValueObject\Storage\FileCacheStorage; +use Rector\Config\RectorConfig; +use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector; +use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector; +use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector; +use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector; +use Rector\Php80\Rector\Class_\AnnotationToAttributeRector; + +return RectorConfig::configure() + ->withSets([ + CodeigniterSetList::CODEIGNITER_44, + ]) + // auto import fully qualified class names + ->withImportNames(removeUnusedImports: true) + // The paths to refactor (can also be supplied with CLI arguments) + ->withPaths([ + __DIR__ . '/app', + __DIR__ . '/tests', + ]) + ->withParallel(120, 8, 10) + ->withCache('/tmp/rector', FileCacheStorage::class) + // Include Composer's autoload - required for global execution, remove if running locally + ->withAutoloadPaths([ + __DIR__ . '/vendor/autoload.php', + ]) + // Do you need to include constants, class aliases, or a custom autoloader? + ->withBootstrapFiles([ + realpath(getcwd()) . '/vendor/codeigniter4/framework/system/Test/bootstrap.php', + ]) + // Are there files or rules you need to skip? + ->withSkip([ + __DIR__ . '/app/Views', + + JsonThrowOnErrorRector::class, + StringifyStrNeedlesRector::class, + + // Note: requires php 8 + RemoveUnusedPromotedPropertyRector::class, + + // May load view files directly when detecting classes + StringClassNameToClassConstantRector::class, + + AnnotationToAttributeRector::class, + ]);