Unverified Commit fda1921d authored by Daniel Siepmann's avatar Daniel Siepmann
Browse files

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
parent 3034f3fe
......@@ -61,20 +61,70 @@ abstract class AbstractClassnameChecker implements PhpCsSniff
* @SuppressWarnings(PHPMD.ElseExpression) This is for performance reason.
*/
public function process(PhpCsFile $phpcsFile, $stackPtr)
{
try {
if ($this->shouldLookBefore()) {
list($classnamePosition, $classname) = $this->getBefore($phpcsFile, $stackPtr);
} else {
list($classnamePosition, $classname) = $this->getAfter($phpcsFile, $stackPtr);
}
} catch (\UnexpectedValueException $e) {
return;
}
$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();
if ($this->shouldLookBefore()) {
$classnamePosition = $phpcsFile->findPrevious(T_STRING, $stackPtr);
} else {
$classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr);
$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) {
return;
throw new \UnexpectedValueException('Could not find start of classname.', 1494319665);
}
$classname = $tokens[$classnamePosition]['content'];
$this->processFeatures($phpcsFile, $classnamePosition, $classname);
$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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment