From f9f82b06292ac05dc86f1664a085ce1caafaf499 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 23 Mar 2017 10:55:09 +0100 Subject: [PATCH] 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, '\\/'); + } +}