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:
parent
d12990f6e1
commit
14c82e4a10
4 changed files with 57 additions and 41 deletions
|
@ -49,15 +49,6 @@ class LegacyClassnameFeature implements FeatureInterface
|
||||||
*/
|
*/
|
||||||
protected $sniff;
|
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)
|
public function __construct(PhpCsSniff $sniff)
|
||||||
{
|
{
|
||||||
$this->sniff = $sniff;
|
$this->sniff = $sniff;
|
||||||
|
@ -188,40 +179,22 @@ class LegacyClassnameFeature implements FeatureInterface
|
||||||
$classname
|
$classname
|
||||||
) {
|
) {
|
||||||
$prefix = '\\';
|
$prefix = '\\';
|
||||||
if ($this->forceEmptyPrefix() || $phpcsFile->getTokens()[$classnamePosition -1]['code'] === T_NS_SEPARATOR) {
|
if ($this->useEmptyPrefix($phpcsFile, $classnamePosition)) {
|
||||||
$prefix = '';
|
$prefix = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$phpcsFile->fixer->replaceToken(
|
$newClassname = str_replace(
|
||||||
$classnamePosition,
|
$classname,
|
||||||
str_replace(
|
$prefix . $this->getNewClassname($classname),
|
||||||
$classname,
|
$phpcsFile->getTokens()[$classnamePosition]['content']
|
||||||
$prefix . $this->getNewClassname($classname),
|
|
||||||
$phpcsFile->getTokens()[$classnamePosition]['content']
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Handle double quotes, with special escaping.
|
||||||
* Use this inside your getTokenForReplacement if $classname is inside a string.
|
if ($newClassname[0] === '"') {
|
||||||
* Strings will be converted to single quotes.
|
$newClassname = '"\\\\' . str_replace('\\', '\\\\', ltrim(substr($newClassname, 1), '\\'));
|
||||||
*
|
|
||||||
* @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);
|
$phpcsFile->fixer->replaceToken($classnamePosition, $newClassname);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -229,11 +202,22 @@ class LegacyClassnameFeature implements FeatureInterface
|
||||||
*
|
*
|
||||||
* @return bool
|
* @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) {
|
if (get_class($this->sniff) === \Typo3Update_Sniffs_Classname_UseSniff::class) {
|
||||||
return true;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
--- tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/InputFileForIssues.php
|
--- tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/IsACallSniff/InputFileForIssues.php
|
||||||
+++ PHP_CodeSniffer
|
+++ PHP_CodeSniffer
|
||||||
@@ -23,9 +23,9 @@
|
@@ -23,15 +23,15 @@
|
||||||
if (is_a($a, t3lib_Singleton::class)) {
|
if (is_a($a, t3lib_Singleton::class)) {
|
||||||
// do something
|
// do something
|
||||||
}
|
}
|
||||||
|
@ -12,3 +12,11 @@
|
||||||
+if (is_a($a, '\TYPO3\CMS\Core\SingletonInterface')) {
|
+if (is_a($a, '\TYPO3\CMS\Core\SingletonInterface')) {
|
||||||
// do something
|
// 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
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"files": {
|
"files": {
|
||||||
"InputFileForIssues.php": {
|
"InputFileForIssues.php": {
|
||||||
"errors": 2,
|
"errors": 4,
|
||||||
"messages": [
|
"messages": [
|
||||||
{
|
{
|
||||||
"column": 14,
|
"column": 14,
|
||||||
|
@ -20,14 +20,32 @@
|
||||||
"severity": 5,
|
"severity": 5,
|
||||||
"source": "Typo3Update.Classname.IsACall.legacyClassname",
|
"source": "Typo3Update.Classname.IsACall.legacyClassname",
|
||||||
"type": "ERROR"
|
"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
|
"warnings": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"totals": {
|
"totals": {
|
||||||
"errors": 2,
|
"errors": 4,
|
||||||
"fixable": 2,
|
"fixable": 4,
|
||||||
"warnings": 0
|
"warnings": 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,3 +29,9 @@ if (is_a($a, 't3lib_Singleton')) {
|
||||||
if (is_a($a, '\t3lib_Singleton')) {
|
if (is_a($a, '\t3lib_Singleton')) {
|
||||||
// do something
|
// do something
|
||||||
}
|
}
|
||||||
|
if (is_a($a, "t3lib_Singleton")) {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
if (is_a($a, "\\t3lib_Singleton")) {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue