From fcb778d903fa0aa95e15d5fd1d9d252799387c6a Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 21 Mar 2017 16:35:25 +0100 Subject: [PATCH 01/16] WIP|FEATURE: Provide sniff to convert old legacy class definitions * Insert missing namespace, based on existing class name. * Allow configuration of Vendor to use. * Will not adjust uses of the class. * All other sniffs are broken right now, they need to be adjusted to new abstract class. Relates: #36 --- Readme.rst | 18 ++ ...Trait.php => AbstractClassnameChecker.php} | 26 ++- .../MissingNamespaceSniff.php | 157 ++++++++++++++++++ src/Standards/Typo3Update/ruleset.xml | 4 + 4 files changed, 200 insertions(+), 5 deletions(-) rename src/Standards/Typo3Update/Sniffs/LegacyClassnames/{ClassnameCheckerTrait.php => AbstractClassnameChecker.php} (90%) create mode 100644 src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php diff --git a/Readme.rst b/Readme.rst index 86f6a6e..1c13268 100644 --- a/Readme.rst +++ b/Readme.rst @@ -69,6 +69,8 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - ``catch`` of legacy class names. +- Convert old legacy class definitions in extensions to namespace ones. + Also we check for the following deprecated calls: - Check for ``create`` on ``ObjectManager``, which is "stupid" just all ``create`` calls are marked @@ -136,3 +138,19 @@ Typo3Update.LegacyClassnames.DocComment: ``allowedTags`` + +``vendor`` + Configure your vendor through ``ruleset.xml`` or using ``--runtime-set``. Default is + ``YourCompany``. + + Example: + +.. code:: xml + + + +Example: + +.. code:: bash + + --runtime-set vendor YourVendor diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php similarity index 90% rename from src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php rename to src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index d6a2fe2..305213e 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -20,12 +20,14 @@ namespace Typo3Update\Sniffs\LegacyClassnames; * 02110-1301, USA. */ +use PHP_CodeSniffer as PhpCs; use PHP_CodeSniffer_File as PhpcsFile; +use PHP_CodeSniffer_Sniff as PhpCsSniff; /** * Provide common uses for all sniffs, regarding class name checks. */ -trait ClassnameCheckerTrait +abstract class AbstractClassnameChecker implements PhpCsSniff { /** * Contains mapping from old -> new class names. @@ -43,6 +45,20 @@ trait ClassnameCheckerTrait */ public $legacyExtensions = ['Extbase', 'Fluid']; + /** + * Returns the configured vendor, e.g. to generate new namespaces. + * + * @return string + */ + protected function getVendor() + { + $vendor = PhpCs::getConfigData('vendor'); + if (!$vendor) { + $vendor = 'YourCompany'; + } + return trim($vendor, '\\/'); + } + /** * @param string $mappingFile File containing php array for mapping. */ @@ -103,7 +119,7 @@ trait ClassnameCheckerTrait * @param string $classname * @return bool */ - private function isLegacyClassname($classname) + protected function isLegacyClassname($classname) { $this->initialize(); return isset($this->legacyClassnames[strtolower($classname)]); @@ -138,7 +154,7 @@ trait ClassnameCheckerTrait * @param string $classname * @return string */ - private function getNewClassname($classname) + protected function getNewClassname($classname) { $this->initialize(); return $this->legacyClassnames[strtolower($classname)]; @@ -200,10 +216,10 @@ trait ClassnameCheckerTrait * @param int $classnamePosition * @param string $classname */ - private function replaceLegacyClassname(PhpcsFile $phpcsFile, $classnamePosition, $classname) + protected function replaceLegacyClassname(PhpcsFile $phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix = false) { $prefix = '\\'; - if ($phpcsFile->getTokens()[$classnamePosition -1]['code'] === T_NS_SEPARATOR) { + if ($forceEmptyPrefix || $phpcsFile->getTokens()[$classnamePosition -1]['code'] === T_NS_SEPARATOR) { $prefix = ''; } diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php new file mode 100644 index 0000000..2d74d70 --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php @@ -0,0 +1,157 @@ + + * + * 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. + */ + +use PHP_CodeSniffer_File as PhpCsFile; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; + +/** + * Detect missing namespaces for class definitions. + */ +class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends AbstractClassnameChecker +{ + /** + * Returns the token types that this sniff is interested in. + * + * @return array + */ + public function register() + { + return [T_CLASS]; + } + + /** + * Processes the tokens that this sniff is interested in. + * + * @param PhpCsFile $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(PhpCsFile $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + $namespacePosition = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr); + if ($namespacePosition !== false) { + return; + } + $classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr); + if ($classnamePosition === false) { + return; + } + $classname = $tokens[$classnamePosition]['content']; + + $this->addFixableError($phpcsFile, $classnamePosition, $classname); + } + + /** + * Overwrite as we don't look up the classname, but check whether the style is legacy. + * + * @param string $classname + * @return bool + */ + protected function isLegacyClassname($classname) + { + return strpos($classname, 'Tx_') === 0; + } + + /** + * @param string $classname + * @return string + */ + protected function getNewClassname($classname) + { + return substr($classname, strrpos($classname, '_') + 1); + } + + /** + * + * @param PhpCsFile $phpcsFile + * @param int $classnamePosition + * @param string $classname + */ + protected function replaceLegacyClassname(PhpCsFile $phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix = true) + { + parent::replaceLegacyClassname($phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix); + + $tokens = $phpcsFile->getTokens(); + $lineEndings = PhpCsFile::detectLineEndings($phpcsFile->getFilename()); + $suffix = $lineEndings; + + // TODO, does not work. + if ($tokens[1] === "\n") { + $suffix .= $lineEndings; + } + + $phpcsFile->fixer->replaceToken( + $this->getNamespacePosition($phpcsFile), + 'getNamespaceDefinition($classname) . $suffix + ); + } + + /** + * @param PhpCsFile $phpcsFile + * @return int + */ + protected function getNamespacePosition(PhpCsFile $phpcsFile) + { + return $phpcsFile->findNext(T_OPEN_TAG, 0); + } + + /** + * Returns whole statement to define namespace. + * + * E.g. namespace VENDOR\ExtName\FolderName; + * + * @param string $classname + * @return string + */ + protected function getNamespaceDefinition($classname) + { + $vendor = trim($this->getVendor(), '\\/'); + + return 'namespace ' + . $vendor + . '\\' + . $this->getNamespace($classname) + . ';' + ; + } + + /** + * Returns namespace, without vendor, based on legacy class name. + * + * E.g. Tx_ExtName_FolderName_FileName -> ExtName\FolderName + * + * @param string $classname + * @return string + */ + protected function getNamespace($classname) + { + $classnameParts = explode('_', $classname); + + unset($classnameParts[0]); // Remove Tx_ + unset($classnameParts[count($classnameParts)]); // Remove class name itself. + + return implode('\\', $classnameParts); + } +} diff --git a/src/Standards/Typo3Update/ruleset.xml b/src/Standards/Typo3Update/ruleset.xml index c8e6a69..260cadb 100644 --- a/src/Standards/Typo3Update/ruleset.xml +++ b/src/Standards/Typo3Update/ruleset.xml @@ -12,4 +12,8 @@ + + + Legacy class definitions are not allowed; found "%s". Wrap your class inside a namespace. + From 21c72c8b96757847e578c2ff4f68a8715eb5a199 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 21 Mar 2017 17:22:18 +0100 Subject: [PATCH 02/16] WIP|FEATURE: Handle trait and interfaces beside classes * Also fix check whether another line should be added after namespace definition. Relates: #36 --- .../Sniffs/LegacyClassnames/MissingNamespaceSniff.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php index 2d74d70..391c357 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php @@ -34,7 +34,11 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract */ public function register() { - return [T_CLASS]; + return [ + T_CLASS, + T_INTERFACE, + T_TRAIT, + ]; } /** @@ -97,8 +101,7 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract $lineEndings = PhpCsFile::detectLineEndings($phpcsFile->getFilename()); $suffix = $lineEndings; - // TODO, does not work. - if ($tokens[1] === "\n") { + if ($tokens[1]['code'] !== T_WHITESPACE) { $suffix .= $lineEndings; } From 9a13223d62a69d3931c1646160e07cf1a3995217 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 21 Mar 2017 13:30:04 +0100 Subject: [PATCH 03/16] TASK: Rename method * To follow CGL Relates: #33 --- .../AbstractClassnameChecker.php | 20 +++++++++---------- .../LegacyClassnames/StaticCallSniff.php | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index 305213e..3827b27 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -21,7 +21,7 @@ namespace Typo3Update\Sniffs\LegacyClassnames; */ use PHP_CodeSniffer as PhpCs; -use PHP_CodeSniffer_File as PhpcsFile; +use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_Sniff as PhpCsSniff; /** @@ -78,7 +78,7 @@ abstract class AbstractClassnameChecker implements PhpCsSniff * * @return bool */ - protected function findPrev() + protected function shouldLookBefore() { return false; } @@ -90,13 +90,13 @@ abstract class AbstractClassnameChecker implements PhpCsSniff * the class name. This way only the register method has to be registered * in default cases. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. + * @param PhpCsFile $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(PhpcsFile $phpcsFile, $stackPtr) + public function process(PhpCsFile $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); @@ -163,11 +163,11 @@ abstract class AbstractClassnameChecker implements PhpCsSniff /** * Add an fixable error if given $classname is legacy. * - * @param PhpcsFile $phpcsFile + * @param PhpCsFile $phpcsFile * @param int $classnamePosition * @param string $classname */ - public function addFixableError(PhpcsFile $phpcsFile, $classnamePosition, $classname) + public function addFixableError(PhpCsFile $phpcsFile, $classnamePosition, $classname) { $classname = trim($classname, '\\\'"'); // Remove trailing slash, and quotes. $this->addMaybeWarning($phpcsFile, $classnamePosition, $classname); @@ -191,11 +191,11 @@ abstract class AbstractClassnameChecker implements PhpCsSniff /** * Add an warning if given $classname is maybe legacy. * - * @param PhpcsFile $phpcsFile + * @param PhpCsFile $phpcsFile * @param int $classnamePosition * @param string $classname */ - private function addMaybeWarning(PhpcsFile $phpcsFile, $classnamePosition, $classname) + private function addMaybeWarning(PhpCsFile $phpcsFile, $classnamePosition, $classname) { if ($this->isLegacyClassname($classname) || $this->isMaybeLegacyClassname($classname) === false) { return; @@ -212,11 +212,11 @@ abstract class AbstractClassnameChecker implements PhpCsSniff /** * Replaces the classname at $classnamePosition with $classname in $phpcsFile. * - * @param PhpcsFile $phpcsFile + * @param PhpCsFile $phpcsFile * @param int $classnamePosition * @param string $classname */ - protected function replaceLegacyClassname(PhpcsFile $phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix = false) + protected function replaceLegacyClassname(PhpCsFile $phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix = false) { $prefix = '\\'; if ($forceEmptyPrefix || $phpcsFile->getTokens()[$classnamePosition -1]['code'] === T_NS_SEPARATOR) { diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/StaticCallSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/StaticCallSniff.php index 91b2eef..d10ce73 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/StaticCallSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/StaticCallSniff.php @@ -32,7 +32,7 @@ class Typo3Update_Sniffs_LegacyClassnames_StaticCallSniff implements PHP_CodeSni * * @return bool */ - protected function findPrev() + protected function shouldLookBefore() { return true; } From 0d985ef42a8d2e6512f541e7cb7378c009df992e Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 21 Mar 2017 13:43:52 +0100 Subject: [PATCH 04/16] TASK: Fix call to undefined method Relates: #33 --- .../Sniffs/LegacyClassnames/AbstractClassnameChecker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index 3827b27..347717b 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -101,7 +101,7 @@ abstract class AbstractClassnameChecker implements PhpCsSniff $tokens = $phpcsFile->getTokens(); $classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr); - if ($this->findPrev()) { + if ($this->shouldLookBefore()) { $classnamePosition = $phpcsFile->findPrevious(T_STRING, $stackPtr); } if ($classnamePosition === false) { From f91d3240e52e6a377adc98f2b3423a1c75e9f0c7 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 08:41:06 +0100 Subject: [PATCH 05/16] TASK: According to PR, use else instead of default --- .../Sniffs/LegacyClassnames/AbstractClassnameChecker.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index 347717b..57668a3 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -100,10 +100,12 @@ abstract class AbstractClassnameChecker implements PhpCsSniff { $tokens = $phpcsFile->getTokens(); - $classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr); if ($this->shouldLookBefore()) { $classnamePosition = $phpcsFile->findPrevious(T_STRING, $stackPtr); + } else { + $classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr); } + if ($classnamePosition === false) { return; } From eaa99973b14f0d5bf68f76084ecf4ae74186da56 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 10:23:45 +0100 Subject: [PATCH 06/16] TASK: Refactor existing sniffs to work again * As we migrated trait to class, all sniffs have to extend instead to use the new class. * Also make PhpCsFile import and type hints the same across all sniffs. Relates: #36 --- .../Sniffs/LegacyClassnames/DocCommentSniff.php | 11 ++++++----- .../Sniffs/LegacyClassnames/InheritanceSniff.php | 6 +++--- .../Sniffs/LegacyClassnames/InlineCommentSniff.php | 11 ++++++----- .../Sniffs/LegacyClassnames/InstanceofSniff.php | 6 +++--- .../InstantiationWithMakeInstanceSniff.php | 9 +++++---- .../LegacyClassnames/InstantiationWithNewSniff.php | 6 +++--- .../InstantiationWithObjectManagerSniff.php | 9 +++++---- .../Sniffs/LegacyClassnames/IsACallSniff.php | 10 +++++----- .../Sniffs/LegacyClassnames/StaticCallSniff.php | 6 +++--- .../LegacyClassnames/TypeHintCatchExceptionSniff.php | 6 ++---- .../Sniffs/LegacyClassnames/TypeHintSniff.php | 11 +++++------ .../Typo3Update/Sniffs/LegacyClassnames/UseSniff.php | 6 ++---- 12 files changed, 48 insertions(+), 49 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff.php index 7ab8eca..7edd478 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff.php @@ -19,15 +19,16 @@ * 02110-1301, USA. */ +use PHP_CodeSniffer_File as PhpCsFile; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; + /** * Migrate PHP Doc comments. * * E.g. annotations like @param or @return, see $allowedTags. */ -class Typo3Update_Sniffs_LegacyClassnames_DocCommentSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_DocCommentSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * The configured tags will be processed. * @var array @@ -55,13 +56,13 @@ class Typo3Update_Sniffs_LegacyClassnames_DocCommentSniff implements PHP_CodeSni /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. + * @param PhpCsFile $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) + public function process(PhpCsFile $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); if (!in_array($tokens[$stackPtr]['content'], $this->allowedTags)) { diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InheritanceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InheritanceSniff.php index 72c5280..884d4b5 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InheritanceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InheritanceSniff.php @@ -19,13 +19,13 @@ * 02110-1301, USA. */ +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; + /** * Detect and migrate extend and implement of old legacy classnames. */ -class Typo3Update_Sniffs_LegacyClassnames_InheritanceSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_InheritanceSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * Returns the token types that this sniff is interested in. * diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php index c6e8303..2b7caaf 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php @@ -19,13 +19,14 @@ * 02110-1301, USA. */ +use PHP_CodeSniffer_File as PhpCsFile; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; + /** * Migrate PHP inline comments, e.g. for IDEs. */ -class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * Original token content for reuse accross methods. * @var string @@ -47,13 +48,13 @@ class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff implements PHP_Code /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. + * @param PhpCsFile $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) + public function process(PhpCsFile $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $this->originalTokenContent = $tokens[$stackPtr]['content']; diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstanceofSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstanceofSniff.php index 1274923..b261922 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstanceofSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstanceofSniff.php @@ -19,13 +19,13 @@ * 02110-1301, USA. */ +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; + /** * Detect and migrate instanceof checks of old legacy classnames. */ -class Typo3Update_Sniffs_LegacyClassnames_InstanceofSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_InstanceofSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * Returns the token types that this sniff is interested in. * diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php index 8b46d7e..169716e 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php @@ -19,14 +19,15 @@ * 02110-1301, USA. */ +use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_Tokens as Tokens; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; /** * Detect and migrate instantiations of old legacy classnames using "makeInstance". */ -class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; /** @@ -48,13 +49,13 @@ class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff imp /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. + * @param PhpCsFile $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) + public function process(PhpCsFile $phpcsFile, $stackPtr) { if (!$this->isFunctionCall($phpcsFile, $stackPtr)) { return; diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithNewSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithNewSniff.php index 318cbcd..9a1758e 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithNewSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithNewSniff.php @@ -19,13 +19,13 @@ * 02110-1301, USA. */ +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; + /** * Detect and migrate old legacy classnames instantiations using phps "new". */ -class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithNewSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithNewSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * Returns the token types that this sniff is interested in. * diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithObjectManagerSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithObjectManagerSniff.php index 632edce..a3d173f 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithObjectManagerSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithObjectManagerSniff.php @@ -19,14 +19,15 @@ * 02110-1301, USA. */ +use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_Tokens as Tokens; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; /** * Detect and migrate old legacy classname instantiations using objectmanager create and get. */ -class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithObjectManagerSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithObjectManagerSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; /** @@ -42,13 +43,13 @@ class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithObjectManagerSniff im /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. + * @param PhpCsFile $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) + public function process(PhpCsFile $phpcsFile, $stackPtr) { if (!$this->isFunctionCall($phpcsFile, $stackPtr)) { return; diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php index 3d41927..b169ac4 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php @@ -19,14 +19,14 @@ * 02110-1301, USA. */ -use PHP_CodeSniffer_Tokens as Tokens; +use PHP_CodeSniffer_File as PhpcsFile; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; /** * Detect and migrate instantiations of old legacy classnames using "makeInstance". */ -class Typo3Update_Sniffs_LegacyClassnames_IsACallSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_IsACallSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; /** @@ -48,13 +48,13 @@ class Typo3Update_Sniffs_LegacyClassnames_IsACallSniff implements PHP_CodeSniffe /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. + * @param PhpCsFile $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) + public function process(PhpCsFile $phpcsFile, $stackPtr) { if (!$this->isFunctionCall($phpcsFile, $stackPtr)) { return; diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/StaticCallSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/StaticCallSniff.php index d10ce73..1be8982 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/StaticCallSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/StaticCallSniff.php @@ -19,13 +19,13 @@ * 02110-1301, USA. */ +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; + /** * Detect and migrate static calls to old legacy classnames. */ -class Typo3Update_Sniffs_LegacyClassnames_StaticCallSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_StaticCallSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * Define whether the T_STRING default behaviour should be checked before * or after the $stackPtr. diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/TypeHintCatchExceptionSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/TypeHintCatchExceptionSniff.php index 6bd9b10..876aec8 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/TypeHintCatchExceptionSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/TypeHintCatchExceptionSniff.php @@ -19,15 +19,13 @@ * 02110-1301, USA. */ -use PHP_CodeSniffer_Tokens as Tokens; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; /** * Migrate Typehints in catch statements. */ -class Typo3Update_Sniffs_LegacyClassnames_TypehintCatchExceptionSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_TypehintCatchExceptionSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * Returns the token types that this sniff is interested in. * diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/TypeHintSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/TypeHintSniff.php index 7e1bb56..3901309 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/TypeHintSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/TypeHintSniff.php @@ -19,15 +19,14 @@ * 02110-1301, USA. */ -use PHP_CodeSniffer_Tokens as Tokens; +use PHP_CodeSniffer_File as PhpCsFile; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; /** * Migrate Typehints in function / method definitions. */ -class Typo3Update_Sniffs_LegacyClassnames_TypehintSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_TypehintSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * Returns the token types that this sniff is interested in. * @@ -41,13 +40,13 @@ class Typo3Update_Sniffs_LegacyClassnames_TypehintSniff implements PHP_CodeSniff /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. + * @param PhpCsFile $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) + public function process(PhpCsFile $phpcsFile, $stackPtr) { $params = $phpcsFile->getMethodParameters($stackPtr); foreach ($params as $parameter) { diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/UseSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/UseSniff.php index 0b7bf6d..9d882c5 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/UseSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/UseSniff.php @@ -19,15 +19,13 @@ * 02110-1301, USA. */ -use PHP_CodeSniffer_Tokens as Tokens; +use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; /** * Migrate old legacy class names in use statements. */ -class Typo3Update_Sniffs_LegacyClassnames_UseSniff implements PHP_CodeSniffer_Sniff +class Typo3Update_Sniffs_LegacyClassnames_UseSniff extends AbstractClassnameChecker { - use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; - /** * Returns the token types that this sniff is interested in. * From f9f82b06292ac05dc86f1664a085ce1caafaf499 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 10:55:09 +0100 Subject: [PATCH 07/16] FEATURE: Migrate plugin and module registration and configuration * Prefix with configured vendor if first argument does not start with a string already. Relates: #36 --- .../AbstractClassnameChecker.php | 18 +--- ...MissingVendorForPluginsAndModulesSniff.php | 89 +++++++++++++++++++ .../Typo3Update/Sniffs/OptionsAccessTrait.php | 43 +++++++++ 3 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php create mode 100644 src/Standards/Typo3Update/Sniffs/OptionsAccessTrait.php diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index 57668a3..6019cdc 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -20,15 +20,17 @@ namespace Typo3Update\Sniffs\LegacyClassnames; * 02110-1301, USA. */ -use PHP_CodeSniffer as PhpCs; use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_Sniff as PhpCsSniff; +use Typo3Update\Sniffs\OptionsAccessTrait; /** * Provide common uses for all sniffs, regarding class name checks. */ abstract class AbstractClassnameChecker implements PhpCsSniff { + use OptionsAccessTrait; + /** * Contains mapping from old -> new class names. * @var array @@ -45,20 +47,6 @@ abstract class AbstractClassnameChecker implements PhpCsSniff */ public $legacyExtensions = ['Extbase', 'Fluid']; - /** - * Returns the configured vendor, e.g. to generate new namespaces. - * - * @return string - */ - protected function getVendor() - { - $vendor = PhpCs::getConfigData('vendor'); - if (!$vendor) { - $vendor = 'YourCompany'; - } - return trim($vendor, '\\/'); - } - /** * @param string $mappingFile File containing php array for mapping. */ diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php new file mode 100644 index 0000000..e2fc43b --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php @@ -0,0 +1,89 @@ + + * + * 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. + */ + +use PHP_CodeSniffer_File as PhpCsFile; +use PHP_CodeSniffer_Sniff as PhpCsSniff; +use PHP_CodeSniffer_Tokens as Tokens; + +/** + * Detect whether vendor is missing for plugins and modules registrations and configurations. + */ +class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff implements PhpCsSniff +{ + use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; + use \Typo3Update\Sniffs\OptionsAccessTrait; + + /** + * Original token content for reuse accross methods. + * @var string + */ + protected $originalTokenContent = ''; + + /** + * Returns the token types that this sniff is interested in. + * + * @return array + */ + public function register() + { + return Tokens::$functionNameTokens; + } + + /** + * Processes the tokens that this sniff is interested in. + * + * @param PhpCsFile $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(PhpCsFile $phpcsFile, $stackPtr) + { + $functionsToHandle = ['registerPlugin', 'configurePlugin', 'registerModule', 'configureModule']; + if (!$this->isFunctionCall($phpcsFile, $stackPtr)) { + return; + } + $tokens = $phpcsFile->getTokens(); + + if (!in_array($tokens[$stackPtr]['content'], $functionsToHandle)) { + return; + } + + $firstParameter = $phpcsFile->findNext([T_WHITESPACE, T_OPEN_PARENTHESIS], $stackPtr + 1, null, true); + if ($firstParameter === false || $tokens[$firstParameter]['code'] === T_CONSTANT_ENCAPSED_STRING) { + return; + } + + $fix = $phpcsFile->addFixableError( + 'No vendor is given, that will break TYPO3 handling for namespaced classes. Add vendor before Extensionkey like: "Vendor." . $_EXTKEY', + $firstParameter, + 'missingVendor' + ); + + if ($fix === true) { + $phpcsFile->fixer->replaceToken( + $firstParameter, + "'{$this->getVendor()}.' . " . $tokens[$firstParameter]['content'] + ); + } + } +} diff --git a/src/Standards/Typo3Update/Sniffs/OptionsAccessTrait.php b/src/Standards/Typo3Update/Sniffs/OptionsAccessTrait.php new file mode 100644 index 0000000..3037bd8 --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/OptionsAccessTrait.php @@ -0,0 +1,43 @@ + + * + * 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. + */ + +use PHP_CodeSniffer as PhpCs; + +/** + * Wrapper to retrieve options from PhpCs with defaults. + */ +trait OptionsAccessTrait +{ + /** + * Returns the configured vendor, e.g. to generate new namespaces. + * + * @return string + */ + public function getVendor() + { + $vendor = PhpCs::getConfigData('vendor'); + if (!$vendor) { + $vendor = 'YourCompany'; + } + return trim($vendor, '\\/'); + } +} From 1561c13b468d45afa6e44b42afde5d459a220a2f Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 11:27:40 +0100 Subject: [PATCH 08/16] FEATURE: Improve handling of missing Vendor * Check for string concatenation instead of earlier more stupid. Relates: #36 --- Readme.rst | 15 +++++++ ...MissingVendorForPluginsAndModulesSniff.php | 42 ++++++++++++++++--- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/Readme.rst b/Readme.rst index 1c13268..8d8b781 100644 --- a/Readme.rst +++ b/Readme.rst @@ -71,6 +71,21 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - Convert old legacy class definitions in extensions to namespace ones. +- Add missing vendor to plugin and module registrations and configurations. + You might want to set this to non fixable and warning if you already provide the vendor inside a + single Variable, together with your extension key, as this is not recognized. So the following + will be recognized: + + - $_EXTKEY, + + - $VENDOR . $_EXTKEY, + + - 'VENDOR.' . $_EXTKEY, + + While the following will not: + + - $key = 'Vendor.' . $_EXTKEY; + Also we check for the following deprecated calls: - Check for ``create`` on ``ObjectManager``, which is "stupid" just all ``create`` calls are marked diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php index e2fc43b..e4eafe8 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php @@ -68,22 +68,52 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff return; } - $firstParameter = $phpcsFile->findNext([T_WHITESPACE, T_OPEN_PARENTHESIS], $stackPtr + 1, null, true); - if ($firstParameter === false || $tokens[$firstParameter]['code'] === T_CONSTANT_ENCAPSED_STRING) { + $firstArgument = $phpcsFile->findNext([T_WHITESPACE, T_OPEN_PARENTHESIS], $stackPtr + 1, null, true); + if (! $this->isVendorMissing($phpcsFile, $firstArgument)) { return; } $fix = $phpcsFile->addFixableError( - 'No vendor is given, that will break TYPO3 handling for namespaced classes. Add vendor before Extensionkey like: "Vendor." . $_EXTKEY', - $firstParameter, + 'No vendor is given, that will break TYPO3 handling for namespaced classes.' + . ' Add vendor before Extensionkey like: "Vendor." . $_EXTKEY', + $firstArgument, 'missingVendor' ); if ($fix === true) { $phpcsFile->fixer->replaceToken( - $firstParameter, - "'{$this->getVendor()}.' . " . $tokens[$firstParameter]['content'] + $firstArgument, + "'{$this->getVendor()}.' . " . $tokens[$firstArgument]['content'] ); } } + + /** + * Checks whether vendor is missing in given argument. + * + * @param PhpCsFile $phpcsFile + * @param int|bool $argumentStart + * + * @return bool + */ + protected function isVendorMissing(PhpCsFile $phpcsFile, $argumentStart) + { + if ($argumentStart === false) { + return false; + } + + $argumentEnd = $phpcsFile->findNext(T_COMMA, $argumentStart); + if ($argumentEnd === false) { + return false; + } + + $stringConcats = array_filter( + array_slice($phpcsFile->getTokens(), $argumentStart, $argumentEnd - $argumentStart), + function (array $token) { + return $token['code'] === 'PHPCS_T_STRING_CONCAT'; + } + ); + + return count($stringConcats) === 0; + } } From 8fbe25a422b5e8b733634c9f9c9920ea79fc0532 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 11:40:17 +0100 Subject: [PATCH 09/16] TASK: Cleanup missing vendor * Add configured vendor in message for easer migration in IDEs (by hand). * Remove non existing function from checks. Relates: #36 --- .../MissingVendorForPluginsAndModulesSniff.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php index e4eafe8..072bf62 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php @@ -58,7 +58,7 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff */ public function process(PhpCsFile $phpcsFile, $stackPtr) { - $functionsToHandle = ['registerPlugin', 'configurePlugin', 'registerModule', 'configureModule']; + $functionsToHandle = ['registerPlugin', 'configurePlugin', 'registerModule']; if (!$this->isFunctionCall($phpcsFile, $stackPtr)) { return; } @@ -75,9 +75,10 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff $fix = $phpcsFile->addFixableError( 'No vendor is given, that will break TYPO3 handling for namespaced classes.' - . ' Add vendor before Extensionkey like: "Vendor." . $_EXTKEY', + . ' Add vendor before Extensionkey like: "%s." . $_EXTKEY', $firstArgument, - 'missingVendor' + 'missingVendor', + [$this->getVendor()] ); if ($fix === true) { From 6420c72945f856b0c18b781f71baa89dcddc532d Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 13:09:58 +0100 Subject: [PATCH 10/16] FEATURE: Allow remapping of usages for redefiined classes * As we already adjust function, trait and interface definitions, we now also migrate the usage of the old versions. * This is done in second run, as mentioned in adjusted Readme. Relates: #36 --- Readme.rst | 7 + .../AbstractClassnameChecker.php | 39 +++--- .../Sniffs/LegacyClassnames/Mapping.php | 124 ++++++++++++++++++ .../MissingNamespaceSniff.php | 16 +-- 4 files changed, 157 insertions(+), 29 deletions(-) create mode 100644 src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php diff --git a/Readme.rst b/Readme.rst index 8d8b781..cf19a83 100644 --- a/Readme.rst +++ b/Readme.rst @@ -71,6 +71,13 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - Convert old legacy class definitions in extensions to namespace ones. +- Convert usage of previously converted class definitions. On first run the definition will be + converted, on second the usage. This is due to the fact, that PHPCS might find the definition + after the usage, so please run twice. + + *NOTE* The configured file will be updated after each run, for each converted class, trait and + interface definition. + - Add missing vendor to plugin and module registrations and configurations. You might want to set this to non fixable and warning if you already provide the vendor inside a single Variable, together with your extension key, as this is not recognized. So the following diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index 6019cdc..b72e1d2 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -22,6 +22,7 @@ namespace Typo3Update\Sniffs\LegacyClassnames; use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_Sniff as PhpCsSniff; +use Typo3Update\Sniffs\LegacyClassnames\Mapping; use Typo3Update\Sniffs\OptionsAccessTrait; /** @@ -31,12 +32,6 @@ abstract class AbstractClassnameChecker implements PhpCsSniff { use OptionsAccessTrait; - /** - * Contains mapping from old -> new class names. - * @var array - */ - private $legacyClassnames = []; - /** * A list of extension names that might contain legacy class names. * Used to check clas names for warnings. @@ -47,17 +42,9 @@ abstract class AbstractClassnameChecker implements PhpCsSniff */ public $legacyExtensions = ['Extbase', 'Fluid']; - /** - * @param string $mappingFile File containing php array for mapping. - */ - private function initialize($mappingFile = __DIR__ . '/../../../../../LegacyClassnames.php') + public function __construct() { - if ($this->legacyClassnames !== []) { - return; - } - - $legacyClassnames = require $mappingFile; - $this->legacyClassnames = $legacyClassnames['aliasToClassNameMapping']; + $this->legacyMapping = Mapping::getInstance(); } /** @@ -111,8 +98,7 @@ abstract class AbstractClassnameChecker implements PhpCsSniff */ protected function isLegacyClassname($classname) { - $this->initialize(); - return isset($this->legacyClassnames[strtolower($classname)]); + return $this->legacyMapping->isLegacyClassname($classname); } /** @@ -146,8 +132,21 @@ abstract class AbstractClassnameChecker implements PhpCsSniff */ protected function getNewClassname($classname) { - $this->initialize(); - return $this->legacyClassnames[strtolower($classname)]; + return $this->legacyMapping->getNewClassname($classname); + } + + /** + * Use to add new mappings found during parsing. + * E.g. in MissingNamespaceSniff old class definitions are fixed and a new mapping exists afterwards. + * + * @param string $legacyClassname + * @param string $newClassname + * + * @return void + */ + protected function addLegacyClassname($legacyClassname, $newClassname) + { + $this->legacyMapping->addLegacyClassname($legacyClassname, $newClassname); } /** diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php new file mode 100644 index 0000000..885ecfd --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php @@ -0,0 +1,124 @@ + + * + * 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. + */ + +use Typo3Update\Sniffs\OptionsAccessTrait; + +/** + * Singleton wrapper for mappings. + * + * Will check the configured file for whether a class is legacy and provides further methods. + * Also can update to add new migrated class names. + */ +class Mapping +{ + use OptionsAccessTrait; + + // Singleton implementation - Start + static protected $instance = null; + public static function getInstance() + { + if (static::$instance === null) { + static::$instance = new Mapping(); + } + + return static::$instance; + } + private function __clone() + { + } + private function __wakeup() + { + } + private function __construct() + { + // $mappingFile = $this->getMappingFile(); + $mappingFile = __DIR__ . '/../../../../../LegacyClassnames.php'; + + $this->mappings = require $mappingFile; + } + // Singleton implementation - End + + /** + * Contains mappings as defined by composer for alias mapping. + * @var array + */ + protected $mappings = []; + + /** + * Checks whether a mapping exists for the given $classname, + * indicating it's legacy. + * + * @param string $classname + * @return bool + */ + public function isLegacyClassname($classname) + { + return isset($this->mappings['aliasToClassNameMapping'][strtolower($classname)]); + } + + /** + * @param string $classname + * @return string + */ + public function getNewClassname($classname) + { + return $this->mappings['aliasToClassNameMapping'][strtolower($classname)]; + } + + /** + * Use to add new mappings found during parsing. + * E.g. in MissingNamespaceSniff old class definitions are fixed and a new mapping exists afterwards. + * + * @param string $legacyClassname + * @param string $newClassname + * + * @return void + */ + public function addLegacyClassname($legacyClassname, $newClassname) + { + $key = strtolower($legacyClassname); + + $this->mappings['aliasToClassNameMapping'][$key] = $newClassname; + $this->mappings['classNameToAliasMapping'][$newClassname] = [$key => $key]; + } + + /** + * Used to persist new mappings. + */ + public function __destruct() + { + // For some reasons desctruct is called multiple times, while construct + // is called once. Until we know the issue and fix it, this is our + // workaround to not break the file and do stuff in an unkown instance. + if ($this !== static::$instance) { + return; + } + + // $mappingFile = $this->getMappingFile(); + $mappingFile = __DIR__ . '/../../../../../LegacyClassnames.php'; + + file_put_contents( + $mappingFile, + 'mappings, true) . ';' + ); + } +} diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php index 391c357..5374dd3 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php @@ -109,6 +109,10 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract $this->getNamespacePosition($phpcsFile), 'getNamespaceDefinition($classname) . $suffix ); + $this->addLegacyClassname( + $classname, + $this->getNamespace($classname) . '\\' . $this->getNewClassname($classname) + ); } /** @@ -130,14 +134,7 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract */ protected function getNamespaceDefinition($classname) { - $vendor = trim($this->getVendor(), '\\/'); - - return 'namespace ' - . $vendor - . '\\' - . $this->getNamespace($classname) - . ';' - ; + return 'namespace ' . $this->getNamespace($classname) . ';'; } /** @@ -150,11 +147,12 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract */ protected function getNamespace($classname) { + $vendor = trim($this->getVendor(), '\\/'); $classnameParts = explode('_', $classname); unset($classnameParts[0]); // Remove Tx_ unset($classnameParts[count($classnameParts)]); // Remove class name itself. - return implode('\\', $classnameParts); + return $vendor . '\\' . implode('\\', $classnameParts); } } From 9b3d5d95152d081a05d1ba40640a5ee201fb5dd9 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 16:12:40 +0100 Subject: [PATCH 11/16] BUGFIX: Support multiple interfaces * Process extends as before. * Process all interfaces in addition, not only the first one. --- .../LegacyClassnames/InheritanceSniff.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InheritanceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InheritanceSniff.php index 884d4b5..59c9dd3 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InheritanceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InheritanceSniff.php @@ -19,6 +19,7 @@ * 02110-1301, USA. */ +use PHP_CodeSniffer_File as PhpCsFile; use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; /** @@ -38,4 +39,56 @@ class Typo3Update_Sniffs_LegacyClassnames_InheritanceSniff extends AbstractClass T_IMPLEMENTS, ]; } + + /** + * Processes the tokens that this sniff is interested in. + * + * This is the default implementation, as most of the time next T_STRING is + * the class name. This way only the register method has to be registered + * in default cases. + * + * @param PhpCsFile $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(PhpCsFile $phpcsFile, $stackPtr) + { + if ($phpcsFile->getTokens()[$stackPtr]['code'] === T_IMPLEMENTS) { + $this->processInterfaces($phpcsFile, $stackPtr); + return; + } + + parent::process($phpcsFile, $stackPtr); + } + + /** + * Process all interfaces for current class. + * + * @param PhpCsFile $phpcsFile + * @param int $stackPtr + * + * @return void + */ + protected function processInterfaces(PhpCsFile $phpcsFile, $stackPtr) + { + $interfaces = $phpcsFile->findImplementedInterfaceNames($phpcsFile->findPrevious(T_CLASS, $stackPtr)); + if ($interfaces === false) { + return; + } + + foreach ($interfaces as $interface) { + if (! $this->isLegacyClassname($interface)) { + continue; + } + + $position = $phpcsFile->findNext(T_STRING, $stackPtr, null, false, $interface); + if ($position === false) { + continue; + } + + $this->addFixableError($phpcsFile, $position, $interface); + } + } } From 6a115bd8d02711aa2d82cee023bba873b74652ad Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 16:24:22 +0100 Subject: [PATCH 12/16] TASK: Cleanup before PR * Adjust some comments and formating. * Add missing, but used, properties. * Shorten variables used only once. Resolves: #36 --- .../Sniffs/LegacyClassnames/AbstractClassnameChecker.php | 6 ++++++ .../Typo3Update/Sniffs/LegacyClassnames/Mapping.php | 8 ++------ .../Sniffs/LegacyClassnames/MissingNamespaceSniff.php | 5 +++-- .../MissingVendorForPluginsAndModulesSniff.php | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index 218ccee..a01da60 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -43,6 +43,11 @@ abstract class AbstractClassnameChecker implements PhpCsSniff */ public $legacyExtensions = ['Extbase', 'Fluid']; + /** + * @var Mapping + */ + protected $legacyMapping; + public function __construct() { $this->legacyMapping = Mapping::getInstance(); @@ -205,6 +210,7 @@ abstract class AbstractClassnameChecker implements PhpCsSniff * @param PhpCsFile $phpcsFile * @param int $classnamePosition * @param string $classname + * @param bool $forceEmptyPrefix Defines whether '\\' prefix should be checked or always be left out. */ protected function replaceLegacyClassname(PhpCsFile $phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix = false) { diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php index e704584..c7c455e 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php @@ -50,9 +50,7 @@ class Mapping } private function __construct() { - $mappingFile = $this->getMappingFile(); - - $this->mappings = require $mappingFile; + $this->mappings = require $this->getMappingFile(); } // Singleton implementation - End @@ -112,10 +110,8 @@ class Mapping return; } - $mappingFile = $this->getMappingFile(); - file_put_contents( - $mappingFile, + $this->getMappingFile(), 'mappings, true) . ';' ); } diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php index 5374dd3..3e3acac 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php @@ -92,6 +92,7 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract * @param PhpCsFile $phpcsFile * @param int $classnamePosition * @param string $classname + * @param bool $forceEmptyPrefix Defines whether '\\' prefix should be checked or always be left out. */ protected function replaceLegacyClassname(PhpCsFile $phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix = true) { @@ -138,9 +139,9 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract } /** - * Returns namespace, without vendor, based on legacy class name. + * Returns namespace, based on legacy class name. * - * E.g. Tx_ExtName_FolderName_FileName -> ExtName\FolderName + * E.g. Tx_ExtName_FolderName_FileName -> VENDOR\ExtName\FolderName * * @param string $classname * @return string diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php index 072bf62..4c76a90 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php @@ -84,7 +84,7 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff if ($fix === true) { $phpcsFile->fixer->replaceToken( $firstArgument, - "'{$this->getVendor()}.' . " . $tokens[$firstArgument]['content'] + "'{$this->getVendor()}.' . {$tokens[$firstArgument]['content']}" ); } } From 842a54d5d98c7f3bd288fa1838ea046c586b0716 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 16:40:03 +0100 Subject: [PATCH 13/16] TASK: Fix quality issues --- .../LegacyClassnames/AbstractClassnameChecker.php | 8 ++++++-- .../Sniffs/LegacyClassnames/InlineCommentSniff.php | 3 ++- .../Typo3Update/Sniffs/LegacyClassnames/Mapping.php | 7 ++++++- .../LegacyClassnames/MissingNamespaceSniff.php | 13 +++++++++---- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index a01da60..2e9896b 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -212,8 +212,12 @@ abstract class AbstractClassnameChecker implements PhpCsSniff * @param string $classname * @param bool $forceEmptyPrefix Defines whether '\\' prefix should be checked or always be left out. */ - protected function replaceLegacyClassname(PhpCsFile $phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix = false) - { + protected function replaceLegacyClassname( + PhpCsFile $phpcsFile, + $classnamePosition, + $classname, + $forceEmptyPrefix = false + ) { $prefix = '\\'; if ($forceEmptyPrefix || $phpcsFile->getTokens()[$classnamePosition -1]['code'] === T_NS_SEPARATOR) { $prefix = ''; diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php index 2b7caaf..fcf9229 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php @@ -60,7 +60,8 @@ class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff extends AbstractCla $this->originalTokenContent = $tokens[$stackPtr]['content']; $commentParts = preg_split('/\s+/', $this->originalTokenContent); - if (count($commentParts) !== 5 || $commentParts[1] !== '@var' || ($commentParts[2][0] !== '$' && $commentParts[3][0] !== '$')) { + if (count($commentParts) !== 5 || $commentParts[1] !== '@var' + || ($commentParts[2][0] !== '$' && $commentParts[3][0] !== '$')) { return; } diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php index c7c455e..16a8acf 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/Mapping.php @@ -28,12 +28,17 @@ use Typo3Update\Sniffs\OptionsAccessTrait; * Will check the configured file for whether a class is legacy and provides further methods. * Also can update to add new migrated class names. */ -class Mapping +final class Mapping { use OptionsAccessTrait; // Singleton implementation - Start static protected $instance = null; + /** + * Get the singleton instance. + * + * @return Mapping + */ public static function getInstance() { if (static::$instance === null) { diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php index 3e3acac..3f8a197 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingNamespaceSniff.php @@ -59,11 +59,12 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract return; } $classnamePosition = $phpcsFile->findNext(T_STRING, $stackPtr); + if ($classnamePosition === false) { return; } - $classname = $tokens[$classnamePosition]['content']; + $classname = $tokens[$classnamePosition]['content']; $this->addFixableError($phpcsFile, $classnamePosition, $classname); } @@ -94,8 +95,12 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract * @param string $classname * @param bool $forceEmptyPrefix Defines whether '\\' prefix should be checked or always be left out. */ - protected function replaceLegacyClassname(PhpCsFile $phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix = true) - { + protected function replaceLegacyClassname( + PhpCsFile $phpcsFile, + $classnamePosition, + $classname, + $forceEmptyPrefix = true + ) { parent::replaceLegacyClassname($phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix); $tokens = $phpcsFile->getTokens(); @@ -118,7 +123,7 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract /** * @param PhpCsFile $phpcsFile - * @return int + * @return int|false */ protected function getNamespacePosition(PhpCsFile $phpcsFile) { From 426dc8b318f90ce0d7ec02571859d819a41dc57a Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 16:48:28 +0100 Subject: [PATCH 14/16] BUGFIX: Define property where it's used * As we use it in parent class, define it there. --- .../Sniffs/LegacyClassnames/AbstractClassnameChecker.php | 9 +++++++++ .../Sniffs/LegacyClassnames/DocCommentSniff.php | 6 ------ .../Sniffs/LegacyClassnames/InlineCommentSniff.php | 6 ------ .../InstantiationWithMakeInstanceSniff.php | 6 ------ .../Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php | 6 ------ .../MissingVendorForPluginsAndModulesSniff.php | 6 ------ 6 files changed, 9 insertions(+), 30 deletions(-) diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php index 2e9896b..a13fe15 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/AbstractClassnameChecker.php @@ -48,6 +48,15 @@ abstract class AbstractClassnameChecker implements PhpCsSniff */ protected $legacyMapping; + /** + * 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() { $this->legacyMapping = Mapping::getInstance(); diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff.php index 7edd478..2a85c89 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/DocCommentSniff.php @@ -35,12 +35,6 @@ class Typo3Update_Sniffs_LegacyClassnames_DocCommentSniff extends AbstractClassn */ public $allowedTags = ['@param', '@return', '@var']; - /** - * Original token content for reuse accross methods. - * @var string - */ - protected $originalTokenContent = ''; - /** * Returns the token types that this sniff is interested in. * diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php index fcf9229..dc9c17d 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InlineCommentSniff.php @@ -27,12 +27,6 @@ use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; */ class Typo3Update_Sniffs_LegacyClassnames_InlineCommentSniff extends AbstractClassnameChecker { - /** - * Original token content for reuse accross methods. - * @var string - */ - protected $originalTokenContent = ''; - /** * Returns the token types that this sniff is interested in. * diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php index 169716e..7e06b6b 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php @@ -30,12 +30,6 @@ class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff ext { use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; - /** - * Original token content for reuse accross methods. - * @var string - */ - protected $originalTokenContent = ''; - /** * Returns the token types that this sniff is interested in. * diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php index b169ac4..b4a2f30 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/IsACallSniff.php @@ -29,12 +29,6 @@ class Typo3Update_Sniffs_LegacyClassnames_IsACallSniff extends AbstractClassname { use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; - /** - * Original token content for reuse accross methods. - * @var string - */ - protected $originalTokenContent = ''; - /** * Returns the token types that this sniff is interested in. * diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php index 4c76a90..3f2825f 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/MissingVendorForPluginsAndModulesSniff.php @@ -31,12 +31,6 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; use \Typo3Update\Sniffs\OptionsAccessTrait; - /** - * Original token content for reuse accross methods. - * @var string - */ - protected $originalTokenContent = ''; - /** * Returns the token types that this sniff is interested in. * From 69de131a4c9c71b8b248af883578fed24065acd6 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 28 Mar 2017 08:40:19 +0200 Subject: [PATCH 15/16] TASK: Improve readme --- Readme.rst | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/Readme.rst b/Readme.rst index 6a90ec5..ce6c08f 100644 --- a/Readme.rst +++ b/Readme.rst @@ -69,29 +69,29 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - ``catch`` of legacy class names. -- Convert old legacy class definitions in extensions to namespace ones. +- Convert old legacy class *definitions* in extensions to namespaces. - Convert usage of previously converted class definitions. On first run the definition will be - converted, on second the usage. This is due to the fact, that PHPCS might find the definition + converted, on second run the usage. This is due to the fact, that PHPCS might find the definition after the usage, so please run twice. *NOTE* The configured file will be updated after each run, for each converted class, trait and - interface definition. + interface definition. See options. - Add missing vendor to plugin and module registrations and configurations. You might want to set this to non fixable and warning if you already provide the vendor inside a single Variable, together with your extension key, as this is not recognized. So the following will be recognized: - - $_EXTKEY, + - ``$_EXTKEY,`` - - $VENDOR . $_EXTKEY, + - ``$VENDOR . $_EXTKEY,`` - - 'VENDOR.' . $_EXTKEY, + - ``'VENDOR.' . $_EXTKEY,`` While the following will not: - - $key = 'Vendor.' . $_EXTKEY; + - ``$key = 'Vendor.' . $_EXTKEY;`` Also we check for the following deprecated calls: @@ -161,22 +161,6 @@ Typo3Update.LegacyClassnames.DocComment: ``allowedTags`` -``vendor`` - Configure your vendor through ``ruleset.xml`` or using ``--runtime-set``. Default is - ``YourCompany``. - - Example: - -.. code:: xml - - - -Example: - -.. code:: bash - - --runtime-set vendor YourVendor - ``mappingFile`` Configure where the `LegacyClassnames.php` is located, through ``ruleset.xml`` or using ``--runtime-set``. Default is `LegacyClassnames.php` in the project root. @@ -192,3 +176,19 @@ Example: .. code:: bash --runtime-set mappingFile /projects/typo3_installation/vendor/composer/autoload_classaliasmap.php + +``vendor`` + Configure your vendor through ``ruleset.xml`` or using ``--runtime-set``. Default is + ``YourCompany``. + + Example: + +.. code:: xml + + + +Example: + +.. code:: bash + + --runtime-set vendor YourVendor From 3cc85feec794ce208b1a1ac8a241be5e1bfabcdf Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 28 Mar 2017 08:48:43 +0200 Subject: [PATCH 16/16] TASK: Extend readme --- Readme.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Readme.rst b/Readme.rst index ce6c08f..a7a1243 100644 --- a/Readme.rst +++ b/Readme.rst @@ -49,6 +49,8 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - Static calls like ``t3lib_div::`` to ``\TYPO3\Core\Utility\GeneralUtility``. +- Static call also checks for ``::class``, as technically we just look before the ``::``. + - Typehints in methods and function like injects. - ``instanceof`` checks.