Merge pull request #29 from DanielSiepmann/feature/refactor-string-classname-replacement

TASK: Refactor code
This commit is contained in:
simonhard 2017-03-21 09:38:18 +01:00 committed by GitHub
commit b080696803
3 changed files with 27 additions and 25 deletions

View file

@ -139,7 +139,7 @@ trait ClassnameCheckerTrait
*/ */
public function addFixableError(PhpcsFile $phpcsFile, $classnamePosition, $classname) public function addFixableError(PhpcsFile $phpcsFile, $classnamePosition, $classname)
{ {
$classname = trim($classname, '\\'); $classname = trim($classname, '\\\'"'); // Remove trailing slash, and quotes.
$this->addMaybeWarning($phpcsFile, $classnamePosition, $classname); $this->addMaybeWarning($phpcsFile, $classnamePosition, $classname);
if ($this->isLegacyClassname($classname) === false) { if ($this->isLegacyClassname($classname) === false) {
@ -210,4 +210,26 @@ trait ClassnameCheckerTrait
{ {
return $classname; return $classname;
} }
/**
* Use this inside your getTokenForReplacement if $classname is inside a string.
* Strings will be converted to single quotes.
*
* @param string $classname
* @return string
*/
protected function getTokenReplacementForString($classname)
{
$stringSign = $this->originalTokenContent[0];
$token = explode($stringSign, $this->originalTokenContent);
$token[1] = $classname;
// Migrate double quote to single quote.
// This way no escaping of backslashes in class names is necessary.
if ($stringSign === '"') {
$stringSign = "'";
}
return implode($stringSign, $token);
}
} }

View file

@ -70,7 +70,7 @@ class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff imp
return; return;
} }
$classname = trim($tokens[$classnamePosition]['content'], '\'"'); $classname = $tokens[$classnamePosition]['content'];
$this->originalTokenContent = $tokens[$classnamePosition]['content']; $this->originalTokenContent = $tokens[$classnamePosition]['content'];
$this->addFixableError($phpcsFile, $classnamePosition, $classname); $this->addFixableError($phpcsFile, $classnamePosition, $classname);
} }
@ -83,16 +83,6 @@ class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff imp
*/ */
protected function getTokenForReplacement($classname) protected function getTokenForReplacement($classname)
{ {
$stringSign = $this->originalTokenContent[0]; return $this->getTokenReplacementForString($classname);
$token = explode($stringSign, $this->originalTokenContent);
$token[1] = $classname;
// Migrate double quote to single quote.
// This way no escaping of backslashes in class names is necessary.
if ($stringSign === '"') {
$stringSign = "'";
}
return implode($stringSign, $token);
} }
} }

View file

@ -74,7 +74,7 @@ class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithObjectManagerSniff im
); );
} }
$classname = trim($tokens[$classnamePosition]['content'], '\'"'); $classname = $tokens[$classnamePosition]['content'];
$this->originalTokenContent = $tokens[$classnamePosition]['content']; $this->originalTokenContent = $tokens[$classnamePosition]['content'];
$this->addFixableError($phpcsFile, $classnamePosition, $classname); $this->addFixableError($phpcsFile, $classnamePosition, $classname);
} }
@ -87,16 +87,6 @@ class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithObjectManagerSniff im
*/ */
protected function getTokenForReplacement($classname) protected function getTokenForReplacement($classname)
{ {
$stringSign = $this->originalTokenContent[0]; return $this->getTokenReplacementForString($classname);
$token = explode($stringSign, $this->originalTokenContent);
$token[1] = $classname;
// Migrate double quote to single quote.
// This way no escaping of backslashes in class names is necessary.
if ($stringSign === '"') {
$stringSign = "'";
}
return implode($stringSign, $token);
} }
} }