Skip to content

Commit 901c1eb

Browse files
Merge pull request #127 from magento-commerce/AC-1740
AC-1740: Add rectorphp as a dependency to magento-coding-standard and run it as part of the GitHub actions
2 parents c14c326 + 856d8ca commit 901c1eb

File tree

5 files changed

+193
-2
lines changed

5 files changed

+193
-2
lines changed

.github/workflows/php.yml

+17
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,20 @@ jobs:
7575

7676
- name: Run ESLint
7777
run: npm run eslint -- eslint/rules
78+
rector:
79+
runs-on: ubuntu-latest
80+
name: Rector tests
81+
82+
steps:
83+
- name: Setup node
84+
uses: actions/setup-node@v2
85+
with:
86+
node-version: '16'
87+
88+
- uses: actions/checkout@v2
89+
90+
- name: Install dependencies
91+
run: composer install
92+
93+
- name: Run rector
94+
run: vendor/bin/rector process Magento2 Magento2Framework PHP_CodeSniffer --dry-run --autoload-file vendor/squizlabs/php_codesniffer/autoload.php

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,25 @@ You can execute ESLint as follows:
112112
npm run eslint -- path/to/analyze
113113
```
114114

115+
### RECTOR PHP
116+
From `magento-condign-standard` project, you can execute rector php as follows:
117+
```bash
118+
vendor/bin/rector process Magento2 Magento2Framework PHP_CodeSniffer --dry-run --autoload-file vendor/squizlabs/php_codesniffer/autoload.php
119+
```
120+
The rules from rector that are applied are set inside the config file: `rector.php`
121+
122+
The option `--dry-run` displays errors found, but code is not automatically fixed.
123+
124+
To run rector for `magento` projects you need to:
125+
- Specify the magento path and the autoload file for the magento project:
126+
```bash
127+
vendor/bin/rector process MAGENTO_PATH --dry-run --autoload-file MAGENTO_AUTOLOAD_FILE
128+
```
129+
Example:
130+
```bash
131+
vendor/bin/rector process magento2ce/app/code/Magento/Cms/Model --dry-run --autoload-file magento2ce/vendor/autoload.php
132+
```
133+
115134
## License
116135

117136
Each Magento source file included in this distribution is licensed under the OSL-3.0 license.

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"ext-simplexml": "*",
1414
"ext-dom": "*",
1515
"phpcompatibility/php-compatibility": "^9.3",
16-
"squizlabs/php_codesniffer": "^3.6"
16+
"squizlabs/php_codesniffer": "^3.6",
17+
"rector/rector": "^0.12.4"
1718
},
1819
"require-dev": {
1920
"phpunit/phpunit": "^9.5.8"

composer.lock

+125-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rector.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Core\Configuration\Option;
6+
use Rector\Core\ValueObject\PhpVersion;
7+
use Rector\Php80\Rector\Class_\StringableForToStringRector;
8+
use Rector\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector;
9+
use Rector\Php80\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
10+
use Rector\Php80\Rector\ClassMethod\SetStateToStaticRector;
11+
use Rector\Php81\Rector\FuncCall\Php81ResourceReturnToObjectRector;
12+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
13+
14+
return static function (ContainerConfigurator $containerConfigurator): void {
15+
// get parameters
16+
$parameters = $containerConfigurator->parameters();
17+
18+
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80);
19+
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_81);
20+
21+
// get services (needed for register a single rule)
22+
$services = $containerConfigurator->services();
23+
24+
// register a single rule
25+
$services->set(FinalPrivateToPrivateVisibilityRector::class);
26+
$services->set(OptionalParametersAfterRequiredRector::class);
27+
$services->set(SetStateToStaticRector::class);
28+
$services->set(StringableForToStringRector::class);
29+
$services->set(Php81ResourceReturnToObjectRector::class);
30+
};

0 commit comments

Comments
 (0)