diff --git a/src/Files/File.php b/src/Files/File.php index dbaaf3b204..96bda88ef2 100644 --- a/src/Files/File.php +++ b/src/Files/File.php @@ -1204,20 +1204,15 @@ public function getFilename() * @param int $stackPtr The position of the declaration token which * declared the class, interface, trait, or function. * - * @return string|null The name of the class, interface, trait, or function; - * or NULL if the function or class is anonymous. + * @return string The name of the class, interface, trait, or function or an empty string + * if the name could not be determined (live coding). * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token is not of type - * T_FUNCTION, T_CLASS, T_ANON_CLASS, - * T_CLOSURE, T_TRAIT, T_ENUM, or T_INTERFACE. + * T_FUNCTION, T_CLASS, T_TRAIT, T_ENUM, or T_INTERFACE. */ public function getDeclarationName($stackPtr) { $tokenCode = $this->tokens[$stackPtr]['code']; - if ($tokenCode === T_ANON_CLASS || $tokenCode === T_CLOSURE) { - return null; - } - if ($tokenCode !== T_FUNCTION && $tokenCode !== T_CLASS && $tokenCode !== T_INTERFACE @@ -1236,7 +1231,7 @@ public function getDeclarationName($stackPtr) $stopPoint = $this->tokens[$stackPtr]['scope_opener']; } - $content = null; + $content = ''; for ($i = $stackPtr; $i < $stopPoint; $i++) { if ($this->tokens[$i]['code'] === T_STRING) { $content = $this->tokens[$i]['content']; diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php index e076b83dce..e83cb09bf1 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php @@ -99,12 +99,10 @@ public function process(File $phpcsFile, $stackPtr) $classPtr = $phpcsFile->getCondition($stackPtr, T_CLASS); if ($classPtr !== false) { // Check for magic methods and ignore these as the method signature cannot be changed. - $methodName = $phpcsFile->getDeclarationName($stackPtr); - if (empty($methodName) === false) { - $methodNameLc = strtolower($methodName); - if (isset($this->magicMethods[$methodNameLc]) === true) { - return; - } + $methodName = $phpcsFile->getDeclarationName($stackPtr); + $methodNameLc = strtolower($methodName); + if (isset($this->magicMethods[$methodNameLc]) === true) { + return; } // Check for extends/implements and adjust the error code when found. diff --git a/src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php index 44c16390e6..f009d56ef2 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/AbstractClassNamePrefixSniff.php @@ -44,7 +44,7 @@ public function process(File $phpcsFile, $stackPtr) } $className = $phpcsFile->getDeclarationName($stackPtr); - if ($className === null) { + if ($className === '') { // Live coding or parse error. return; } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php index d596f17442..48d93c6d50 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/CamelCapsFunctionNameSniff.php @@ -112,14 +112,14 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { + if ($methodName === '') { // Live coding or parse error. Bow out. return; } - $className = $phpcsFile->getDeclarationName($currScope); - if (isset($className) === false) { - $className = '[Anonymous Class]'; + $className = '[Anonymous Class]'; + if ($tokens[$currScope]['code'] !== T_ANON_CLASS) { + $className = $phpcsFile->getDeclarationName($currScope); } $errorData = [$className.'::'.$methodName]; @@ -187,7 +187,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); - if ($functionName === null) { + if ($functionName === '') { // Live coding or parse error. Bow out. return; } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php index e87f066b3f..1818ce1af3 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php @@ -67,10 +67,9 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop return; } - $className = $phpcsFile->getDeclarationName($currScope); - if (empty($className) === false) { - // Not an anonymous class. - $className = strtolower($className); + $className = '[Anonymous Class]'; + if ($tokens[$currScope]['code'] !== T_ANON_CLASS) { + $className = strtolower($phpcsFile->getDeclarationName($currScope)); } if ($className !== $this->currentClass) { @@ -79,7 +78,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { + if ($methodName === '') { // Live coding or parse error. Bow out. return; } @@ -170,7 +169,7 @@ protected function loadFunctionNamesInScope(File $phpcsFile, $currScope) } $methodName = $phpcsFile->getDeclarationName($i); - if ($methodName === null) { + if ($methodName === '') { // Live coding or parse error. Ignore. continue; } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceNameSuffixSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceNameSuffixSniff.php index 6dfad6b406..1bd01b335c 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/InterfaceNameSuffixSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/InterfaceNameSuffixSniff.php @@ -39,7 +39,7 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $interfaceName = $phpcsFile->getDeclarationName($stackPtr); - if ($interfaceName === null) { + if ($interfaceName === '') { // Live coding or parse error. Bow out. return; } diff --git a/src/Standards/Generic/Sniffs/NamingConventions/TraitNameSuffixSniff.php b/src/Standards/Generic/Sniffs/NamingConventions/TraitNameSuffixSniff.php index 79a77557d1..45bc549718 100644 --- a/src/Standards/Generic/Sniffs/NamingConventions/TraitNameSuffixSniff.php +++ b/src/Standards/Generic/Sniffs/NamingConventions/TraitNameSuffixSniff.php @@ -39,7 +39,7 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $traitName = $phpcsFile->getDeclarationName($stackPtr); - if ($traitName === null) { + if ($traitName === '') { // Live coding or parse error. Bow out. return; } diff --git a/src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.7.inc b/src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.7.inc new file mode 100644 index 0000000000..8a0dd4827f --- /dev/null +++ b/src/Standards/Generic/Tests/CodeAnalysis/UselessOverridingMethodUnitTest.7.inc @@ -0,0 +1,9 @@ +getMethodProperties($stackPtr); - $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($tokens[$stackPtr]['code'] === T_FUNCTION && $methodName !== null) { - if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { - $spaces = 'newline'; - } else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) { - $spaces = $tokens[($openBracket - 1)]['length']; - } else { - $spaces = 0; - } - - if ($spaces !== 0) { - $error = 'Expected 0 spaces before opening parenthesis; %s found'; - $data = [$spaces]; - $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data); - if ($fix === true) { - $phpcsFile->fixer->replaceToken(($openBracket - 1), ''); + if ($tokens[$stackPtr]['code'] === T_FUNCTION) { + $methodProps = $phpcsFile->getMethodProperties($stackPtr); + $methodName = $phpcsFile->getDeclarationName($stackPtr); + if ($methodName !== '') { + if ($tokens[($openBracket - 1)]['content'] === $phpcsFile->eolChar) { + $spaces = 'newline'; + } else if ($tokens[($openBracket - 1)]['code'] === T_WHITESPACE) { + $spaces = $tokens[($openBracket - 1)]['length']; + } else { + $spaces = 0; } - } - // Must be no space before semicolon in abstract/interface methods. - if ($methodProps['has_body'] === false) { - $end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket); - if ($end !== false) { - if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) { - $spaces = 'newline'; - } else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) { - $spaces = $tokens[($end - 1)]['length']; - } else { - $spaces = 0; + if ($spaces !== 0) { + $error = 'Expected 0 spaces before opening parenthesis; %s found'; + $data = [$spaces]; + $fix = $phpcsFile->addFixableError($error, $openBracket, 'SpaceBeforeOpenParen', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($openBracket - 1), ''); } + } - if ($spaces !== 0) { - $error = 'Expected 0 spaces before semicolon; %s found'; - $data = [$spaces]; - $fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data); - if ($fix === true) { - $phpcsFile->fixer->replaceToken(($end - 1), ''); + // Must be no space before semicolon in abstract/interface methods. + if ($methodProps['has_body'] === false) { + $end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket); + if ($end !== false) { + if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) { + $spaces = 'newline'; + } else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) { + $spaces = $tokens[($end - 1)]['length']; + } else { + $spaces = 0; + } + + if ($spaces !== 0) { + $error = 'Expected 0 spaces before semicolon; %s found'; + $data = [$spaces]; + $fix = $phpcsFile->addFixableError($error, $end, 'SpaceBeforeSemicolon', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($end - 1), ''); + } } } - } + }//end if }//end if }//end if diff --git a/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php index b1521aa682..f46fb2791d 100644 --- a/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/src/Standards/PEAR/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -83,14 +83,14 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { - // Ignore closures. + if ($methodName === '') { + // Ignore live coding. return; } - $className = $phpcsFile->getDeclarationName($currScope); - if (isset($className) === false) { - $className = '[Anonymous Class]'; + $className = '[Anonymous Class]'; + if ($tokens[$currScope]['code'] !== T_ANON_CLASS) { + $className = $phpcsFile->getDeclarationName($currScope); } $errorData = [$className.'::'.$methodName]; @@ -181,11 +181,6 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); - if ($functionName === null) { - // Ignore closures. - return; - } - if (ltrim($functionName, '_') === '') { // Ignore special functions. return; diff --git a/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.inc b/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.1.inc similarity index 100% rename from src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.inc rename to src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.1.inc diff --git a/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.2.inc b/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.2.inc new file mode 100644 index 0000000000..dcb5ed507a --- /dev/null +++ b/src/Standards/PEAR/Tests/Commenting/ClassCommentUnitTest.2.inc @@ -0,0 +1,7 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 4 => 1, - 15 => 1, - 51 => 1, - 63 => 1, - 65 => 2, - 66 => 1, - 68 => 1, - 70 => 1, - 71 => 1, - 72 => 1, - 74 => 2, - 75 => 1, - 76 => 1, - 77 => 1, - 85 => 1, - 96 => 5, - 106 => 5, - 116 => 5, - 126 => 5, - 161 => 1, - 163 => 1, - ]; + switch ($testFile) { + case 'ClassCommentUnitTest.1.inc': + return [ + 4 => 1, + 15 => 1, + 51 => 1, + 63 => 1, + 65 => 2, + 66 => 1, + 68 => 1, + 70 => 1, + 71 => 1, + 72 => 1, + 74 => 2, + 75 => 1, + 76 => 1, + 77 => 1, + 85 => 1, + 96 => 5, + 106 => 5, + 116 => 5, + 126 => 5, + 161 => 1, + 163 => 1, + ]; + + case 'ClassCommentUnitTest.2.inc': + return [ + 7 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -63,14 +76,22 @@ public function getErrorList() * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * + * @param string $testFile The name of the test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 71 => 1, - 73 => 1, - ]; + switch ($testFile) { + case 'ClassCommentUnitTest.1.inc': + return [ + 71 => 1, + 73 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.1.inc similarity index 100% rename from src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc rename to src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.1.inc diff --git a/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc.fixed b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc.fixed rename to src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.1.inc.fixed diff --git a/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.2.inc b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.2.inc new file mode 100644 index 0000000000..6fd197e569 --- /dev/null +++ b/src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.2.inc @@ -0,0 +1,7 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 5 => 1, - 10 => 1, - 12 => 1, - 13 => 1, - 14 => 1, - 15 => 1, - 28 => 1, - 76 => 1, - 87 => 1, - 103 => 1, - 109 => 1, - 112 => 1, - 122 => 1, - 123 => 2, - 124 => 2, - 125 => 1, - 126 => 1, - 137 => 1, - 138 => 1, - 139 => 1, - 152 => 1, - 155 => 1, - 165 => 1, - 172 => 1, - 183 => 1, - 190 => 2, - 206 => 1, - 234 => 1, - 272 => 1, - 313 => 1, - 317 => 1, - 327 => 1, - 329 => 1, - 332 => 1, - 344 => 1, - 343 => 1, - 345 => 1, - 346 => 1, - 360 => 1, - 361 => 1, - 363 => 1, - 364 => 1, - 406 => 1, - 417 => 1, - 456 => 1, - 466 => 1, - 474 => 1, - 476 => 1, - 486 => 1, - 502 => 1, - 521 => 1, - 523 => 1, - 533 => 1, - 545 => 1, - ]; + switch ($testFile) { + case 'FunctionCommentUnitTest.1.inc': + return [ + 5 => 1, + 10 => 1, + 12 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 28 => 1, + 76 => 1, + 87 => 1, + 103 => 1, + 109 => 1, + 112 => 1, + 122 => 1, + 123 => 2, + 124 => 2, + 125 => 1, + 126 => 1, + 137 => 1, + 138 => 1, + 139 => 1, + 152 => 1, + 155 => 1, + 165 => 1, + 172 => 1, + 183 => 1, + 190 => 2, + 206 => 1, + 234 => 1, + 272 => 1, + 313 => 1, + 317 => 1, + 327 => 1, + 329 => 1, + 332 => 1, + 344 => 1, + 343 => 1, + 345 => 1, + 346 => 1, + 360 => 1, + 361 => 1, + 363 => 1, + 364 => 1, + 406 => 1, + 417 => 1, + 456 => 1, + 466 => 1, + 474 => 1, + 476 => 1, + 486 => 1, + 502 => 1, + 521 => 1, + 523 => 1, + 533 => 1, + 545 => 1, + ]; + + case 'FunctionCommentUnitTest.2.inc': + return [ + 7 => 1, + ]; + + case 'FunctionCommentUnitTest.3.inc': + return [ + 10 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.inc b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc similarity index 100% rename from src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.inc rename to src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc diff --git a/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc new file mode 100644 index 0000000000..27b95be4fa --- /dev/null +++ b/src/Standards/PEAR/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc @@ -0,0 +1,5 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 11 => 1, - 12 => 1, - 13 => 1, - 14 => 1, - 15 => 1, - 16 => 1, - 17 => 2, - 18 => 2, - 19 => 2, - 20 => 2, - 24 => 1, - 25 => 1, - 26 => 1, - 27 => 1, - 28 => 1, - 29 => 1, - 30 => 2, - 31 => 2, - 32 => 2, - 33 => 2, - 35 => 1, - 36 => 1, - 37 => 2, - 38 => 2, - 39 => 2, - 40 => 2, - 43 => 1, - 44 => 1, - 45 => 1, - 46 => 1, - 50 => 1, - 51 => 1, - 52 => 1, - 53 => 1, - 56 => 1, - 57 => 1, - 58 => 1, - 59 => 1, - 67 => 1, - 68 => 1, - 69 => 1, - 70 => 1, - 71 => 1, - 72 => 1, - 73 => 2, - 74 => 2, - 75 => 2, - 76 => 2, - 80 => 1, - 81 => 1, - 82 => 1, - 83 => 1, - 86 => 1, - 87 => 1, - 88 => 1, - 89 => 1, - 95 => 1, - 96 => 1, - 97 => 1, - 98 => 1, - 99 => 1, - 100 => 1, - 101 => 2, - 102 => 2, - 103 => 2, - 104 => 2, - 123 => 1, - 125 => 1, - 126 => 2, - 129 => 1, - 130 => 1, - 131 => 1, - 132 => 1, - 133 => 1, - 134 => 1, - 135 => 1, - 136 => 1, - 137 => 1, - 138 => 1, - 139 => 1, - 140 => 3, - 141 => 1, - 143 => 1, - 144 => 1, - 145 => 3, - 147 => 2, - 148 => 1, - 149 => 1, - 181 => 1, - 201 => 1, - 203 => 1, - 204 => 2, - 207 => 2, - 212 => 1, - 213 => 1, - 214 => 1, - 235 => 1, - 236 => 2, - 239 => 1, - 242 => 1, - ]; + switch ($testFile) { + case 'ValidFunctionNameUnitTest.1.inc': + return [ + 11 => 1, + 12 => 1, + 13 => 1, + 14 => 1, + 15 => 1, + 16 => 1, + 17 => 2, + 18 => 2, + 19 => 2, + 20 => 2, + 24 => 1, + 25 => 1, + 26 => 1, + 27 => 1, + 28 => 1, + 29 => 1, + 30 => 2, + 31 => 2, + 32 => 2, + 33 => 2, + 35 => 1, + 36 => 1, + 37 => 2, + 38 => 2, + 39 => 2, + 40 => 2, + 43 => 1, + 44 => 1, + 45 => 1, + 46 => 1, + 50 => 1, + 51 => 1, + 52 => 1, + 53 => 1, + 56 => 1, + 57 => 1, + 58 => 1, + 59 => 1, + 67 => 1, + 68 => 1, + 69 => 1, + 70 => 1, + 71 => 1, + 72 => 1, + 73 => 2, + 74 => 2, + 75 => 2, + 76 => 2, + 80 => 1, + 81 => 1, + 82 => 1, + 83 => 1, + 86 => 1, + 87 => 1, + 88 => 1, + 89 => 1, + 95 => 1, + 96 => 1, + 97 => 1, + 98 => 1, + 99 => 1, + 100 => 1, + 101 => 2, + 102 => 2, + 103 => 2, + 104 => 2, + 123 => 1, + 125 => 1, + 126 => 2, + 129 => 1, + 130 => 1, + 131 => 1, + 132 => 1, + 133 => 1, + 134 => 1, + 135 => 1, + 136 => 1, + 137 => 1, + 138 => 1, + 139 => 1, + 140 => 3, + 141 => 1, + 143 => 1, + 144 => 1, + 145 => 3, + 147 => 2, + 148 => 1, + 149 => 1, + 181 => 1, + 201 => 1, + 203 => 1, + 204 => 2, + 207 => 2, + 212 => 1, + 213 => 1, + 214 => 1, + 235 => 1, + 236 => 2, + 239 => 1, + 242 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php b/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php index 0cf1022f05..068d2a24a9 100644 --- a/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php +++ b/src/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php @@ -40,8 +40,8 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { - // Ignore closures. + if ($methodName === '') { + // Ignore live coding. return; } @@ -58,9 +58,9 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop $testName = ltrim($methodName, '_'); if ($testName !== '' && Common::isCamelCaps($testName, false, true, false) === false) { $error = 'Method name "%s" is not in camel caps format'; - $className = $phpcsFile->getDeclarationName($currScope); - if (isset($className) === false) { - $className = '[Anonymous Class]'; + $className = '[Anonymous Class]'; + if ($tokens[$currScope]['code'] !== T_ANON_CLASS) { + $className = $phpcsFile->getDeclarationName($currScope); } $errorData = [$className.'::'.$methodName]; diff --git a/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.inc b/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.1.inc similarity index 100% rename from src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.inc rename to src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.1.inc diff --git a/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.2.inc b/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.2.inc new file mode 100644 index 0000000000..b39ba26804 --- /dev/null +++ b/src/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.2.inc @@ -0,0 +1,9 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 6 => 1, - 7 => 1, - 11 => 1, - 12 => 1, - 13 => 1, - 17 => 1, - 21 => 1, - 25 => 1, - 26 => 1, - 77 => 1, - ]; + switch ($testFile) { + case 'CamelCapsMethodNameUnitTest.1.inc': + return [ + 6 => 1, + 7 => 1, + 11 => 1, + 12 => 1, + 13 => 1, + 17 => 1, + 21 => 1, + 25 => 1, + 26 => 1, + 77 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php index 6500d04681..d70660886e 100644 --- a/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Methods/MethodDeclarationSniff.php @@ -49,8 +49,8 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { - // Ignore closures. + if ($methodName === '') { + // Ignore live coding. return; } diff --git a/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.1.inc similarity index 100% rename from src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.inc rename to src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.1.inc diff --git a/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.1.inc.fixed similarity index 100% rename from src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.inc.fixed rename to src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.1.inc.fixed diff --git a/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.2.inc b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.2.inc new file mode 100644 index 0000000000..f662fb4028 --- /dev/null +++ b/src/Standards/PSR2/Tests/Methods/MethodDeclarationUnitTest.2.inc @@ -0,0 +1,9 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 9 => 1, - 11 => 1, - 13 => 1, - 15 => 3, - 24 => 1, - 34 => 1, - 36 => 1, - 38 => 1, - 40 => 3, - 50 => 1, - 52 => 1, - 54 => 1, - 56 => 3, - 63 => 2, - 73 => 1, - ]; + switch ($testFile) { + case 'MethodDeclarationUnitTest.1.inc': + return [ + 9 => 1, + 11 => 1, + 13 => 1, + 15 => 3, + 24 => 1, + 34 => 1, + 36 => 1, + 38 => 1, + 40 => 3, + 50 => 1, + 52 => 1, + 54 => 1, + 56 => 3, + 63 => 2, + 73 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -57,18 +65,26 @@ public function getErrorList() * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * + * @param string $testFile The name of the test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 5 => 1, - 21 => 1, - 30 => 1, - 46 => 1, - 63 => 1, - 70 => 1, - ]; + switch ($testFile) { + case 'MethodDeclarationUnitTest.1.inc': + return [ + 5 => 1, + 21 => 1, + 30 => 1, + 46 => 1, + 63 => 1, + 70 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php b/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php index 346534ba4b..31af5f3a47 100644 --- a/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php +++ b/src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php @@ -55,7 +55,7 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); $ooName = $phpcsFile->getDeclarationName($stackPtr); - if ($ooName === null) { + if ($ooName === '') { // Probably parse error/live coding. return; } diff --git a/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php index f9cffbb174..a9fd5d39b8 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/ClosingDeclarationCommentSniff.php @@ -67,6 +67,11 @@ public function process(File $phpcsFile, $stackPtr) } $decName = $phpcsFile->getDeclarationName($stackPtr); + if ($decName === '') { + // Parse error or live coding. + return; + } + $comment = '//end '.$decName.'()'; } else if ($tokens[$stackPtr]['code'] === T_CLASS) { $comment = '//end class'; diff --git a/src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php b/src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php index af3d902ecb..1c3db67c0d 100644 --- a/src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php @@ -43,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr) if (empty($tokens[$stackPtr]['conditions']) === true) { $functionName = $phpcsFile->getDeclarationName($stackPtr); - if ($functionName === null) { + if ($functionName === '') { return; } diff --git a/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php b/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php index 2450ba7fdb..90ceaf2212 100644 --- a/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php +++ b/src/Standards/Squiz/Sniffs/NamingConventions/ValidFunctionNameSniff.php @@ -29,7 +29,7 @@ class ValidFunctionNameSniff extends PEARValidFunctionNameSniff protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) { $functionName = $phpcsFile->getDeclarationName($stackPtr); - if ($functionName === null) { + if ($functionName === '') { return; } diff --git a/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php b/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php index 39ed5b0dd4..faad52e2b4 100644 --- a/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php +++ b/src/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php @@ -49,8 +49,8 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop } $methodName = $phpcsFile->getDeclarationName($stackPtr); - if ($methodName === null) { - // Ignore closures. + if ($methodName === '') { + // Ignore live coding. return; } diff --git a/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.inc rename to src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.2.inc b/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.2.inc new file mode 100644 index 0000000000..18ab1e7940 --- /dev/null +++ b/src/Standards/Squiz/Tests/Commenting/ClassCommentUnitTest.2.inc @@ -0,0 +1,7 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 2 => 1, - 15 => 1, - 31 => 1, - 54 => 1, - 143 => 1, - 145 => 1, - ]; + switch ($testFile) { + case 'ClassCommentUnitTest.1.inc': + return [ + 2 => 1, + 15 => 1, + 31 => 1, + 54 => 1, + 143 => 1, + 145 => 1, + ]; + + case 'ClassCommentUnitTest.2.inc': + return [ + 7 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() @@ -48,17 +61,25 @@ public function getErrorList() * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * + * @param string $testFile The name of the test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 29 => 1, - 30 => 1, - 50 => 1, - 66 => 1, - 67 => 1, - ]; + switch ($testFile) { + case 'ClassCommentUnitTest.1.inc': + return [ + 29 => 1, + 30 => 1, + 50 => 1, + 66 => 1, + 67 => 1, + ]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.6.inc b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.6.inc new file mode 100644 index 0000000000..a996c81aa8 --- /dev/null +++ b/src/Standards/Squiz/Tests/Commenting/ClosingDeclarationCommentUnitTest.6.inc @@ -0,0 +1,7 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - $errors = [ - 5 => 1, - 10 => 3, - 12 => 2, - 13 => 2, - 14 => 1, - 15 => 1, - 17 => 3, - 28 => 1, - 43 => 1, - 76 => 1, - 87 => 1, - 103 => 1, - 109 => 1, - 112 => 1, - 122 => 1, - 123 => 3, - 124 => 2, - 125 => 1, - 126 => 1, - 128 => 1, - 137 => 4, - 138 => 4, - 139 => 4, - 143 => 3, - 155 => 1, - 159 => 1, - 161 => 2, - 166 => 1, - 173 => 1, - 183 => 1, - 190 => 2, - 193 => 2, - 196 => 1, - 199 => 2, - 201 => 1, - 210 => 1, - 211 => 1, - 222 => 1, - 223 => 1, - 224 => 1, - 225 => 1, - 226 => 1, - 227 => 1, - 230 => 2, - 232 => 7, - 246 => 1, - 248 => 4, - 261 => 1, - 263 => 1, - 276 => 1, - 277 => 1, - 278 => 1, - 279 => 1, - 280 => 1, - 281 => 1, - 284 => 1, - 286 => 7, - 294 => 1, - 302 => 1, - 312 => 1, - 358 => 1, - 359 => 2, - 363 => 3, - 372 => 1, - 373 => 1, - 377 => 1, - 387 => 1, - 407 => 1, - 441 => 1, - 500 => 1, - 526 => 1, - 548 => 1, - 575 => 2, - 627 => 2, - 641 => 1, - 669 => 1, - 688 => 1, - 744 => 1, - 748 => 1, - 767 => 1, - 789 => 1, - 792 => 1, - 794 => 1, - 797 => 1, - 828 => 1, - 840 => 1, - 852 => 1, - 864 => 1, - 886 => 1, - 888 => 1, - 890 => 1, - 978 => 1, - 997 => 1, - 1002 => 1, - 1004 => 2, - 1006 => 1, - 1029 => 1, - 1053 => 1, - 1058 => 2, - 1069 => 1, - 1070 => 1, - 1071 => 1, - 1075 => 6, - 1080 => 2, - 1083 => 1, - 1084 => 1, - 1085 => 1, - 1089 => 3, - 1093 => 4, - 1100 => 1, - 1101 => 1, - 1102 => 1, - 1103 => 1, - 1107 => 8, - 1123 => 1, - 1124 => 1, - 1125 => 1, - 1129 => 3, - 1138 => 1, - 1139 => 1, - 1144 => 1, - 1145 => 1, - 1151 => 1, - 1154 => 1, - 1160 => 1, - ]; + switch ($testFile) { + case 'FunctionCommentUnitTest.1.inc': + $errors = [ + 5 => 1, + 10 => 3, + 12 => 2, + 13 => 2, + 14 => 1, + 15 => 1, + 17 => 3, + 28 => 1, + 43 => 1, + 76 => 1, + 87 => 1, + 103 => 1, + 109 => 1, + 112 => 1, + 122 => 1, + 123 => 3, + 124 => 2, + 125 => 1, + 126 => 1, + 128 => 1, + 137 => 4, + 138 => 4, + 139 => 4, + 143 => 3, + 155 => 1, + 159 => 1, + 161 => 2, + 166 => 1, + 173 => 1, + 183 => 1, + 190 => 2, + 193 => 2, + 196 => 1, + 199 => 2, + 201 => 1, + 210 => 1, + 211 => 1, + 222 => 1, + 223 => 1, + 224 => 1, + 225 => 1, + 226 => 1, + 227 => 1, + 230 => 2, + 232 => 7, + 246 => 1, + 248 => 4, + 261 => 1, + 263 => 1, + 276 => 1, + 277 => 1, + 278 => 1, + 279 => 1, + 280 => 1, + 281 => 1, + 284 => 1, + 286 => 7, + 294 => 1, + 302 => 1, + 312 => 1, + 358 => 1, + 359 => 2, + 363 => 3, + 372 => 1, + 373 => 1, + 377 => 1, + 387 => 1, + 407 => 1, + 441 => 1, + 500 => 1, + 526 => 1, + 548 => 1, + 575 => 2, + 627 => 2, + 641 => 1, + 669 => 1, + 688 => 1, + 744 => 1, + 748 => 1, + 767 => 1, + 789 => 1, + 792 => 1, + 794 => 1, + 797 => 1, + 828 => 1, + 840 => 1, + 852 => 1, + 864 => 1, + 886 => 1, + 888 => 1, + 890 => 1, + 978 => 1, + 997 => 1, + 1002 => 1, + 1004 => 2, + 1006 => 1, + 1029 => 1, + 1053 => 1, + 1058 => 2, + 1069 => 1, + 1070 => 1, + 1071 => 1, + 1075 => 6, + 1080 => 2, + 1083 => 1, + 1084 => 1, + 1085 => 1, + 1089 => 3, + 1093 => 4, + 1100 => 1, + 1101 => 1, + 1102 => 1, + 1103 => 1, + 1107 => 8, + 1123 => 1, + 1124 => 1, + 1125 => 1, + 1129 => 3, + 1138 => 1, + 1139 => 1, + 1144 => 1, + 1145 => 1, + 1151 => 1, + 1154 => 1, + 1160 => 1, + ]; + + // Mixed type hints only work from PHP 8.0 onwards. + if (PHP_VERSION_ID >= 80000) { + $errors[265] = 1; + $errors[459] = 1; + $errors[893] = 3; + } else { + $errors[1023] = 1; + } + return $errors; - // Mixed type hints only work from PHP 8.0 onwards. - if (PHP_VERSION_ID >= 80000) { - $errors[265] = 1; - $errors[459] = 1; - $errors[893] = 3; - } else { - $errors[1023] = 1; - } + case 'FunctionCommentUnitTest.2.inc': + return [ + 8 => 1, + ]; - return $errors; + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.inc b/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.inc rename to src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.2.inc b/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.2.inc new file mode 100644 index 0000000000..27b95be4fa --- /dev/null +++ b/src/Standards/Squiz/Tests/Functions/GlobalFunctionUnitTest.2.inc @@ -0,0 +1,5 @@ + */ - public function getWarningList() + public function getWarningList($testFile='') { - return [2 => 1]; + switch ($testFile) { + case 'GlobalFunctionUnitTest.1.inc': + return [2 => 1]; + + default: + return []; + }//end switch }//end getWarningList() diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.inc b/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.inc rename to src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc b/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc new file mode 100644 index 0000000000..e06219f0dc --- /dev/null +++ b/src/Standards/Squiz/Tests/NamingConventions/ValidFunctionNameUnitTest.2.inc @@ -0,0 +1,5 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 4 => 1, - 5 => 1, - 6 => 1, - 7 => 1, - 8 => 1, - 9 => 1, - 11 => 1, - 12 => 1, - 13 => 1, - 14 => 2, - ]; + switch ($testFile) { + case 'ValidFunctionNameUnitTest.1.inc': + return [ + 4 => 1, + 5 => 1, + 6 => 1, + 7 => 1, + 8 => 1, + 9 => 1, + 11 => 1, + 12 => 1, + 13 => 1, + 14 => 2, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc b/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.1.inc similarity index 100% rename from src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc rename to src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.1.inc diff --git a/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.2.inc b/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.2.inc new file mode 100644 index 0000000000..9836049d56 --- /dev/null +++ b/src/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.2.inc @@ -0,0 +1,9 @@ + */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 6 => 1, - 30 => 1, - 39 => 1, - 46 => 1, - ]; + switch ($testFile) { + case 'MethodScopeUnitTest.1.inc': + return [ + 6 => 1, + 30 => 1, + 39 => 1, + 46 => 1, + ]; + + default: + return []; + }//end switch }//end getErrorList() diff --git a/tests/Core/File/GetDeclarationNameParseError1Test.php b/tests/Core/File/GetDeclarationNameParseError1Test.php index ee70c4b0f3..3c90169e5b 100644 --- a/tests/Core/File/GetDeclarationNameParseError1Test.php +++ b/tests/Core/File/GetDeclarationNameParseError1Test.php @@ -21,17 +21,17 @@ final class GetDeclarationNameParseError1Test extends AbstractMethodTestCase /** - * Test receiving "null" in case of a parse error. + * Test receiving an empty string in case of a parse error. * * @return void */ - public function testGetDeclarationNameNull() + public function testGetDeclarationName() { $target = $this->getTargetToken('/* testLiveCoding */', T_FUNCTION); $result = self::$phpcsFile->getDeclarationName($target); - $this->assertNull($result); + $this->assertSame('', $result); - }//end testGetDeclarationNameNull() + }//end testGetDeclarationName() }//end class diff --git a/tests/Core/File/GetDeclarationNameParseError2Test.php b/tests/Core/File/GetDeclarationNameParseError2Test.php index ad036a8d0d..4d8029b7d5 100644 --- a/tests/Core/File/GetDeclarationNameParseError2Test.php +++ b/tests/Core/File/GetDeclarationNameParseError2Test.php @@ -21,17 +21,17 @@ final class GetDeclarationNameParseError2Test extends AbstractMethodTestCase /** - * Test receiving "null" in case of a parse error. + * Test receiving an empty string in case of a parse error. * * @return void */ - public function testGetDeclarationNameNull() + public function testGetDeclarationName() { $target = $this->getTargetToken('/* testLiveCoding */', T_FUNCTION); $result = self::$phpcsFile->getDeclarationName($target); - $this->assertNull($result); + $this->assertSame('', $result); - }//end testGetDeclarationNameNull() + }//end testGetDeclarationName() }//end class diff --git a/tests/Core/File/GetDeclarationNameTest.php b/tests/Core/File/GetDeclarationNameTest.php index 6371aacf61..ed07605773 100644 --- a/tests/Core/File/GetDeclarationNameTest.php +++ b/tests/Core/File/GetDeclarationNameTest.php @@ -10,6 +10,7 @@ namespace PHP_CodeSniffer\Tests\Core\File; use PHP_CodeSniffer\Tests\Core\AbstractMethodTestCase; +use PHP_CodeSniffer\Util\Tokens; /** * Tests for the \PHP_CodeSniffer\Files\File:getDeclarationName method. @@ -23,47 +24,38 @@ final class GetDeclarationNameTest extends AbstractMethodTestCase /** * Test receiving an expected exception when a non-supported token is passed. * - * @return void - */ - public function testInvalidTokenPassed() - { - $this->expectRunTimeException('Token type "T_STRING" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM'); - - $target = $this->getTargetToken('/* testInvalidTokenPassed */', T_STRING); - self::$phpcsFile->getDeclarationName($target); - - }//end testInvalidTokenPassed() - - - /** - * Test receiving "null" when passed an anonymous construct or in case of a parse error. - * * @param string $testMarker The comment which prefaces the target token in the test file. * @param int|string $targetType Token type of the token to get as stackPtr. * - * @dataProvider dataGetDeclarationNameNull + * @dataProvider dataInvalidTokenPassed * * @return void */ - public function testGetDeclarationNameNull($testMarker, $targetType) + public function testInvalidTokenPassed($testMarker, $targetType) { + $tokenName = Tokens::tokenName($targetType); + $this->expectRunTimeException('Token type "'.$tokenName.'" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM'); + $target = $this->getTargetToken($testMarker, $targetType); - $result = self::$phpcsFile->getDeclarationName($target); - $this->assertNull($result); + self::$phpcsFile->getDeclarationName($target); - }//end testGetDeclarationNameNull() + }//end testInvalidTokenPassed() /** * Data provider. * - * @see testGetDeclarationNameNull() For the array format. + * @see testGetInvalidTokenPassed() For the array format. * * @return array> */ - public static function dataGetDeclarationNameNull() + public static function dataInvalidTokenPassed() { return [ + 'unsupported token T_STRING' => [ + 'testMarker' => '/* testInvalidTokenPassed */', + 'targetType' => T_STRING, + ], 'closure' => [ 'testMarker' => '/* testClosure */', 'targetType' => T_CLOSURE, @@ -86,7 +78,7 @@ public static function dataGetDeclarationNameNull() ], ]; - }//end dataGetDeclarationNameNull() + }//end dataInvalidTokenPassed() /**