BUGFIX: Leading namespace separator in replaced classnames
* First remove possible namespace seperators from class name to allow lookup. * Check whether we have to prefix the new classname with namespace seperator. Resolves: #14
This commit is contained in:
parent
1b2e72ec96
commit
0012c229ed
1 changed files with 22 additions and 4 deletions
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue