diff --git a/custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php b/custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php index 2e8a703..47ab8a2 100644 --- a/custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php +++ b/custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php @@ -13,7 +13,7 @@ class FullyQualifiedSniff implements Sniff */ public function register() { - return array(T_DOUBLE_COLON, T_NEW); + return array(T_DOUBLE_COLON, T_NEW, T_EXTENDS); } /** @@ -43,11 +43,17 @@ private function getClassCall(File $phpcsFile, $stackPtr): string switch ($tokens[$stackPtr]['code']) { case T_NEW: - return $phpcsFile->getTokensAsString( + $tokensAsString = $phpcsFile->getTokensAsString( $stackPtr, $phpcsFile->findEndOfStatement($stackPtr) - $stackPtr ); + return substr( + $tokensAsString, + 0, + strpos($tokensAsString, '(') + ); + case T_DOUBLE_COLON: $classCallStart = $phpcsFile->findStartOfStatement($stackPtr); @@ -55,6 +61,18 @@ private function getClassCall(File $phpcsFile, $stackPtr): string $classCallStart, $stackPtr - $classCallStart ); + + case T_EXTENDS: + $tokensAsString = $phpcsFile->getTokensAsString( + $stackPtr, + $phpcsFile->findEndOfStatement($stackPtr) - $stackPtr + ); + + return trim(substr( + $tokensAsString, + 0, + strpos($tokensAsString, '{') + )); } throw new RuntimeException(sprintf( diff --git a/tests/rules/classes/allowed/FullyQualifiedSniff.php b/tests/rules/classes/allowed/FullyQualifiedSniff.php index 43ed644..cbebc25 100644 --- a/tests/rules/classes/allowed/FullyQualifiedSniff.php +++ b/tests/rules/classes/allowed/FullyQualifiedSniff.php @@ -5,12 +5,20 @@ namespace flyeralarm\Test; use RuntimeException; +use stdClass; -class FullyQualifiedSniff +class FullyQualifiedSniff extends stdClass { public function a() { $className = RuntimeException::class; $a = new RuntimeException(); } + + public function b() + { + $a = new RuntimeException( + 'We can\'t explain' + ); + } } diff --git a/tests/rules/classes/allowed/NamespaceVendorLowercasePackageUpperCamelCase.php b/tests/rules/classes/allowed/NamespaceVendorLowercasePackageUpperCamelCase.php index b4ed219..8bd59de 100644 --- a/tests/rules/classes/allowed/NamespaceVendorLowercasePackageUpperCamelCase.php +++ b/tests/rules/classes/allowed/NamespaceVendorLowercasePackageUpperCamelCase.php @@ -4,6 +4,8 @@ namespace flyeralarm\FooBar; -class NamespaceVendorLowercasePackageUpperCamelCase extends \PHP_CodeSniffer_File +use PHP_CodeSniffer_File; + +class NamespaceVendorLowercasePackageUpperCamelCase extends PHP_CodeSniffer_File { } diff --git a/tests/rules/classes/not-allowed/FullyQualifiedSniff.php b/tests/rules/classes/not-allowed/FullyQualifiedSniff.php index 720f040..0e7901d 100644 --- a/tests/rules/classes/not-allowed/FullyQualifiedSniff.php +++ b/tests/rules/classes/not-allowed/FullyQualifiedSniff.php @@ -1,6 +1,6 @@