From 1561c13b468d45afa6e44b42afde5d459a220a2f Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 11:27:40 +0100 Subject: [PATCH] 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; + } }