TASK: Refactor sniffs
* Move generic logic to abstract class. * As not only UseSniff but also all others have the same logic, if method is not overwritten. * Therefore all other sniffs should work with new feature of removed extension. Relates: #44
This commit is contained in:
parent
3034f3fec4
commit
fda1921d50
2 changed files with 62 additions and 32 deletions
|
@ -62,19 +62,69 @@ abstract class AbstractClassnameChecker implements PhpCsSniff
|
|||
*/
|
||||
public function process(PhpCsFile $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
if ($this->shouldLookBefore()) {
|
||||
$classnamePosition = $phpcsFile->findPrevious(T_STRING, $stackPtr);
|
||||
} else {
|
||||
$classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr);
|
||||
}
|
||||
|
||||
if ($classnamePosition === false) {
|
||||
try {
|
||||
if ($this->shouldLookBefore()) {
|
||||
list($classnamePosition, $classname) = $this->getBefore($phpcsFile, $stackPtr);
|
||||
} else {
|
||||
list($classnamePosition, $classname) = $this->getAfter($phpcsFile, $stackPtr);
|
||||
}
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
$classname = $tokens[$classnamePosition]['content'];
|
||||
$this->processFeatures($phpcsFile, $classnamePosition, $classname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get position and classname before current stack pointer.
|
||||
*
|
||||
* @param PhpCsFile $phpcsFile
|
||||
* @param int $stackPtr The position in the stack where
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getBefore(PhpCsFile $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
$classnamePosition = $phpcsFile->findPrevious(T_STRING, $stackPtr);
|
||||
if ($classnamePosition === false) {
|
||||
throw new \UnexpectedValueException('Could not find start of classname.', 1494319966);
|
||||
}
|
||||
|
||||
$classname = $tokens[$classnamePosition]['content'];
|
||||
|
||||
return [
|
||||
$classnamePosition,
|
||||
$classname
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get position and classname after current stack pointer.
|
||||
*
|
||||
* @param PhpCsFile $phpcsFile
|
||||
* @param int $stackPtr The position in the stack where
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAfter(PhpCsFile $phpcsFile, $stackPtr)
|
||||
{
|
||||
$classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr);
|
||||
if ($classnamePosition === false) {
|
||||
throw new \UnexpectedValueException('Could not find start of classname.', 1494319665);
|
||||
}
|
||||
|
||||
$end = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $classnamePosition, null, true, null, true);
|
||||
if ($end === false) {
|
||||
throw new \UnexpectedValueException('Could not find end of classname.', 1494319651);
|
||||
}
|
||||
|
||||
$classname = $phpcsFile->getTokensAsString($classnamePosition, $end - $classnamePosition);
|
||||
|
||||
return [
|
||||
$classnamePosition,
|
||||
$classname
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,14 +19,10 @@
|
|||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
use PHP_CodeSniffer_File as PhpCsFile;
|
||||
use PHP_CodeSniffer_Sniff as PhpCsSniff;
|
||||
use Typo3Update\Feature\FeaturesSupport;
|
||||
use Typo3Update\Sniffs\Classname\AbstractClassnameChecker;
|
||||
|
||||
class Typo3Update_Sniffs_Classname_UseSniff implements PhpCsSniff
|
||||
class Typo3Update_Sniffs_Classname_UseSniff extends AbstractClassnameChecker
|
||||
{
|
||||
use FeaturesSupport;
|
||||
|
||||
/**
|
||||
* Returns the token types that this sniff is interested in.
|
||||
*
|
||||
|
@ -36,20 +32,4 @@ class Typo3Update_Sniffs_Classname_UseSniff implements PhpCsSniff
|
|||
{
|
||||
return [T_USE];
|
||||
}
|
||||
|
||||
public function process(PhpCsFile $phpcsFile, $stackPtr)
|
||||
{
|
||||
$start = $phpcsFile->findNext(T_STRING, $stackPtr);
|
||||
if ($start === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$end = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $start, null, true, null, true);
|
||||
if ($end === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$classname = $phpcsFile->getTokensAsString($start, $end - $start);
|
||||
$this->processFeatures($phpcsFile, $start, $classname);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue