diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index a22f59c2..54a02483 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -75,3 +75,20 @@ jobs: - name: Run ESLint run: npm run eslint -- eslint/rules + rector: + runs-on: ubuntu-latest + name: Rector tests + + steps: + - name: Setup node + uses: actions/setup-node@v2 + with: + node-version: '16' + + - uses: actions/checkout@v2 + + - name: Install dependencies + run: composer install + + - name: Run rector + run: vendor/bin/rector process Magento2 Magento2Framework PHP_CodeSniffer --dry-run --autoload-file vendor/squizlabs/php_codesniffer/autoload.php diff --git a/README.md b/README.md index be9937d0..3cfd6c0a 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,25 @@ You can execute ESLint as follows: npm run eslint -- path/to/analyze ``` +### RECTOR PHP +From `magento-condign-standard` project, you can execute rector php as follows: +```bash +vendor/bin/rector process Magento2 Magento2Framework PHP_CodeSniffer --dry-run --autoload-file vendor/squizlabs/php_codesniffer/autoload.php +``` +The rules from rector that are applied are set inside the config file: `rector.php` + +The option `--dry-run` displays errors found, but code is not automatically fixed. + +To run rector for `magento` projects you need to: +- Specify the magento path and the autoload file for the magento project: +```bash +vendor/bin/rector process MAGENTO_PATH --dry-run --autoload-file MAGENTO_AUTOLOAD_FILE +``` +Example: +```bash +vendor/bin/rector process magento2ce/app/code/Magento/Cms/Model --dry-run --autoload-file magento2ce/vendor/autoload.php +``` + ## License Each Magento source file included in this distribution is licensed under the OSL-3.0 license. diff --git a/composer.json b/composer.json index 3ad22f70..9b59a792 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "ext-simplexml": "*", "ext-dom": "*", "phpcompatibility/php-compatibility": "^9.3", - "squizlabs/php_codesniffer": "^3.6" + "squizlabs/php_codesniffer": "^3.6", + "rector/rector": "^0.12.4" }, "require-dev": { "phpunit/phpunit": "^9.5.8" diff --git a/composer.lock b/composer.lock index 47559dbd..0e07e918 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": "2e8abe33e655572c04bb6cdc53f88626", + "content-hash": "61cf9451f97627aeb7908e83b51a1d82", "packages": [ { "name": "phpcompatibility/php-compatibility", @@ -68,6 +68,130 @@ }, "time": "2019-12-27T09:44:58+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "cbe085f9fdead5b6d62e4c022ca52dc9427a10ee" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/cbe085f9fdead5b6d62e4c022ca52dc9427a10ee", + "reference": "cbe085f9fdead5b6d62e4c022ca52dc9427a10ee", + "shasum": "" + }, + "require": { + "php": "^7.1|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "support": { + "issues": "https://github.com/phpstan/phpstan/issues", + "source": "https://github.com/phpstan/phpstan/tree/1.2.0" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://www.patreon.com/phpstan", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2021-11-18T14:09:01+00:00" + }, + { + "name": "rector/rector", + "version": "0.12.4", + "source": { + "type": "git", + "url": "https://github.com/rectorphp/rector.git", + "reference": "d7d4659acae85a9ba5b8b75d4dadb998ffed2a48" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rectorphp/rector/zipball/d7d4659acae85a9ba5b8b75d4dadb998ffed2a48", + "reference": "d7d4659acae85a9ba5b8b75d4dadb998ffed2a48", + "shasum": "" + }, + "require": { + "php": "^7.1|^8.0", + "phpstan/phpstan": "^1.1.1" + }, + "conflict": { + "phpstan/phpdoc-parser": "<1.2", + "rector/rector-cakephp": "*", + "rector/rector-doctrine": "*", + "rector/rector-laravel": "*", + "rector/rector-nette": "*", + "rector/rector-phpoffice": "*", + "rector/rector-phpunit": "*", + "rector/rector-prefixed": "*", + "rector/rector-symfony": "*" + }, + "bin": [ + "bin/rector" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "0.12-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Prefixed and PHP 7.1 downgraded version of rector/rector", + "support": { + "issues": "https://github.com/rectorphp/rector/issues", + "source": "https://github.com/rectorphp/rector/tree/0.12.4" + }, + "funding": [ + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2021-11-15T14:06:50+00:00" + }, { "name": "squizlabs/php_codesniffer", "version": "3.6.1", diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..502f7744 --- /dev/null +++ b/rector.php @@ -0,0 +1,30 @@ +parameters(); + + $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80); + $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_81); + + // get services (needed for register a single rule) + $services = $containerConfigurator->services(); + + // register a single rule + $services->set(FinalPrivateToPrivateVisibilityRector::class); + $services->set(OptionalParametersAfterRequiredRector::class); + $services->set(SetStateToStaticRector::class); + $services->set(StringableForToStringRector::class); + $services->set(Php81ResourceReturnToObjectRector::class); +};