Skip to content

Commit 8b3a2dd

Browse files
committed
ci: add rector
Signed-off-by: Emilien Escalle <[email protected]>
1 parent 8a59655 commit 8b3a2dd

File tree

9 files changed

+246
-214
lines changed

9 files changed

+246
-214
lines changed

.github/workflows/__shared-ci.yml

Lines changed: 11 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,14 @@ 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: composer rector
62+
5863
- name: 🔬 Static analysis
5964
if: matrix.stable
60-
run: composer stan -- --error-format=checkstyle | tools/vendor/bin/cs2pr
65+
run: composer phpstan -- --error-format=github
6166

6267
- name: ♻️ Tests cache
6368
uses: actions/cache@v4
@@ -66,7 +71,7 @@ jobs:
6671
key: ${{ runner.os }}-tests-${{ github.sha }}
6772
restore-keys: |
6873
${{ runner.os }}-tests-
69-
74+
7075
- name: 🧪 Test
7176
run: composer test:ci
7277

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ 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)))
3838

39-
stan: ## Execute PHPStan for given PHP version
40-
@$(call run-php,composer stan $(filter-out $@,$(MAKECMDGOALS)))
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)))
44+
45+
phpstan: ## Execute PHPStan for given PHP version
46+
@$(call run-php,composer phpstan $(filter-out $@,$(MAKECMDGOALS)))
4147

4248
ci: ## Execute CI scripts for given PHP version
4349
@$(call run-php,composer ci $(filter-out $@,$(MAKECMDGOALS)))

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@
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+
"phpstan": "tools/vendor/bin/phpstan analyse --level max src",
5355
"ci": [
5456
"@php-cs-fixer",
55-
"@stan",
57+
"@rector",
58+
"@phpstan",
5659
"@test:ci"
5760
]
5861
},

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/Cli.php

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,63 @@
44

55
class Cli
66
{
7-
private static $SCRIPT_NAME = 'php-css-lint';
8-
private static $RETURN_CODE_ERROR = 1;
9-
private static $RETURN_CODE_SUCCESS = 0;
7+
private const SCRIPT_NAME = 'php-css-lint';
8+
private const RETURN_CODE_ERROR = 1;
9+
private const RETURN_CODE_SUCCESS = 0;
1010

1111
/**
1212
* Entrypoint of the cli, will execute the linter according to the given arguments
13-
* @param array $aArguments arguments to be parsed (@see $_SERVER['argv'])
13+
* @param array $arguments arguments to be parsed (@see $_SERVER['argv'])
1414
* @return int the return code related to the execution of the linter
1515
**/
16-
public function run(array $aArguments): int
16+
public function run(array $arguments): int
1717
{
18-
$oCliArgs = $this->parseArguments($aArguments);
19-
if (!$oCliArgs->filePathOrCssString) {
18+
$cliArgs = $this->parseArguments($arguments);
19+
if (!$cliArgs->filePathOrCssString) {
2020
$this->printUsage();
21-
return self::$RETURN_CODE_SUCCESS;
21+
return self::RETURN_CODE_SUCCESS;
2222
}
2323

24-
$oProperties = new \CssLint\Properties();
25-
if ($oCliArgs->options) {
26-
$aOptions = json_decode($oCliArgs->options, true);
24+
$properties = new \CssLint\Properties();
25+
if ($cliArgs->options) {
26+
$options = json_decode($cliArgs->options, true);
2727

2828
if (json_last_error()) {
29-
$sErrorMessage = json_last_error_msg();
30-
$this->printError('Unable to parse option argument: ' . $sErrorMessage);
31-
return self::$RETURN_CODE_ERROR;
29+
$errorMessage = json_last_error_msg();
30+
$this->printError('Unable to parse option argument: ' . $errorMessage);
31+
return self::RETURN_CODE_ERROR;
3232
}
3333

34-
if (!$aOptions) {
34+
if (!$options) {
3535
$this->printError('Unable to parse empty option argument');
36-
return self::$RETURN_CODE_ERROR;
36+
return self::RETURN_CODE_ERROR;
3737
}
38-
$oProperties->setOptions($aOptions);
38+
$properties->setOptions($options);
3939
}
4040

41-
$oCssLinter = new \CssLint\Linter($oProperties);
41+
$cssLinter = new \CssLint\Linter($properties);
4242

43-
$sFilePathOrCssString = $oCliArgs->filePathOrCssString;
44-
if (!file_exists($sFilePathOrCssString)) {
45-
return $this->lintString($oCssLinter, $sFilePathOrCssString);
43+
$filePathOrCssString = $cliArgs->filePathOrCssString;
44+
if (!file_exists($filePathOrCssString)) {
45+
return $this->lintString($cssLinter, $filePathOrCssString);
4646
}
4747

48-
$sFilePath = $sFilePathOrCssString;
49-
if (!is_readable($sFilePath)) {
50-
$this->printError('File "' . $sFilePath . '" is not readable');
51-
return self::$RETURN_CODE_ERROR;
48+
$filePath = $filePathOrCssString;
49+
if (!is_readable($filePath)) {
50+
$this->printError('File "' . $filePath . '" is not readable');
51+
return self::RETURN_CODE_ERROR;
5252
}
5353

54-
return $this->lintFile($oCssLinter, $sFilePath);
54+
return $this->lintFile($cssLinter, $filePath);
5555
}
5656

5757
/**
5858
* Retrieve the parsed Cli arguments from given arguments array
5959
* @return \CssLint\CliArgs an instance of Cli arguments object containing parsed arguments
6060
*/
61-
private function parseArguments(array $aArguments): \CssLint\CliArgs
61+
private function parseArguments(array $arguments): \CssLint\CliArgs
6262
{
63-
return new \CssLint\CliArgs($aArguments);
63+
return new \CssLint\CliArgs($arguments);
6464
}
6565

6666
/**
@@ -71,7 +71,7 @@ private function printUsage()
7171
$this->printLine('Usage:' . PHP_EOL .
7272
'------' . PHP_EOL .
7373
PHP_EOL .
74-
' ' . self::$SCRIPT_NAME . ' [--options=\'{ }\'] css_file_or_string_to_lint' . PHP_EOL .
74+
' ' . self::SCRIPT_NAME . ' [--options=\'{ }\'] css_file_or_string_to_lint' . PHP_EOL .
7575
PHP_EOL .
7676
'Arguments:' . PHP_EOL .
7777
'----------' . PHP_EOL .
@@ -95,83 +95,83 @@ private function printUsage()
9595
'---------' . PHP_EOL .
9696
PHP_EOL .
9797
' Lint a CSS file:' . PHP_EOL .
98-
' ' . self::$SCRIPT_NAME . ' ./path/to/css_file_path_to_lint.css' . PHP_EOL . PHP_EOL .
98+
' ' . self::SCRIPT_NAME . ' ./path/to/css_file_path_to_lint.css' . PHP_EOL . PHP_EOL .
9999
' Lint a CSS string:' . PHP_EOL .
100-
' ' . self::$SCRIPT_NAME . ' ".test { color: red; }"' . PHP_EOL . PHP_EOL .
100+
' ' . self::SCRIPT_NAME . ' ".test { color: red; }"' . PHP_EOL . PHP_EOL .
101101
' Lint with only tabulation as indentation:' . PHP_EOL .
102-
' ' . self::$SCRIPT_NAME .
102+
' ' . self::SCRIPT_NAME .
103103
' --options=\'{ "allowedIndentationChars": ["\t"] }\' ".test { color: red; }"' . PHP_EOL .
104104
PHP_EOL . PHP_EOL);
105105
}
106106

107107
/**
108108
* Performs lint on a given file path
109-
* @param \CssLint\Linter $oCssLinter the instance of the linter
110-
* @param string $sFilePath the path of the file to be linted
109+
* @param \CssLint\Linter $cssLinter the instance of the linter
110+
* @param string $filePath the path of the file to be linted
111111
* @return int the return code related to the execution of the linter
112112
*/
113-
private function lintFile(\CssLint\Linter $oCssLinter, string $sFilePath): int
113+
private function lintFile(\CssLint\Linter $cssLinter, string $filePath): int
114114
{
115-
$this->printLine('# Lint CSS file "' . $sFilePath . '"...');
115+
$this->printLine('# Lint CSS file "' . $filePath . '"...');
116116

117-
if ($oCssLinter->lintFile($sFilePath)) {
118-
$this->printLine("\033[32m => CSS file \"" . $sFilePath . "\" is valid\033[0m" . PHP_EOL);
119-
return self::$RETURN_CODE_SUCCESS;
117+
if ($cssLinter->lintFile($filePath)) {
118+
$this->printLine("\033[32m => CSS file \"" . $filePath . "\" is valid\033[0m" . PHP_EOL);
119+
return self::RETURN_CODE_SUCCESS;
120120
}
121121

122-
$this->printLine("\033[31m => CSS file \"" . $sFilePath . "\" is not valid:\033[0m" . PHP_EOL);
123-
$this->displayLinterErrors($oCssLinter->getErrors());
124-
return self::$RETURN_CODE_ERROR;
122+
$this->printLine("\033[31m => CSS file \"" . $filePath . "\" is not valid:\033[0m" . PHP_EOL);
123+
$this->displayLinterErrors($cssLinter->getErrors());
124+
return self::RETURN_CODE_ERROR;
125125
}
126126

127127

128128
/**
129129
* Performs lint on a given string
130-
* @param \CssLint\Linter $oCssLinter the instance of the linter
131-
* @param string $sString the CSS string to be linted
130+
* @param \CssLint\Linter $cssLinter the instance of the linter
131+
* @param string $stringValue the CSS string to be linted
132132
* @return int the return code related to the execution of the linter
133133
*/
134-
private function lintString(\CssLint\Linter $oCssLinter, string $sString): int
134+
private function lintString(\CssLint\Linter $cssLinter, string $stringValue): int
135135
{
136136
$this->printLine('# Lint CSS string...');
137137

138-
if ($oCssLinter->lintString($sString)) {
138+
if ($cssLinter->lintString($stringValue)) {
139139
$this->printLine("\033[32m => CSS string is valid\033[0m" . PHP_EOL);
140-
return self::$RETURN_CODE_SUCCESS;
140+
return self::RETURN_CODE_SUCCESS;
141141
}
142142

143143
$this->printLine("\033[31m => CSS string is not valid:\033[0m" . PHP_EOL);
144-
$this->displayLinterErrors($oCssLinter->getErrors());
145-
return self::$RETURN_CODE_ERROR;
144+
$this->displayLinterErrors($cssLinter->getErrors());
145+
return self::RETURN_CODE_ERROR;
146146
}
147147

148148
/**
149149
* Display an error message
150-
* @param string $sError the message to be displayed
150+
* @param string $error the message to be displayed
151151
*/
152-
private function printError(string $sError)
152+
private function printError(string $error)
153153
{
154-
$this->printLine("\033[31m/!\ Error: " . $sError . "\033[0m" . PHP_EOL);
154+
$this->printLine("\033[31m/!\ Error: " . $error . "\033[0m" . PHP_EOL);
155155
}
156156

157157
/**
158158
* Display the errors returned by the linter
159-
* @param array $aErrors the generated errors to be displayed
159+
* @param array $errors the generated errors to be displayed
160160
*/
161-
private function displayLinterErrors(array $aErrors)
161+
private function displayLinterErrors(array $errors)
162162
{
163-
foreach ($aErrors as $sError) {
164-
$this->printLine("\033[31m - " . $sError . "\033[0m");
163+
foreach ($errors as $error) {
164+
$this->printLine("\033[31m - " . $error . "\033[0m");
165165
}
166166
$this->printLine("");
167167
}
168168

169169
/**
170170
* Display the given message in a new line
171-
* @param string $sMessage the message to be displayed
171+
* @param string $message the message to be displayed
172172
*/
173-
private function printLine(string $sMessage)
173+
private function printLine(string $message)
174174
{
175-
echo $sMessage . PHP_EOL;
175+
echo $message . PHP_EOL;
176176
}
177177
}

src/CssLint/CliArgs.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,47 @@ 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+
$parsedArguments = $this->extractArguments($arguments);
2828

29-
if (!empty($aParsedArguments['options'])) {
30-
$this->options = $aParsedArguments['options'];
29+
if (!empty($parsedArguments['options'])) {
30+
$this->options = $parsedArguments['options'];
3131
}
3232
}
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 $argument) {
4444
// --foo --bar=baz
45-
if (substr($sArgument, 0, 2) == '--') {
46-
$sEqualPosition = strpos($sArgument, '=');
45+
if (str_starts_with((string) $argument, '--')) {
46+
$equalPosition = strpos((string) $argument, '=');
4747

4848
// --bar=baz
49-
if ($sEqualPosition !== false) {
50-
$sKey = substr($sArgument, 2, $sEqualPosition - 2);
51-
$sValue = substr($sArgument, $sEqualPosition + 1);
52-
$aParsedArguments[$sKey] = $sValue;
49+
if ($equalPosition !== false) {
50+
$key = substr((string) $argument, 2, $equalPosition - 2);
51+
$value = substr((string) $argument, $equalPosition + 1);
52+
$aParsedArguments[$key] = $value;
5353
}
5454
}
5555
}

0 commit comments

Comments
 (0)