TASK: Migrate IsACallSniff

* Add further tests with quotes.
* Handle prefix in strings.
* Remove old originalTokenContent which is no longer in use, as we use
  str_replace, introduced in earlier commit.

Relates: #72
This commit is contained in:
Daniel Siepmann 2017-04-25 14:23:49 +02:00
parent d12990f6e1
commit 14c82e4a10
4 changed files with 57 additions and 41 deletions

View file

@ -49,15 +49,6 @@ class LegacyClassnameFeature implements FeatureInterface
*/
protected $sniff;
/**
* Used by some sniffs to keep original token for replacement.
*
* E.g. when Token itself is a whole inline comment, and we just want to replace the classname within.
*
* @var string
*/
protected $originalTokenContent = '';
public function __construct(PhpCsSniff $sniff)
{
$this->sniff = $sniff;
@ -188,40 +179,22 @@ class LegacyClassnameFeature implements FeatureInterface
$classname
) {
$prefix = '\\';
if ($this->forceEmptyPrefix() || $phpcsFile->getTokens()[$classnamePosition -1]['code'] === T_NS_SEPARATOR) {
if ($this->useEmptyPrefix($phpcsFile, $classnamePosition)) {
$prefix = '';
}
$phpcsFile->fixer->replaceToken(
$classnamePosition,
str_replace(
$classname,
$prefix . $this->getNewClassname($classname),
$phpcsFile->getTokens()[$classnamePosition]['content']
)
$newClassname = str_replace(
$classname,
$prefix . $this->getNewClassname($classname),
$phpcsFile->getTokens()[$classnamePosition]['content']
);
}
/**
* 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 = "'";
// Handle double quotes, with special escaping.
if ($newClassname[0] === '"') {
$newClassname = '"\\\\' . str_replace('\\', '\\\\', ltrim(substr($newClassname, 1), '\\'));
}
return implode($stringSign, $token);
$phpcsFile->fixer->replaceToken($classnamePosition, $newClassname);
}
/**
@ -229,11 +202,22 @@ class LegacyClassnameFeature implements FeatureInterface
*
* @return bool
*/
protected function forceEmptyPrefix()
protected function useEmptyPrefix(PhpCsFile $phpcsFile, $classnamePosition)
{
// Use statements don't start with T_NS_SEPARATOR.
if (get_class($this->sniff) === \Typo3Update_Sniffs_Classname_UseSniff::class) {
return true;
}
// If T_NS_SEPARATOR is already present before, don't add again.
if ($phpcsFile->getTokens()[$classnamePosition -1]['code'] === T_NS_SEPARATOR) {
return true;
}
// If inside string starting with T_NS_SEPARATOR don't add again.
if (isset($phpcsFile->getTokens()[$classnamePosition]['content'][1])
&& $phpcsFile->getTokens()[$classnamePosition]['content'][1] === '\\'
) {
return true;
}
return false;
}

View file

@ -1,6 +1,6 @@
--- tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/InputFileForIssues.php
+++ PHP_CodeSniffer
@@ -23,9 +23,9 @@
@@ -23,15 +23,15 @@
if (is_a($a, t3lib_Singleton::class)) {
// do something
}
@ -12,3 +12,11 @@
+if (is_a($a, '\TYPO3\CMS\Core\SingletonInterface')) {
// do something
}
-if (is_a($a, "t3lib_Singleton")) {
+if (is_a($a, "\\TYPO3\\CMS\\Core\\SingletonInterface")) {
// do something
}
-if (is_a($a, "\\t3lib_Singleton")) {
+if (is_a($a, "\\TYPO3\\CMS\\Core\\SingletonInterface")) {
// do something
}

View file

@ -1,7 +1,7 @@
{
"files": {
"InputFileForIssues.php": {
"errors": 2,
"errors": 4,
"messages": [
{
"column": 14,
@ -20,14 +20,32 @@
"severity": 5,
"source": "Typo3Update.Classname.IsACall.legacyClassname",
"type": "ERROR"
},
{
"column": 14,
"fixable": true,
"line": 32,
"message": "Legacy classes are not allowed; found \"t3lib_Singleton\", use \"TYPO3\\CMS\\Core\\SingletonInterface\" instead",
"severity": 5,
"source": "Typo3Update.Classname.IsACall.legacyClassname",
"type": "ERROR"
},
{
"column": 14,
"fixable": true,
"line": 35,
"message": "Legacy classes are not allowed; found \"t3lib_Singleton\", use \"TYPO3\\CMS\\Core\\SingletonInterface\" instead",
"severity": 5,
"source": "Typo3Update.Classname.IsACall.legacyClassname",
"type": "ERROR"
}
],
"warnings": 0
}
},
"totals": {
"errors": 2,
"fixable": 2,
"errors": 4,
"fixable": 4,
"warnings": 0
}
}

View file

@ -29,3 +29,9 @@ if (is_a($a, 't3lib_Singleton')) {
if (is_a($a, '\t3lib_Singleton')) {
// do something
}
if (is_a($a, "t3lib_Singleton")) {
// do something
}
if (is_a($a, "\\t3lib_Singleton")) {
// do something
}