Merge pull request #18 from DanielSiepmann/hotfix/14-double-backslash

BUGFIX: Leading namespace separator in replaced classnames
This commit is contained in:
Daniel Siepmann 2017-03-16 17:33:06 +01:00 committed by GitHub
commit 5fd9083b01

View file

@ -114,6 +114,7 @@ trait ClassnameCheckerTrait
*/
public function addFixableError(PhpcsFile $phpcsFile, $classnamePosition, $classname)
{
$classname = trim($classname, '\\');
$this->addMaybeWarning($phpcsFile, $classnamePosition, $classname);
if ($this->isLegacyClassname($classname) === false) {
@ -128,10 +129,7 @@ trait ClassnameCheckerTrait
);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(
$classnamePosition,
$this->getTokenForReplacement('\\' . $this->getNewClassname($classname))
);
$this->replaceLegacyClassname($phpcsFile, $classnamePosition, $classname);
}
}
@ -156,6 +154,26 @@ trait ClassnameCheckerTrait
);
}
/**
* Replaces the classname at $classnamePosition with $classname in $phpcsFile.
*
* @param PhpcsFile $phpcsFile
* @param int $classnamePosition
* @param string $classname
*/
private function replaceLegacyClassname(PhpcsFile $phpcsFile, $classnamePosition, $classname)
{
$prefix = '\\';
if ($phpcsFile->getTokens()[$classnamePosition -1]['code'] === T_NS_SEPARATOR) {
$prefix = '';
}
$phpcsFile->fixer->replaceToken(
$classnamePosition,
$this->getTokenForReplacement($prefix . $this->getNewClassname($classname))
);
}
/**
* String to use for replacing / fixing the token.
* Default is class name itself, can be overwritten in sniff for special behaviour.