From 14c82e4a10453e1308b9509cffa17bc8059d16a8 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 25 Apr 2017 14:23:49 +0200 Subject: [PATCH] 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 --- .../Feature/LegacyClassnameFeature.php | 58 +++++++------------ .../Classname/IsACallSniff/Expected.diff | 10 +++- .../Classname/IsACallSniff/Expected.json | 24 +++++++- .../IsACallSniff/InputFileForIssues.php | 6 ++ 4 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/Standards/Typo3Update/Feature/LegacyClassnameFeature.php b/src/Standards/Typo3Update/Feature/LegacyClassnameFeature.php index c5d27f5..269650c 100644 --- a/src/Standards/Typo3Update/Feature/LegacyClassnameFeature.php +++ b/src/Standards/Typo3Update/Feature/LegacyClassnameFeature.php @@ -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; } diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/Expected.diff b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/Expected.diff index 8e2e341..d099979 100644 --- a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/Expected.diff +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/Expected.diff @@ -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 + } diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/Expected.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/Expected.json index 6ec4608..6795e14 100644 --- a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/Expected.json +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/Expected.json @@ -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 } } diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/InputFileForIssues.php b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/InputFileForIssues.php index 4265076..53a8a7b 100644 --- a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/InputFileForIssues.php +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/InputFileForIssues.php @@ -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 +}