diff --git a/Readme.rst b/Readme.rst index 9955444..4aba35d 100644 --- a/Readme.rst +++ b/Readme.rst @@ -56,6 +56,20 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - Inline comments for IDEs, e.g. ``/* @var $configurationManager Tx_Extbase_Configuration_ConfigurationManager */`` +- Instantiation through ``new``. + +- Instantiation through ``makeInstance``. Only Classnames in Strings are supported, no ``::class``. + +- Instantiation through ``ObjectManager``, check afterwards as this is static and all function calls + using ``get`` and ``create`` will be adjusted. Might be useful to exclude this sniff and run it + separately. + Only Classnames in Strings are supported, no ``::class``. + +Also we check for the following deprecated calls: + +- Check for ``create`` on ``ObjectManager``, which is "stupid" just all ``create`` calls are marked + with a warning. + What does it look like? ======================= diff --git a/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php b/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php new file mode 100644 index 0000000..2368d4d --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/ExtendedPhpCsSupportTrait.php @@ -0,0 +1,69 @@ + + * + * 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_Tokens as Tokens; + +/** + * Provide common uses for all sniffs. + */ +trait ExtendedPhpCsSupportTrait +{ + /** + * Check whether current stackPtr is a function call. + * + * Code taken from PEAR_Sniffs_Functions_FunctionCallSignatureSniff for reuse. + * + * @param PhpCsFile $phpcsFile + * @param int $stackPtr + * + * @return bool + */ + protected function isFunctionCall(PhpCsFile $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + // Find the next non-empty token. + $openBracket = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + + if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { + // Not a function call. + return false; + } + + if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { + // Not a function call. + return false; + } + + // Find the previous non-empty token. + $search = Tokens::$emptyTokens; + $search[] = T_BITWISE_AND; + $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true); + if ($tokens[$previous]['code'] === T_FUNCTION) { + // It's a function definition, not a function call. + return false; + } + + return true; + } +} diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php index 8d7f6a7..a32c574 100644 --- a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/ClassnameCheckerTrait.php @@ -21,9 +21,10 @@ namespace Typo3Update\Sniffs\LegacyClassnames; */ use PHP_CodeSniffer_File as PhpcsFile; +use PHP_CodeSniffer_Tokens as Tokens; /** - * Provide common uses for all sniffs. + * Provide common uses for all sniffs, regarding class name checks. */ trait ClassnameCheckerTrait { diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php new file mode 100644 index 0000000..94bb175 --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithMakeInstanceSniff.php @@ -0,0 +1,98 @@ + + * + * 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_Tokens as Tokens; + +/** + * Detect and migrate static calls to old legacy classnames. + */ +class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithMakeInstanceSniff implements PHP_CodeSniffer_Sniff +{ + use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; + 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. + * + * @return array + */ + public function register() + { + return Tokens::$functionNameTokens; + } + + /** + * 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) + { + if (!$this->isFunctionCall($phpcsFile, $stackPtr)) { + return; + } + $tokens = $phpcsFile->getTokens(); + + if ($tokens[$stackPtr]['content'] !== 'makeInstance') { + return; + } + + $classnamePosition = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr); + if ($classnamePosition === false) { + return; + } + + $classname = trim($tokens[$classnamePosition]['content'], '\'"'); + $this->originalTokenContent = $tokens[$classnamePosition]['content']; + $this->addFixableError($phpcsFile, $classnamePosition, $classname); + } + + /** + * As token contains more then just class name, we have to build new content ourself. + * + * @param string $classname + * @return string + */ + protected function getTokenForReplacement($classname) + { + $stringSign = $this->originalTokenContent[0]; + $token = explode($stringSign, $this->originalTokenContent); + $token[1] = $classname; + + // Migrate double quote to single quote. + // This way no escaping of backslashes in class names is necessary. + if ($stringSign === '"') { + $stringSign = "'"; + } + + return implode($stringSign, $token); + } +} diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithNewSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithNewSniff.php new file mode 100644 index 0000000..d98dc79 --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithNewSniff.php @@ -0,0 +1,59 @@ + + * + * 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 static calls to old legacy classnames. + */ +class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithNewSniff 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_NEW]; + } + + /** + * 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); + } +} diff --git a/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithObjectManagerSniff.php b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithObjectManagerSniff.php new file mode 100644 index 0000000..c16b47d --- /dev/null +++ b/src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithObjectManagerSniff.php @@ -0,0 +1,102 @@ + + * + * 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_Tokens as Tokens; + +/** + * Detect and migrate static calls to old legacy classnames. + */ +class Typo3Update_Sniffs_LegacyClassnames_InstantiationWithObjectManagerSniff implements PHP_CodeSniffer_Sniff +{ + use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait; + use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; + + /** + * 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 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) + { + if (!$this->isFunctionCall($phpcsFile, $stackPtr)) { + return; + } + $tokens = $phpcsFile->getTokens(); + + $functionName = $tokens[$stackPtr]['content']; + if (!in_array($functionName, ['get', 'create'])) { + return; + } + + $classnamePosition = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $stackPtr); + if ($classnamePosition === false) { + return; + } + + if ($functionName === 'create') { + $phpcsFile->addWarning( + 'The "create" method of ObjectManager is no longer supported, please migrate to "get".', + $stackPtr, + 'mightBeDeprecatedMethod', + ['create'] + ); + } + + $classname = trim($tokens[$classnamePosition]['content'], '\'"'); + $this->originalTokenContent = $tokens[$classnamePosition]['content']; + $this->addFixableError($phpcsFile, $classnamePosition, $classname); + } + + /** + * As token contains more then just class name, we have to build new content ourself. + * + * @param string $classname + * @return string + */ + protected function getTokenForReplacement($classname) + { + $stringSign = $this->originalTokenContent[0]; + $token = explode($stringSign, $this->originalTokenContent); + $token[1] = $classname; + + // Migrate double quote to single quote. + // This way no escaping of backslashes in class names is necessary. + if ($stringSign === '"') { + $stringSign = "'"; + } + + return implode($stringSign, $token); + } +}