From 7a16dac529e265fe5e76b89956352a9f137f6dc6 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 4 May 2017 13:35:02 +0200 Subject: [PATCH] FEATURE: Allow auto migration of classes in strings * We now check class names case sensitive in strings. * This will prevent issues with stuff like TCA, as class names mathing table names are written in lower case. * Also add further tests and fix issues. * Extend test to check that lower version is not replaced but mentioned as warning. * Expected class names in Tx_ format are reported as fixable error. Resolves: #15 --- .../Feature/LegacyClassnameFeature.php | 15 ++++-- .../Feature/LegacyClassnameMapping.php | 8 ++-- tests/Feature/LegacyClassnameMappingTest.php | 9 ++++ .../Classname/StringSniff/Expected.diff | 17 +++++++ .../Classname/StringSniff/Expected.json | 48 +++++++++++++------ .../StringSniff/InputFileForIssues.php | 5 +- 6 files changed, 77 insertions(+), 25 deletions(-) diff --git a/src/Standards/Typo3Update/Feature/LegacyClassnameFeature.php b/src/Standards/Typo3Update/Feature/LegacyClassnameFeature.php index 4b2d50c..e11902d 100644 --- a/src/Standards/Typo3Update/Feature/LegacyClassnameFeature.php +++ b/src/Standards/Typo3Update/Feature/LegacyClassnameFeature.php @@ -23,6 +23,7 @@ namespace Typo3Update\Feature; use PHP_CodeSniffer as PhpCs; use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_Sniff as PhpCsSniff; +use Typo3Update_Sniffs_Classname_StringSniff as StringSniff; /** * This feature will add fixable errors for old legacy classnames. @@ -96,8 +97,8 @@ class LegacyClassnameFeature implements FeatureInterface */ protected function isLegacyClassname($classname) { - if (get_class($this->sniff) === \Typo3Update_Sniffs_Classname_StringSniff::class) { - return false; + if (get_class($this->sniff) === StringSniff::class) { + return $this->legacyMapping->isLegacyClassname($classname); } return $this->legacyMapping->isCaseInsensitiveLegacyClassname($classname); @@ -112,6 +113,12 @@ class LegacyClassnameFeature implements FeatureInterface */ protected function isMaybeLegacyClassname($classname) { + if (get_class($this->sniff) === StringSniff::class + && $this->legacyMapping->isCaseInsensitiveLegacyClassname($classname) + ) { + return true; + } + if (strpos($classname, 'Tx_') === false) { return false; } @@ -121,9 +128,7 @@ class LegacyClassnameFeature implements FeatureInterface return $nameParts[1]; }, $classname); - if (!in_array($extensionName, $this->legacyExtensions) - && get_class($this->sniff) !== \Typo3Update_Sniffs_Classname_StringSniff::class - ) { + if (!in_array($extensionName, $this->legacyExtensions)) { return false; } diff --git a/src/Standards/Typo3Update/Feature/LegacyClassnameMapping.php b/src/Standards/Typo3Update/Feature/LegacyClassnameMapping.php index 47201c5..5b4dd93 100644 --- a/src/Standards/Typo3Update/Feature/LegacyClassnameMapping.php +++ b/src/Standards/Typo3Update/Feature/LegacyClassnameMapping.php @@ -128,7 +128,7 @@ final class LegacyClassnameMapping $lowerVersion = strtolower($classname); return $this->isLegacyTypo3Classname($classname) || $this->isLegacyMappingClassname($classname) - || $this->isLegacyTypo3Classname($lowerVersion) || $this->isLegacyMappingClassname($lowerVersion); + || isset($this->typo3MappingsKeys[$lowerVersion]) || isset($this->mappingsKeys[$lowerVersion]); } /** @@ -137,7 +137,7 @@ final class LegacyClassnameMapping */ public function getNewClassname($classname) { - if ($this->isLegacyTypo3Classname($classname) || $this->isLegacyTypo3Classname(strtolower($classname))) { + if ($this->isLegacyTypo3Classname($classname) || isset($this->typo3MappingsKeys[strtolower($classname)])) { return $this->typo3Mappings[$this->getTypo3MappingKey($classname)]; } @@ -168,7 +168,7 @@ final class LegacyClassnameMapping */ protected function isLegacyTypo3Classname($classname) { - return isset($this->typo3Mappings[$classname]) || isset($this->typo3MappingsKeys[$classname]); + return isset($this->typo3Mappings[$classname]); } /** @@ -177,7 +177,7 @@ final class LegacyClassnameMapping */ protected function isLegacyMappingClassname($classname) { - return isset($this->mappings[$classname]) || isset($this->mappingsKeys[$classname]); + return isset($this->mappings[$classname]); } /** diff --git a/tests/Feature/LegacyClassnameMappingTest.php b/tests/Feature/LegacyClassnameMappingTest.php index ea47cd4..b75a449 100644 --- a/tests/Feature/LegacyClassnameMappingTest.php +++ b/tests/Feature/LegacyClassnameMappingTest.php @@ -66,6 +66,10 @@ class LegacyClassnameMappingTest extends TestCase */ public function inCaseSensitivityLookupWorks() { + $this->assertFalse( + $this->subject->isLegacyClassname('tx_extbase_domain_model_backenduser'), + 'Classname was returned to be legacy but should not due to lowercase version and case sensitivity.' + ); $this->assertFalse( $this->subject->isLegacyClassname('Tx_Extbase_Domain_Model_Backenduser'), 'Classname was returned to be legacy but should not due to lowercase version and case sensitivity.' @@ -81,6 +85,11 @@ class LegacyClassnameMappingTest extends TestCase */ public function weCanRetrieveNewClassname() { + $this->assertSame( + 'TYPO3\CMS\Extbase\Command\HelpCommandController', + $this->subject->getNewClassname('tx_extbase_command_helpcommandcontroller'), + 'New class name could not be fetched for lower cased version.' + ); $this->assertSame( 'TYPO3\CMS\Extbase\Command\HelpCommandController', $this->subject->getNewClassname('Tx_Extbase_Command_HelpCommandController'), diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/Expected.diff b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/Expected.diff index e69de29..22eaf36 100644 --- a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/Expected.diff +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/Expected.diff @@ -0,0 +1,17 @@ +--- tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/InputFileForIssues.php ++++ PHP_CodeSniffer +@@ -19,10 +19,10 @@ + * 02110-1301, USA. + */ + +-$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = 'Tx_Extbase_Command_HelpCommandController'; +-$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = "Tx_Extbase_Command_HelpCommandController"; ++$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = '\TYPO3\CMS\Extbase\Command\HelpCommandController'; ++$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = "\\TYPO3\\CMS\\Extbase\\Command\\HelpCommandController"; + +-$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['createHashBase'][] = 'EXT:ext_key/Classes/Hooks/Cache.php:&Tx_Extbase_Command_HelpCommandController->getHash'; ++$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['createHashBase'][] = 'EXT:ext_key/Classes/Hooks/Cache.php:&\TYPO3\CMS\Extbase\Command\HelpCommandController->getHash'; + +-$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = "Tx_About_Domain_Model_Extension"; ++$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = "\\TYPO3\\CMS\\About\\Domain\\Model\\Extension"; + $GLOBALS['TCA']['tx_about_domain_model_extension'] = []; diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/Expected.json b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/Expected.json index fa5eb99..0e43a14 100644 --- a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/Expected.json +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/Expected.json @@ -1,42 +1,60 @@ { "files": { "InputFileForIssues.php": { - "errors": 0, + "errors": 4, "messages": [ { "column": 80, - "fixable": false, + "fixable": true, "line": 22, - "message": "Legacy classes are not allowed; found Tx_Extbase_Command_HelpCommandController that might be a legacy class that does not exist anymore", + "message": "Legacy classes are not allowed; found \"Tx_Extbase_Command_HelpCommandController\", use \"TYPO3\\CMS\\Extbase\\Command\\HelpCommandController\" instead", "severity": 5, - "source": "Typo3Update.Classname.String.mightBeLegacyClassname", - "type": "WARNING" + "source": "Typo3Update.Classname.String.legacyClassname", + "type": "ERROR" }, { "column": 80, - "fixable": false, + "fixable": true, "line": 23, - "message": "Legacy classes are not allowed; found Tx_Extbase_Command_HelpCommandController that might be a legacy class that does not exist anymore", + "message": "Legacy classes are not allowed; found \"Tx_Extbase_Command_HelpCommandController\", use \"TYPO3\\CMS\\Extbase\\Command\\HelpCommandController\" instead", "severity": 5, - "source": "Typo3Update.Classname.String.mightBeLegacyClassname", - "type": "WARNING" + "source": "Typo3Update.Classname.String.legacyClassname", + "type": "ERROR" }, { "column": 93, - "fixable": false, + "fixable": true, "line": 25, - "message": "Legacy classes are not allowed; found Tx_ExtKey_Hooks_Cache that might be a legacy class that does not exist anymore", + "message": "Legacy classes are not allowed; found \"Tx_Extbase_Command_HelpCommandController\", use \"TYPO3\\CMS\\Extbase\\Command\\HelpCommandController\" instead", + "severity": 5, + "source": "Typo3Update.Classname.String.legacyClassname", + "type": "ERROR" + }, + { + "column": 80, + "fixable": true, + "line": 27, + "message": "Legacy classes are not allowed; found \"Tx_About_Domain_Model_Extension\", use \"TYPO3\\CMS\\About\\Domain\\Model\\Extension\" instead", + "severity": 5, + "source": "Typo3Update.Classname.String.legacyClassname", + "type": "ERROR" + }, + { + "column": 17, + "fixable": false, + "line": 28, + "message": "Legacy classes are not allowed; found tx_about_domain_model_extension that might be a legacy class that does not exist anymore", "severity": 5, "source": "Typo3Update.Classname.String.mightBeLegacyClassname", "type": "WARNING" } ], - "warnings": 3 + "warnings": 1 } }, "totals": { - "errors": 0, - "fixable": 0, - "warnings": 3 + "errors": 4, + "fixable": 4, + "warnings": 1 } } diff --git a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/InputFileForIssues.php b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/InputFileForIssues.php index 2a902c2..658896c 100644 --- a/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/InputFileForIssues.php +++ b/tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/StringSniff/InputFileForIssues.php @@ -22,4 +22,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = 'Tx_Extbase_Command_HelpCommandController'; $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = "Tx_Extbase_Command_HelpCommandController"; -$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['createHashBase'][] = 'EXT:ext_key/Classes/Hooks/Cache.php:&Tx_ExtKey_Hooks_Cache->getHash'; +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['createHashBase'][] = 'EXT:ext_key/Classes/Hooks/Cache.php:&Tx_Extbase_Command_HelpCommandController->getHash'; + +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'][] = "Tx_About_Domain_Model_Extension"; +$GLOBALS['TCA']['tx_about_domain_model_extension'] = [];