From 097fd7522c8f4e21796411a30a4959714efbb259 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 9 Mar 2017 08:50:58 +0100 Subject: [PATCH 1/5] FEATURE: Migrate legacy class names for insteanceof * Check classes after instanceof token and migrate them when possible Resolves: #7 --- .../ClassnameCheckerTrait.php | 58 ++++++++++++++++++ .../LegacyClassnames/InstanceofSniff.php | 61 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstanceofSniff.php diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php index f3c7010..a4bddde 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php @@ -33,6 +33,16 @@ trait ClassnameCheckerTrait */ private $legacyClassnames = []; + /** + * A list of extension names that might contain legacy class names. + * Used to check clas names for warnings. + * + * @var array + */ + private $legacyExtensions = [ + 'Extbase', + ]; + /** * @param string $mappingFile File containing php array for mapping. */ @@ -59,6 +69,31 @@ trait ClassnameCheckerTrait return isset($this->legacyClassnames[strtolower($classname)]); } + /** + * Guesses whether the given classname is legacy. Will not check + * isLegacyClassname + * + * @param string $classname + * @return bool + */ + private function isMaybeLegacyClassname($classname) + { + if (strpos($classname, 'Tx_') === false) { + return false; + } + + $extensionName = call_user_func(function ($classname) { + $nameParts = explode('_', $classname); + return $nameParts[1]; + }, $classname); + + if (!in_array($extensionName, $this->legacyExtensions)) { + return false; + } + + return true; + } + /** * @param string $classname * @return string @@ -78,6 +113,8 @@ trait ClassnameCheckerTrait */ public function addFixableError(PhpcsFile $phpcsFile, $classnamePosition, $classname) { + $this->addMaybeWarning($phpcsFile, $classnamePosition, $classname); + if ($this->isLegacyClassname($classname) === false) { return; } @@ -97,6 +134,27 @@ trait ClassnameCheckerTrait } } + /** + * Add an warning if given $classname is maybe legacy. + * + * @param PhpcsFile $phpcsFile + * @param int $classnamePosition + * @param string $classname + */ + private function addMaybeWarning(PhpcsFile $phpcsFile, $classnamePosition, $classname) + { + if ($this->isLegacyClassname($classname) || $this->isMaybeLegacyClassname($classname) === false) { + return; + } + + $phpcsFile->addWarning( + 'Legacy classes are not allowed; found %s that might be a legacy class that does not exist anymore', + $classnamePosition, + 'mightBeLegacyClassname', + [$classname] + ); + } + /** * String to use for replacing / fixing the token. * Default is class name itself, can be overwritten in sniff for special behaviour. diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstanceofSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstanceofSniff.php new file mode 100644 index 0000000..05cf416 --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstanceofSniff.php @@ -0,0 +1,61 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +/** + * Detect and migrate extend and implement of old legacy classnames. + */ +class Typo3Update_Sniffs_LegacyClassnames_InstanceofSniff implements PHP_CodeSniffer_Sniff +{ + use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; + + /** + * Returns the token types that this sniff is interested in. + * + * @return array + */ + public function register() + { + return [ + T_INSTANCEOF, + ]; + } + + /** + * Processes the tokens that this sniff is interested in. + * + * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. + * @param int $stackPtr The position in the stack where + * the token was found. + * + * @return void + */ + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + $classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr); + if ($classnamePosition === false) { + return; + } + $classname = $tokens[$classnamePosition]['content']; + + $this->addFixableError($phpcsFile, $classnamePosition, $classname); + } +} From 11cfcf85637db0b7028a72dc83c6093899717ffe Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 14 Mar 2017 08:40:45 +0100 Subject: [PATCH 2/5] TASK: Extend docs with new feature --- Readme.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.rst b/Readme.rst index 228c3b6..1c3fb4c 100644 --- a/Readme.rst +++ b/Readme.rst @@ -51,6 +51,7 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - Typehints in methods and function like injects. +- ``instanceof`` checks. What does it look like? ======================= From ae3c850e58e0c77b188fb4a7971b2688a7bfb210 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 16 Mar 2017 11:02:49 +0100 Subject: [PATCH 3/5] FEATURE: Make legacyExtensions configurable * Make property public to allow configuration through ruleset.xml * Provide example configuration in ruleset.xml Relates: #7 --- .../Sniffs/LegacyClassnames/ClassnameCheckerTrait.php | 6 +++--- src/Standards/Typo3Update/ruleset.xml | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php index a4bddde..be76148 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php @@ -37,11 +37,11 @@ trait ClassnameCheckerTrait * A list of extension names that might contain legacy class names. * Used to check clas names for warnings. * + * Configure through ruleset.xml. + * * @var array */ - private $legacyExtensions = [ - 'Extbase', - ]; + public $legacyExtensions = ['Extbase', 'Fluid']; /** * @param string $mappingFile File containing php array for mapping. diff --git a/src/Standards/Typo3Update/ruleset.xml b/src/Standards/Typo3Update/ruleset.xml index b44e6e2..b067e1d 100644 --- a/src/Standards/Typo3Update/ruleset.xml +++ b/src/Standards/Typo3Update/ruleset.xml @@ -1,4 +1,10 @@ Provides sniffs and fixes for TYPO3 Updates. + + + + + + From cdccab83bc94a19f2fc5d5b6a9b10033261e3ddf Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 16 Mar 2017 11:03:20 +0100 Subject: [PATCH 4/5] BUGFIX: Allow replacement of method * As this method should be replaces by concrete classes. Relates: #7 --- .../Sniffs/LegacyClassnames/ClassnameCheckerTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php index be76148..8d7f6a7 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php @@ -162,7 +162,7 @@ trait ClassnameCheckerTrait * @param string $classname * @return string */ - private function getTokenForReplacement($classname) + protected function getTokenForReplacement($classname) { return $classname; } From cc9552e02855cc66f5541d5a76c2381b588edfd1 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 16 Mar 2017 11:06:15 +0100 Subject: [PATCH 5/5] TASK: Document configuration option Relates: #7 --- Readme.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Readme.rst b/Readme.rst index 1c3fb4c..ebb3c0a 100644 --- a/Readme.rst +++ b/Readme.rst @@ -82,3 +82,23 @@ What does it look like? ---------------------------------------------------------------------- Time: 35ms; Memory: 5Mb + +Configuration +============= + +Configuration is done through PHPCS Standards, e.g. provide a custom ``ruleset.xml`` or customize +the provided one. + +``legacyExtensions`` + Configures which extension names are legacy. Used to provide further checks and warnings about + possible legacy code. E.g. inside of non auto migrated situations. + + Example: + +.. code:: xml + + + + + +