diff --git a/README.md b/README.md index 992540f..4d648b6 100644 --- a/README.md +++ b/README.md @@ -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)_): ``` - + ``` diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 364905f..49f9215 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 diff --git a/src/Cdn77/ruleset.xml b/src/Cdn77/ruleset.xml index 821fc08..3480f7c 100644 --- a/src/Cdn77/ruleset.xml +++ b/src/Cdn77/ruleset.xml @@ -12,6 +12,10 @@ + + 0 + + diff --git a/src/Cdn77CS/ruleset.xml b/src/Cdn77CS/ruleset.xml deleted file mode 100644 index f178ab7..0000000 --- a/src/Cdn77CS/ruleset.xml +++ /dev/null @@ -1,93 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/Sniffs/Ordering/AlphabeticallyOrderedConstantsSniffTest.php b/tests/Sniffs/Ordering/AlphabeticallyOrderedConstantsSniffTest.php index ec1d05d..2378ba6 100644 --- a/tests/Sniffs/Ordering/AlphabeticallyOrderedConstantsSniffTest.php +++ b/tests/Sniffs/Ordering/AlphabeticallyOrderedConstantsSniffTest.php @@ -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, diff --git a/tests/TestCase.php b/tests/TestCase.php index 126da5f..913e03c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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 @@ -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); + } }