From 187dc918a98e551e86ecc8873be394f5af0fa542 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 16 Mar 2017 14:12:58 +0100 Subject: [PATCH] FEATURE: Migrate instantiation through object manager * Mark "create" as deprecated with a warning. * Migrate first argument of "create" and "get" calls if they are deprecated. Resolves: #4 --- Readme.rst | 9 ++ .../InstantiationWithObjectManagerSniff.php | 102 ++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/Standards/Typo3Update/Sniffs/LegacyClassnames/InstantiationWithObjectManagerSniff.php diff --git a/Readme.rst b/Readme.rst index 1991336..7d6a6cf 100644 --- a/Readme.rst +++ b/Readme.rst @@ -60,6 +60,15 @@ new ones like ``\TYPO3\Extbase\...``. This is done for: - Instantiation through ``makeInstance``. +- 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. + +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/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); + } +}