Skip to content

Commit

Permalink
fix: properly disable AlphabeticallyOrderedConstants by default
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jan 9, 2024
1 parent f39c43b commit b7be2a6
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ composer require --dev cdn77/coding-standard
* Reference this coding standard in your `phpcs.xml.dist` (_check out [the one used in this project](phpcs.xml.dist)_):

```
<rule ref="Cdn77CS" />
<rule ref="Cdn77" />
```
29 changes: 29 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
parameters:
ignoreErrors:
-
message: "#^Cannot access offset 'properties' on mixed\\.$#"
count: 1
path: tests/TestCase.php

-
message: "#^Cannot access offset 'severity' on mixed\\.$#"
count: 1
path: tests/TestCase.php

-
message: "#^Method Cdn77\\\\TestCase\\:\\:checkFile\\(\\) has parameter \\$sniffConfig with no value type specified in iterable type array\\.$#"
count: 1
path: tests/TestCase.php

-
message: "#^Method Cdn77\\\\TestCase\\:\\:getSniffClassReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#"
count: 1
path: tests/TestCase.php

-
message: "#^Method Cdn77\\\\TestCase\\:\\:getSniffName\\(\\) should return string but returns string\\|null\\.$#"
count: 1
path: tests/TestCase.php

-
message: "#^Parameter \\#3 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
path: tests/TestCase.php
4 changes: 4 additions & 0 deletions src/Cdn77/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<arg value="p"/>
<arg value="s"/>

<rule ref="Cdn77.Ordering.AlphabeticallyOrderedConstants">
<severity>0</severity>
</rule>

<!-- use Doctrine as the base standard -->
<rule ref="Doctrine">
<exclude name="Generic.Formatting.MultipleStatementAlignment"/>
Expand Down
93 changes: 0 additions & 93 deletions src/Cdn77CS/ruleset.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ final class AlphabeticallyOrderedConstantsSniffTest extends TestCase
{
public function testErrors(): void
{
$file = self::checkFile(__DIR__ . '/data/AlphabeticallyOrderedConstantsSniffTest.inc');
$file = self::checkFile(
__DIR__ . '/data/AlphabeticallyOrderedConstantsSniffTest.inc',
sniffConfig: ['severity' => 5],
);
$expectedErrors = [
9 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
19 => AlphabeticallyOrderedConstantsSniff::CodeIncorrectConstantOrder,
Expand Down
84 changes: 84 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@

namespace Cdn77;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Files\LocalFile;
use PHP_CodeSniffer\Runner;
use PHP_CodeSniffer\Sniffs\Sniff;
use ReflectionClass;
use SlevomatCodingStandard\Sniffs\TestCase as SlevomatTestCase;

use function array_merge;
use function assert;
use function class_exists;
use function count;
use function define;
use function defined;
use function in_array;
use function preg_replace;
use function sprintf;
use function str_replace;
use function strlen;
use function strpos;
use function substr;

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

return $class;
}

// phpcs:ignore
protected static function checkFile(string $filePath, array $sniffProperties = [], array $codesToCheck = [], array $cliArgs = [], array $sniffConfig = []): File
{
if (defined('PHP_CODESNIFFER_CBF') === false) {
// phpcs:ignore
define('PHP_CODESNIFFER_CBF', false);
}

$codeSniffer = new Runner();
$codeSniffer->config = new Config(array_merge(['-s'], $cliArgs));
$codeSniffer->init();

if (count($sniffConfig) > 0) {
$codeSniffer->ruleset->ruleset[self::getSniffName()] = $sniffConfig;
}

if (count($sniffProperties) > 0) {
$codeSniffer->ruleset->ruleset[self::getSniffName()]['properties'] = $sniffProperties;
}

$sniffClassName = static::getSniffClassName();
$sniff = new $sniffClassName();
assert($sniff instanceof Sniff);

$codeSniffer->ruleset->sniffs = [$sniffClassName => $sniff];

if (count($codesToCheck) > 0) {
foreach (self::getSniffClassReflection()->getConstants() as $constantName => $constantValue) {
if (strpos($constantName, 'CODE_') !== 0 || in_array($constantValue, $codesToCheck, true)) {
continue;
}

$codeSniffer->ruleset->ruleset[sprintf('%s.%s', self::getSniffName(), $constantValue)]['severity'] = 0;
}
}

$codeSniffer->ruleset->populateTokenListeners();

$file = new LocalFile($filePath, $codeSniffer->ruleset, $codeSniffer->config);
$file->process();

return $file;
}

private static function getSniffName(): string
{
return preg_replace(
[
'~\\\~',
'~\.Sniffs~',
'~Sniff$~',
],
[
'.',
'',
'',
],
static::getSniffClassName(),
);
}

private static function getSniffClassReflection(): ReflectionClass
{
static $reflections = [];

$className = static::getSniffClassName();

return $reflections[$className] ?? $reflections[$className] = new ReflectionClass($className);
}
}

0 comments on commit b7be2a6

Please sign in to comment.