Skip to content

Commit 8b141bf

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

File tree

12 files changed

+423
-377
lines changed

12 files changed

+423
-377
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)))

README.md

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,4 @@
4444
2. [Usage](https://neilime.github.io/php-css-lint/usage)
4545
3. [Code Coverage](https://codecov.io/gh/neilime/php-css-lint)
4646
4. [PHP Doc](https://neilime.github.io/php-css-lint/phpdoc)
47-
48-
# Development
49-
50-
## Setup
51-
52-
`PHP_VERSION` is the version of php to use during the development. Example: `8.2`
53-
54-
```sh
55-
make build-php PHP_VERSION
56-
make install PHP_VERSION
57-
```
58-
59-
## Running tests
60-
61-
```sh
62-
make test PHP_VERSION
63-
```
64-
65-
## Fix code linting
66-
67-
```sh
68-
make lint-fix PHP_VERSION
69-
```
70-
71-
## Running CI scripts
72-
73-
```sh
74-
make ci PHP_VERSION
75-
```
47+
5. [Development](https://neilime.github.io/php-css-lint/development)

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
->withAttributesSets(
15+
all: true
16+
)
17+
->withPreparedSets(
18+
deadCode: true,
19+
codeQuality: true,
20+
codingStyle: true,
21+
typeDeclarations: true,
22+
privatization: true,
23+
naming: true,
24+
instanceOf: true,
25+
earlyReturn: true,
26+
strictBooleans: true,
27+
carbon: true,
28+
rectorPreset: true,
29+
phpunitCodeQuality: true,
30+
phpunit: true,
31+
)
32+
->withCache(
33+
cacheClass: FileCacheStorage::class,
34+
cacheDirectory: __DIR__ . '/tools/cache/rector'
35+
);;

src/CssLint/Cli.php

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,92 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace CssLint;
46

7+
/**
8+
* @phpstan-import-type Errors from \CssLint\Linter
9+
* @package CssLint
10+
*/
511
class Cli
612
{
7-
private static $SCRIPT_NAME = 'php-css-lint';
8-
private static $RETURN_CODE_ERROR = 1;
9-
private static $RETURN_CODE_SUCCESS = 0;
13+
private const SCRIPT_NAME = 'php-css-lint';
14+
15+
private const RETURN_CODE_ERROR = 1;
16+
17+
private const RETURN_CODE_SUCCESS = 0;
1018

1119
/**
1220
* Entrypoint of the cli, will execute the linter according to the given arguments
13-
* @param array $aArguments arguments to be parsed (@see $_SERVER['argv'])
21+
* @param string[] $arguments arguments to be parsed (@see $_SERVER['argv'])
1422
* @return int the return code related to the execution of the linter
1523
**/
16-
public function run(array $aArguments): int
24+
public function run(array $arguments): int
1725
{
18-
$oCliArgs = $this->parseArguments($aArguments);
19-
if (!$oCliArgs->filePathOrCssString) {
26+
$cliArgs = $this->parseArguments($arguments);
27+
if ($cliArgs->filePathOrCssString === null || $cliArgs->filePathOrCssString === '' || $cliArgs->filePathOrCssString === '0') {
2028
$this->printUsage();
21-
return self::$RETURN_CODE_SUCCESS;
29+
return self::RETURN_CODE_SUCCESS;
2230
}
2331

24-
$oProperties = new \CssLint\Properties();
25-
if ($oCliArgs->options) {
26-
$aOptions = json_decode($oCliArgs->options, true);
32+
$properties = new \CssLint\Properties();
33+
if ($cliArgs->options !== null && $cliArgs->options !== '' && $cliArgs->options !== '0') {
34+
$options = json_decode($cliArgs->options, true);
2735

28-
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;
36+
if (json_last_error() !== 0) {
37+
$errorMessage = json_last_error_msg();
38+
$this->printError('Unable to parse option argument: ' . $errorMessage);
39+
return self::RETURN_CODE_ERROR;
3240
}
3341

34-
if (!$aOptions) {
42+
if (!$options) {
3543
$this->printError('Unable to parse empty option argument');
36-
return self::$RETURN_CODE_ERROR;
44+
return self::RETURN_CODE_ERROR;
3745
}
38-
$oProperties->setOptions($aOptions);
46+
47+
if (!is_array($options)) {
48+
$this->printError('Unable to parse option argument: must be a json object');
49+
return self::RETURN_CODE_ERROR;
50+
}
51+
52+
$properties->setOptions($options);
3953
}
4054

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

43-
$sFilePathOrCssString = $oCliArgs->filePathOrCssString;
44-
if (!file_exists($sFilePathOrCssString)) {
45-
return $this->lintString($oCssLinter, $sFilePathOrCssString);
57+
$filePathOrCssString = $cliArgs->filePathOrCssString;
58+
if (!file_exists($filePathOrCssString)) {
59+
return $this->lintString($cssLinter, $filePathOrCssString);
4660
}
4761

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

54-
return $this->lintFile($oCssLinter, $sFilePath);
68+
return $this->lintFile($cssLinter, $filePath);
5569
}
5670

5771
/**
5872
* Retrieve the parsed Cli arguments from given arguments array
73+
* @param string[] $arguments arguments to be parsed (@see $_SERVER['argv'])
5974
* @return \CssLint\CliArgs an instance of Cli arguments object containing parsed arguments
6075
*/
61-
private function parseArguments(array $aArguments): \CssLint\CliArgs
76+
private function parseArguments(array $arguments): \CssLint\CliArgs
6277
{
63-
return new \CssLint\CliArgs($aArguments);
78+
return new \CssLint\CliArgs($arguments);
6479
}
6580

6681
/**
6782
* Display usage of the cli
6883
*/
69-
private function printUsage()
84+
private function printUsage(): void
7085
{
7186
$this->printLine('Usage:' . PHP_EOL .
7287
'------' . PHP_EOL .
7388
PHP_EOL .
74-
' ' . self::$SCRIPT_NAME . ' [--options=\'{ }\'] css_file_or_string_to_lint' . PHP_EOL .
89+
' ' . self::SCRIPT_NAME . " [--options='{ }'] css_file_or_string_to_lint" . PHP_EOL .
7590
PHP_EOL .
7691
'Arguments:' . PHP_EOL .
7792
'----------' . PHP_EOL .
@@ -95,83 +110,84 @@ private function printUsage()
95110
'---------' . PHP_EOL .
96111
PHP_EOL .
97112
' Lint a CSS file:' . PHP_EOL .
98-
' ' . self::$SCRIPT_NAME . ' ./path/to/css_file_path_to_lint.css' . PHP_EOL . PHP_EOL .
113+
' ' . self::SCRIPT_NAME . ' ./path/to/css_file_path_to_lint.css' . PHP_EOL . PHP_EOL .
99114
' Lint a CSS string:' . PHP_EOL .
100-
' ' . self::$SCRIPT_NAME . ' ".test { color: red; }"' . PHP_EOL . PHP_EOL .
115+
' ' . self::SCRIPT_NAME . ' ".test { color: red; }"' . PHP_EOL . PHP_EOL .
101116
' Lint with only tabulation as indentation:' . PHP_EOL .
102-
' ' . self::$SCRIPT_NAME .
117+
' ' . self::SCRIPT_NAME .
103118
' --options=\'{ "allowedIndentationChars": ["\t"] }\' ".test { color: red; }"' . PHP_EOL .
104119
PHP_EOL . PHP_EOL);
105120
}
106121

107122
/**
108123
* 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
124+
* @param \CssLint\Linter $cssLinter the instance of the linter
125+
* @param string $filePath the path of the file to be linted
111126
* @return int the return code related to the execution of the linter
112127
*/
113-
private function lintFile(\CssLint\Linter $oCssLinter, string $sFilePath): int
128+
private function lintFile(\CssLint\Linter $cssLinter, string $filePath): int
114129
{
115-
$this->printLine('# Lint CSS file "' . $sFilePath . '"...');
130+
$this->printLine('# Lint CSS file "' . $filePath . '"...');
116131

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;
132+
if ($cssLinter->lintFile($filePath)) {
133+
$this->printLine("\033[32m => CSS file \"" . $filePath . "\" is valid\033[0m" . PHP_EOL);
134+
return self::RETURN_CODE_SUCCESS;
120135
}
121136

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;
137+
$this->printLine("\033[31m => CSS file \"" . $filePath . "\" is not valid:\033[0m" . PHP_EOL);
138+
$this->displayLinterErrors($cssLinter->getErrors());
139+
return self::RETURN_CODE_ERROR;
125140
}
126141

127142

128143
/**
129144
* 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
145+
* @param \CssLint\Linter $cssLinter the instance of the linter
146+
* @param string $stringValue the CSS string to be linted
132147
* @return int the return code related to the execution of the linter
133148
*/
134-
private function lintString(\CssLint\Linter $oCssLinter, string $sString): int
149+
private function lintString(\CssLint\Linter $cssLinter, string $stringValue): int
135150
{
136151
$this->printLine('# Lint CSS string...');
137152

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

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

148163
/**
149164
* Display an error message
150-
* @param string $sError the message to be displayed
165+
* @param string $error the message to be displayed
151166
*/
152-
private function printError(string $sError)
167+
private function printError(string $error): void
153168
{
154-
$this->printLine("\033[31m/!\ Error: " . $sError . "\033[0m" . PHP_EOL);
169+
$this->printLine("\033[31m/!\ Error: " . $error . "\033[0m" . PHP_EOL);
155170
}
156171

157172
/**
158173
* Display the errors returned by the linter
159-
* @param array $aErrors the generated errors to be displayed
174+
* @param Errors $errors the generated errors to be displayed
160175
*/
161-
private function displayLinterErrors(array $aErrors)
176+
private function displayLinterErrors(array $errors): void
162177
{
163-
foreach ($aErrors as $sError) {
164-
$this->printLine("\033[31m - " . $sError . "\033[0m");
178+
foreach ($errors as $error) {
179+
$this->printLine("\033[31m - " . $error . "\033[0m");
165180
}
181+
166182
$this->printLine("");
167183
}
168184

169185
/**
170186
* Display the given message in a new line
171-
* @param string $sMessage the message to be displayed
187+
* @param string $message the message to be displayed
172188
*/
173-
private function printLine(string $sMessage)
189+
private function printLine(string $message): void
174190
{
175-
echo $sMessage . PHP_EOL;
191+
echo $message . PHP_EOL;
176192
}
177193
}

0 commit comments

Comments
 (0)