The format of the array is:
array(
'is_abstract' => false, // true if the abstract keyword was found.
'is_final' => false, // true if the final keyword was found.
);
public getClassProperties ( integer $stackPtr ) : array | ||
$stackPtr | integer | The position in the stack of the T_CLASS token to acquire the properties for. |
return | array |
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile The current file being processed. * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $className = $phpcsFile->findNext(T_STRING, $stackPtr); $name = trim($tokens[$className]['content']); $errorData = array(ucfirst($tokens[$stackPtr]['content'])); $classProperties = $phpcsFile->getClassProperties($stackPtr); $startsWithAbstract = substr($name, 0, 8) == 'Abstract'; if ($classProperties['is_abstract'] && !$startsWithAbstract) { $error = '%s name must begin with "Abstract"'; $phpcsFile->addError($error, $stackPtr, 'StartWithAbstract', $errorData); } if (!$classProperties['is_abstract'] && $startsWithAbstract) { $error = '%s name must not begin with "Abstract"'; $phpcsFile->addError($error, $stackPtr, 'StartNotWithAbstract', $errorData); } }
/** * Processes this test, when one of its tokens is encountered. * * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. * @param int $stackPtr The position of the current token in the stack passed in $tokens. * * @return void */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $properties = $phpcsFile->getClassProperties($stackPtr); if (!$properties['is_abstract']) { return; } $tokens = $phpcsFile->getTokens(); $line = $tokens[$stackPtr]['line']; while ($tokens[$stackPtr]['line'] == $line) { if ('T_STRING' == $tokens[$stackPtr]['type']) { if (substr($tokens[$stackPtr]['content'], 0, 8) != 'Abstract') { $phpcsFile->addError('Class name is not prefixed with "Abstract"', $stackPtr); } break; } $stackPtr++; } }