From 0eade671b2d1e7ff8663810109c675ee3b017d99 Mon Sep 17 00:00:00 2001 From: Michael Nozdrevatykh Date: Fri, 2 Oct 2020 09:41:01 +0200 Subject: [PATCH 1/2] Add fully qualified class sniff --- .../Sniffs/Classes/FullyQualifiedSniff.php | 65 +++++++++++++++++++ ruleset.xml | 1 + .../classes/allowed/FullyQualifiedSniff.php | 16 +++++ .../not-allowed/FullyQualifiedSniff.php | 13 ++++ .../not-allowed/FullyQualifiedSniffStatic.php | 13 ++++ 5 files changed, 108 insertions(+) create mode 100644 custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php create mode 100644 tests/rules/classes/allowed/FullyQualifiedSniff.php create mode 100644 tests/rules/classes/not-allowed/FullyQualifiedSniff.php create mode 100644 tests/rules/classes/not-allowed/FullyQualifiedSniffStatic.php diff --git a/custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php b/custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php new file mode 100644 index 0000000..2e8a703 --- /dev/null +++ b/custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php @@ -0,0 +1,65 @@ +getClassCall($phpcsFile, $stackPtr); + + if (strpos($classCall, '\\') === false) { + return; + } + + $phpcsFile->addError( + 'Qualifier should be replaced with an import: "%s"', + $stackPtr, + 'Found', + [$classCall] + ); + } + + private function getClassCall(File $phpcsFile, $stackPtr): string + { + $tokens = $phpcsFile->getTokens(); + + switch ($tokens[$stackPtr]['code']) { + case T_NEW: + return $phpcsFile->getTokensAsString( + $stackPtr, + $phpcsFile->findEndOfStatement($stackPtr) - $stackPtr + ); + + case T_DOUBLE_COLON: + $classCallStart = $phpcsFile->findStartOfStatement($stackPtr); + + return $phpcsFile->getTokensAsString( + $classCallStart, + $stackPtr - $classCallStart + ); + } + + throw new RuntimeException(sprintf( + 'Unknown token type: "%s"', + $tokens[$stackPtr]['type'] + )); + } +} diff --git a/ruleset.xml b/ruleset.xml index 414afe4..c5d44a1 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -14,4 +14,5 @@ + diff --git a/tests/rules/classes/allowed/FullyQualifiedSniff.php b/tests/rules/classes/allowed/FullyQualifiedSniff.php new file mode 100644 index 0000000..43ed644 --- /dev/null +++ b/tests/rules/classes/allowed/FullyQualifiedSniff.php @@ -0,0 +1,16 @@ + Date: Fri, 2 Oct 2020 11:05:45 +0200 Subject: [PATCH 2/2] QA-fixes --- ruleset.xml | 1 - tests/ruleset.xml | 18 ++++++++++++++++++ tests/runner.php | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/ruleset.xml diff --git a/ruleset.xml b/ruleset.xml index c5d44a1..414afe4 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -14,5 +14,4 @@ - diff --git a/tests/ruleset.xml b/tests/ruleset.xml new file mode 100644 index 0000000..c5d44a1 --- /dev/null +++ b/tests/ruleset.xml @@ -0,0 +1,18 @@ + + + A custom coding standard for FLYERALARM + + + + + + + + + + + + + + + diff --git a/tests/runner.php b/tests/runner.php index a546361..c63563d 100644 --- a/tests/runner.php +++ b/tests/runner.php @@ -25,7 +25,7 @@ function processDir($dirPath) sprintf( "%s -w -p -s --standard=%s %s", escapeshellcmd(__DIR__ . '/../vendor/bin/phpcs'), - escapeshellarg(__DIR__ . '/../ruleset.xml'), + escapeshellarg(__DIR__ . '/ruleset.xml'), escapeshellarg($dirPath . $file) ) );