TASK: Refactor code

* As we sometimes have to fix classnames inside a string, move this
  common work to trait.
* Also make classname "free" of string quotes inside the check, not the
  concrete sniffs.
This commit is contained in:
Daniel Siepmann 2017-03-21 08:59:26 +01:00
parent e6e372f8b1
commit ba2ed2cf5f
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
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);
} }
} }