Skip to content

Commit 7434e73

Browse files
authored
fix: properly disable AlphabeticallyOrderedConstants by default (#133)
1 parent f39c43b commit 7434e73

File tree

6 files changed

+122
-95
lines changed

6 files changed

+122
-95
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ composer require --dev cdn77/coding-standard
1414
* Reference this coding standard in your `phpcs.xml.dist` (_check out [the one used in this project](phpcs.xml.dist)_):
1515

1616
```
17-
<rule ref="Cdn77CS" />
17+
<rule ref="Cdn77" />
1818
```

phpstan-baseline.neon

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: "#^Cannot access offset 'properties' on mixed\\.$#"
5+
count: 1
6+
path: tests/TestCase.php
7+
8+
-
9+
message: "#^Cannot access offset 'severity' on mixed\\.$#"
10+
count: 1
11+
path: tests/TestCase.php
12+
13+
-
14+
message: "#^Method Cdn77\\\\TestCase\\:\\:checkFile\\(\\) has parameter \\$sniffConfig with no value type specified in iterable type array\\.$#"
15+
count: 1
16+
path: tests/TestCase.php
17+
18+
-
19+
message: "#^Method Cdn77\\\\TestCase\\:\\:getSniffClassReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#"
20+
count: 1
21+
path: tests/TestCase.php
22+
23+
-
24+
message: "#^Method Cdn77\\\\TestCase\\:\\:getSniffName\\(\\) should return string but returns string\\|null\\.$#"
25+
count: 1
26+
path: tests/TestCase.php
27+
28+
-
29+
message: "#^Parameter \\#3 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
30+
count: 1
31+
path: tests/TestCase.php

src/Cdn77/ruleset.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<arg value="p"/>
1313
<arg value="s"/>
1414

15+
<rule ref="Cdn77.Ordering.AlphabeticallyOrderedConstants">
16+
<severity>0</severity>
17+
</rule>
18+
1519
<!-- use Doctrine as the base standard -->
1620
<rule ref="Doctrine">
1721
<exclude name="Generic.Formatting.MultipleStatementAlignment"/>

src/Cdn77CS/ruleset.xml

Lines changed: 0 additions & 93 deletions
This file was deleted.

tests/Sniffs/Ordering/AlphabeticallyOrderedConstantsSniffTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ final class AlphabeticallyOrderedConstantsSniffTest extends TestCase
1515
{
1616
public function testErrors(): void
1717
{
18-
$file = self::checkFile(__DIR__ . '/data/AlphabeticallyOrderedConstantsSniffTest.inc');
18+
$file = self::checkFile(
19+
__DIR__ . '/data/AlphabeticallyOrderedConstantsSniffTest.inc',
20+
sniffConfig: ['severity' => 5],
21+
);
1922
$expectedErrors = [
2023
9 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
2124
19 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,

tests/TestCase.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@
44

55
namespace Cdn77;
66

7+
use PHP_CodeSniffer\Config;
8+
use PHP_CodeSniffer\Files\File;
9+
use PHP_CodeSniffer\Files\LocalFile;
10+
use PHP_CodeSniffer\Runner;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
use ReflectionClass;
713
use SlevomatCodingStandard\Sniffs\TestCase as SlevomatTestCase;
814

15+
use function array_merge;
916
use function assert;
1017
use function class_exists;
18+
use function count;
19+
use function define;
20+
use function defined;
21+
use function in_array;
22+
use function preg_replace;
23+
use function sprintf;
1124
use function str_replace;
1225
use function strlen;
26+
use function strpos;
1327
use function substr;
1428

1529
abstract class TestCase extends SlevomatTestCase
@@ -21,4 +35,74 @@ protected static function getSniffClassName(): string
2135

2236
return $class;
2337
}
38+
39+
// phpcs:ignore
40+
protected static function checkFile(string $filePath, array $sniffProperties = [], array $codesToCheck = [], array $cliArgs = [], array $sniffConfig = []): File
41+
{
42+
if (defined('PHP_CODESNIFFER_CBF') === false) {
43+
// phpcs:ignore
44+
define('PHP_CODESNIFFER_CBF', false);
45+
}
46+
47+
$codeSniffer = new Runner();
48+
$codeSniffer->config = new Config(array_merge(['-s'], $cliArgs));
49+
$codeSniffer->init();
50+
51+
if (count($sniffConfig) > 0) {
52+
$codeSniffer->ruleset->ruleset[self::getSniffName()] = $sniffConfig;
53+
}
54+
55+
if (count($sniffProperties) > 0) {
56+
$codeSniffer->ruleset->ruleset[self::getSniffName()]['properties'] = $sniffProperties;
57+
}
58+
59+
$sniffClassName = static::getSniffClassName();
60+
$sniff = new $sniffClassName();
61+
assert($sniff instanceof Sniff);
62+
63+
$codeSniffer->ruleset->sniffs = [$sniffClassName => $sniff];
64+
65+
if (count($codesToCheck) > 0) {
66+
foreach (self::getSniffClassReflection()->getConstants() as $constantName => $constantValue) {
67+
if (strpos($constantName, 'CODE_') !== 0 || in_array($constantValue, $codesToCheck, true)) {
68+
continue;
69+
}
70+
71+
$codeSniffer->ruleset->ruleset[sprintf('%s.%s', self::getSniffName(), $constantValue)]['severity'] = 0;
72+
}
73+
}
74+
75+
$codeSniffer->ruleset->populateTokenListeners();
76+
77+
$file = new LocalFile($filePath, $codeSniffer->ruleset, $codeSniffer->config);
78+
$file->process();
79+
80+
return $file;
81+
}
82+
83+
private static function getSniffName(): string
84+
{
85+
return preg_replace(
86+
[
87+
'~\\\~',
88+
'~\.Sniffs~',
89+
'~Sniff$~',
90+
],
91+
[
92+
'.',
93+
'',
94+
'',
95+
],
96+
static::getSniffClassName(),
97+
);
98+
}
99+
100+
private static function getSniffClassReflection(): ReflectionClass
101+
{
102+
static $reflections = [];
103+
104+
$className = static::getSniffClassName();
105+
106+
return $reflections[$className] ?? $reflections[$className] = new ReflectionClass($className);
107+
}
24108
}

0 commit comments

Comments
 (0)