Skip to content

Commit 44f5de5

Browse files
committed
Vendor: supports PHPCS v4
1 parent 4ed4877 commit 44f5de5

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"require": {
1414
"nette/neon": "^3.4",
1515
"php": "^8.2",
16-
"squizlabs/php_codesniffer": "^3.13"
16+
"squizlabs/php_codesniffer": "^4.0"
1717
},
1818
"require-dev": {
1919
"forrest79/phpcs": "^2.1",
@@ -32,7 +32,7 @@
3232
}
3333
},
3434
"scripts": {
35-
"phpcs": "vendor/bin/phpcs -s src tests/run-tests.php",
35+
"phpcs": "vendor/bin/phpcs -q -s src tests/run-tests.php",
3636
"phpstan": "vendor/bin/phpstan analyse src tests/run-tests.php",
3737
"tests": "php tests/run-tests.php"
3838
},

src/File.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ final class File extends PHP_CodeSniffer\Files\LocalFile
1919

2020
private int $ignoredWarningCount = 0;
2121

22-
private int $ignoredFixableCount = 0;
22+
private int $ignoredFixableErrorCount = 0;
23+
24+
private int $ignoredFixableWarningCount = 0;
2325

2426
private bool $shouldShowOutdatedWarnings;
2527

@@ -52,16 +54,18 @@ public function process(): void
5254

5355

5456
/**
55-
* @param bool $error
56-
* @param string $message
57-
* @param int $line
58-
* @param int $column
59-
* @param string $code
6057
* @param array<mixed> $data
61-
* @param int $severity
62-
* @param bool $fixable
6358
*/
64-
protected function addMessage($error, $message, $line, $column, $code, $data, $severity, $fixable): bool
59+
protected function addMessage(
60+
bool $error,
61+
string $message,
62+
int $line,
63+
int $column,
64+
string $code,
65+
array $data,
66+
int $severity,
67+
bool $fixable,
68+
): bool
6569
{
6670
// This condition is for PHPCBF - when error is ignored and fixable, we want to ignore it immediately.
6771
// For PHPCS we want to process all errors in a classic way - because than we have complete cache a that we want.
@@ -96,7 +100,8 @@ private function checkIgnoredErrors(): void
96100
{
97101
$ignoredErrorCount = 0;
98102
$ignoredWarningCount = 0;
99-
$ignoredFixableCount = 0;
103+
$ignoredFixableErrorCount = 0;
104+
$ignoredFixableWarningCount = 0;
100105

101106
if ($this->getErrorCount() !== 0) {
102107
$errors = [];
@@ -111,7 +116,7 @@ private function checkIgnoredErrors(): void
111116
if ($this->isIgnored($data['source'], $data['message'])) {
112117
$ignoredErrorCount++;
113118
if ($data['fixable']) {
114-
$ignoredFixableCount++;
119+
$ignoredFixableErrorCount++;
115120
}
116121
continue;
117122
}
@@ -147,7 +152,7 @@ private function checkIgnoredErrors(): void
147152
if ($this->isIgnored($data['source'], $data['message'])) {
148153
$ignoredWarningCount++;
149154
if ($data['fixable']) {
150-
$ignoredFixableCount++;
155+
$ignoredFixableWarningCount++;
151156
}
152157
continue;
153158
}
@@ -166,7 +171,7 @@ private function checkIgnoredErrors(): void
166171
}
167172
}
168173

169-
$this->setIgnoredFixableCount($ignoredFixableCount);
174+
$this->setIgnoredFixableCount($ignoredFixableErrorCount, $ignoredFixableWarningCount);
170175

171176
$outdatedErrorCount = 0;
172177

@@ -290,15 +295,28 @@ public function getWarningCount(): int
290295
}
291296

292297

293-
private function setIgnoredFixableCount(int $count): void
298+
private function setIgnoredFixableCount(int $errorCount, int $warningCount): void
294299
{
295-
$this->ignoredFixableCount = $count;
300+
$this->ignoredFixableErrorCount = $errorCount;
301+
$this->ignoredFixableWarningCount = $warningCount;
296302
}
297303

298304

299305
public function getFixableCount(): int
300306
{
301-
return parent::getFixableCount() - $this->ignoredFixableCount;
307+
return parent::getFixableCount() - $this->ignoredFixableErrorCount - $this->ignoredFixableWarningCount;
308+
}
309+
310+
311+
public function getFixableErrorCount(): int
312+
{
313+
return parent::getFixableErrorCount() - $this->ignoredFixableErrorCount;
314+
}
315+
316+
317+
public function getFixableWarningCount(): int
318+
{
319+
return parent::getFixableWarningCount() - $this->ignoredFixableWarningCount;
302320
}
303321

304322
}

tests/run-tests.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ final class TestsRunner
1313
private const PARALLEL_PARAM = '--parallel=2';
1414
private const CACHE_PARAM = '--cache=temp/cache.json';
1515

16-
private const EXIT_CODE_OK = 0;
17-
private const EXIT_CODE_ERROR = 1;
18-
private const EXIT_CODE_FIXABLE_ERROR = 2;
16+
private const EXIT_CODE_OK = 0; // or all fixable errors were fixed and there is no error
17+
private const EXIT_CODE_AUTO_FIXABLE_ERROR = 1;
18+
private const EXIT_CODE_NON_AUTO_FIXABLE_ERROR = 2;
19+
private const EXIT_CODE_AUTO_AND_NON_AUTO_FIXABLE_ERROR = 3;
1920

2021
/**
2122
* [$title, $parameters, $cleanCache]
@@ -149,7 +150,7 @@ private function tests2(): bool
149150
echo sprintf(' - %s: ', $title);
150151

151152
$exec = $this->exec(self::PHPCS_BIN, 'tests02', null, $params);
152-
if ($exec['exitCode'] !== self::EXIT_CODE_ERROR) {
153+
if ($exec['exitCode'] !== self::EXIT_CODE_NON_AUTO_FIXABLE_ERROR) {
153154
echo 'PHPCS exited unexpectedly' . PHP_EOL;
154155
return false;
155156
}
@@ -220,7 +221,7 @@ private function tests4(): bool
220221
echo sprintf(' - %s: ', $title);
221222

222223
$exec = $this->exec(self::PHPCS_BIN, 'tests04', self::BOOTSTRAP_PARAM, $params);
223-
if ($exec['exitCode'] !== self::EXIT_CODE_FIXABLE_ERROR) {
224+
if ($exec['exitCode'] !== self::EXIT_CODE_AUTO_FIXABLE_ERROR) {
224225
echo 'PHPCS exited unexpectedly' . PHP_EOL;
225226
return false;
226227
}
@@ -261,7 +262,7 @@ private function tests5(): bool
261262
echo sprintf(' - %s: ', $title);
262263

263264
$exec = $this->exec(self::PHPCS_BIN, 'tests05', self::BOOTSTRAP_PARAM, $params);
264-
if ($exec['exitCode'] !== self::EXIT_CODE_ERROR) {
265+
if ($exec['exitCode'] !== self::EXIT_CODE_NON_AUTO_FIXABLE_ERROR) {
265266
echo 'PHPCS exited unexpectedly' . PHP_EOL;
266267
return false;
267268
}
@@ -332,7 +333,7 @@ private function tests7(): bool
332333
echo sprintf(' - %s: ', $title);
333334

334335
$exec = $this->exec(self::PHPCS_BIN, 'tests07', self::BOOTSTRAP_PARAM, $params);
335-
if ($exec['exitCode'] !== self::EXIT_CODE_FIXABLE_ERROR) {
336+
if ($exec['exitCode'] !== self::EXIT_CODE_AUTO_AND_NON_AUTO_FIXABLE_ERROR) {
336337
echo 'PHPCS exited unexpectedly' . PHP_EOL;
337338
return false;
338339
}
@@ -414,7 +415,7 @@ private function tests9(): bool
414415
echo sprintf(' - %s: ', $title);
415416

416417
$exec = $this->exec(self::PHPCS_BIN, 'tests09', self::BOOTSTRAP_OUTDATED_PARAM, $params);
417-
if ($exec['exitCode'] !== self::EXIT_CODE_ERROR) {
418+
if ($exec['exitCode'] !== self::EXIT_CODE_NON_AUTO_FIXABLE_ERROR) {
418419
echo 'PHPCS exited unexpectedly' . PHP_EOL;
419420
return false;
420421
}
@@ -453,6 +454,8 @@ private function tests10(): bool
453454
foreach (self::DEFAULT_TEST_CASES as $test) {
454455
[$title, $params, $cleanCache] = $test;
455456

457+
$params[] = '2>&1'; // we want an error output
458+
456459
echo sprintf(' - %s: ', $title);
457460

458461
$exec = $this->exec(self::PHPCBF_BIN, 'tests10', self::BOOTSTRAP_PARAM, $params);
@@ -485,7 +488,7 @@ private function tests11(): bool
485488
echo sprintf(' - %s: ', $title);
486489

487490
$exec = $this->exec(self::PHPCBF_BIN, 'tests11', self::BOOTSTRAP_PARAM, $params);
488-
if ($exec['exitCode'] !== self::EXIT_CODE_ERROR) {
491+
if ($exec['exitCode'] !== self::EXIT_CODE_OK) {
489492
echo 'PHPCS exited unexpectedly' . PHP_EOL;
490493
return false;
491494
}
@@ -527,7 +530,7 @@ private function tests12(): bool
527530
echo sprintf(' - %s: ', $title);
528531

529532
$exec = $this->exec(self::PHPCS_BIN, 'tests12', self::BOOTSTRAP_PARAM, $params, '\\\\Forrest79\\\\PhpCsIgnores\\\\BaselineReport');
530-
if ($exec['exitCode'] !== self::EXIT_CODE_FIXABLE_ERROR) {
533+
if ($exec['exitCode'] !== self::EXIT_CODE_AUTO_FIXABLE_ERROR) {
531534
echo 'PHPCS exited unexpectedly' . PHP_EOL;
532535
return false;
533536
}
@@ -581,7 +584,7 @@ private function exec(
581584
$parameters[] = $boostrap;
582585
}
583586

584-
$command = sprintf('%s --standard=%s/phpcs.xml --report=%s %s -s %s', $bin, $dir, $report, implode(' ', $parameters), $dir);
587+
$command = sprintf('%s --standard=%s/phpcs.xml --report=%s %s -q -s %s', $bin, $dir, $report, implode(' ', $parameters), $dir);
585588

586589
if (exec($command, $output, $exitCode) === false) {
587590
throw new \RuntimeException('Can\'t run PHPCS.');

0 commit comments

Comments
 (0)