Skip to content

Commit 9a40e4b

Browse files
committed
ci: add rector
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
1 parent 8a59655 commit 9a40e4b

File tree

8 files changed

+177
-112
lines changed

8 files changed

+177
-112
lines changed

.github/workflows/__shared-ci.yml

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ jobs:
2424
php-version: ${{ matrix.php-versions }}
2525
extensions: none,iconv,dom,curl,mbstring,tokenizer,xml,xmlwriter,simplexml,ctype
2626
coverage: pcov
27-
27+
2828
- name: ♻️ Get composer cache directory
2929
id: composer-cache
3030
shell: bash
3131
run: echo "dir=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT"
32-
32+
3333
- name: ♻️ Cache composer dependencies
3434
uses: actions/cache@v4
3535
with:
3636
path: ${{ steps.composer-cache.outputs.dir }}
3737
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
3838
restore-keys: ${{ runner.os }}-composer-
39-
39+
4040
- name: ⚙️ Install dependencies
4141
shell: bash
4242
run: |
4343
composer install --no-progress --prefer-dist --optimize-autoloader
4444
composer --working-dir=tools install --no-progress --prefer-dist --optimize-autoloader
45-
45+
4646
- name: ♻️ Tools cache
4747
uses: actions/cache@v4
4848
with:
@@ -55,9 +55,47 @@ jobs:
5555
if: matrix.stable
5656
run: composer php-cs-fixer -- --format=checkstyle | tools/vendor/bin/cs2pr
5757

58+
- name: 🔬 Rector
59+
id: rector
60+
if: matrix.stable
61+
run: |
62+
REPORT=$(composer rector -- --output-format json || true)
63+
echo "report<<EOF" >> "$GITHUB_OUTPUT" && echo "$REPORT" >> "$GITHUB_OUTPUT" && echo "EOF" >> "$GITHUB_OUTPUT"
64+
65+
- uses: actions/github-script@v6
66+
if: matrix.stable
67+
with:
68+
script: |
69+
const report = ${{ steps.rector.outputs.report }}
70+
71+
console.log('::group::Rector report')
72+
73+
for(const fileDiff of report.file_diffs) {
74+
const message = fileDiff.diff.replace(/\n/g, '%0A');
75+
76+
const title = `Rector: ${fileDiff.applied_rectors.join(' / ')}`.trim();
77+
const file = fileDiff.file;
78+
79+
// Extract line start and end, and column start and end from fileDiff.diff (diff format)
80+
const diffMeta = fileDiff.diff.match(/@@ -(\d+),(\d+) \+(\d+),(\d+) @@/);
81+
if (!diffMeta) {
82+
core.error(`Could not parse diff for file: ${fileDiff.file}`);
83+
continue;
84+
}
85+
86+
const startLine = parseInt(diffMeta[1], 10);
87+
const endLine = startLine + parseInt(diffMeta[2], 10) - 1;
88+
89+
console.log(
90+
`::error file=${file},line=${startLine},endLine=${endLine},title=${title}::${message}`
91+
);
92+
}
93+
94+
console.log("::endgroup::");
95+
5896
- name: 🔬 Static analysis
5997
if: matrix.stable
60-
run: composer stan -- --error-format=checkstyle | tools/vendor/bin/cs2pr
98+
run: composer stan -- --error-format=github
6199

62100
- name: ♻️ Tests cache
63101
uses: actions/cache@v4
@@ -66,7 +104,7 @@ jobs:
66104
key: ${{ runner.os }}-tests-${{ github.sha }}
67105
restore-keys: |
68106
${{ runner.os }}-tests-
69-
107+
70108
- name: 🧪 Test
71109
run: composer test:ci
72110

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ lint: ## Execute lint for given PHP version
3434
@$(call run-php,composer php-cs-fixer $(filter-out $@,$(MAKECMDGOALS)))
3535

3636
lint-fix: ## Execute lint fixing for given PHP version
37-
@$(call run-php,composer php-cs-fixer:fix $(filter-out $@,$(MAKECMDGOALS)))
37+
@$(call run-php,composer php-cs-fixer:fix $(filter-out $@,$(MAKECMDGOALS)))
38+
39+
rector: ## Execute rector for given PHP version
40+
@$(call run-php,composer rector $(filter-out $@,$(MAKECMDGOALS)))
41+
42+
rector-fix: ## Execute rector fixing for given PHP version
43+
@$(call run-php,composer rector:fix $(filter-out $@,$(MAKECMDGOALS)))
3844

3945
stan: ## Execute PHPStan for given PHP version
4046
@$(call run-php,composer stan $(filter-out $@,$(MAKECMDGOALS)))

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@
4949
"test:ci": "@test -d pcov.enabled=1 -d max_execution_time=0 --coverage-text --coverage-clover ./build/logs/clover.xml --coverage-html ./build/coverage/",
5050
"php-cs-fixer": "@php-cs-fixer:fix --dry-run",
5151
"php-cs-fixer:fix": "tools/vendor/bin/php-cs-fixer fix --show-progress=dots --diff --config=.php-cs-fixer.dist.php",
52-
"stan": "tools/vendor/bin/phpstan analyse --level 5 src",
52+
"rector": "@rector:fix --dry-run",
53+
"rector:fix": "tools/vendor/bin/rector process src",
54+
"stan": "tools/vendor/bin/phpstan analyse --level max src",
5355
"ci": [
5456
"@php-cs-fixer",
57+
"@rector",
5558
"@stan",
5659
"@test:ci"
5760
]

rector.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
6+
use Rector\Config\RectorConfig;
7+
8+
return RectorConfig::configure()
9+
->withPaths([
10+
__DIR__ . '/src',
11+
__DIR__ . '/tests',
12+
])
13+
->withPhpSets()
14+
->withCache(
15+
cacheClass: FileCacheStorage::class,
16+
cacheDirectory: __DIR__ . '/tools/cache/rector'
17+
);;

src/CssLint/CliArgs.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ class CliArgs
99

1010
/**
1111
* Constructor
12-
* @param array $aArguments arguments to be parsed (@see $_SERVER['argv'])
12+
* @param array $arguments arguments to be parsed (@see $_SERVER['argv'])
1313
* Accepts "-o", "--options" '{}'
1414
* Accepts a string as last argument, a file path or a string containing CSS
1515
*/
16-
public function __construct(array $aArguments)
16+
public function __construct(array $arguments)
1717
{
18-
if (empty($aArguments) || count($aArguments) === 1) {
18+
if (empty($arguments) || count($arguments) === 1) {
1919
return;
2020
}
2121

22-
array_shift($aArguments);
22+
array_shift($arguments);
2323

24-
$this->filePathOrCssString = array_pop($aArguments);
24+
$this->filePathOrCssString = array_pop($arguments);
2525

26-
if ($aArguments) {
27-
$aParsedArguments = $this->extractArguments($aArguments);
26+
if ($arguments) {
27+
$aParsedArguments = $this->extractArguments($arguments);
2828

2929
if (!empty($aParsedArguments['options'])) {
3030
$this->options = $aParsedArguments['options'];
@@ -33,14 +33,14 @@ public function __construct(array $aArguments)
3333
}
3434

3535
/**
36-
* @param array $aArguments array of arguments to be parsed (@see $_SERVER['argv'])
36+
* @param array $arguments array of arguments to be parsed (@see $_SERVER['argv'])
3737
* @return array an associative array of key=>value arguments
3838
*/
39-
private function extractArguments(array $aArguments): array
39+
private function extractArguments(array $arguments): array
4040
{
4141
$aParsedArguments = [];
4242

43-
foreach ($aArguments as $sArgument) {
43+
foreach ($arguments as $sArgument) {
4444
// --foo --bar=baz
4545
if (substr($sArgument, 0, 2) == '--') {
4646
$sEqualPosition = strpos($sArgument, '=');

0 commit comments

Comments
 (0)