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
This commit is contained in:
parent
080816c530
commit
187dc918a9
2 changed files with 111 additions and 0 deletions
|
@ -60,6 +60,15 @@ new ones like ``\TYPO3\Extbase\...``. This is done for:
|
||||||
|
|
||||||
- Instantiation through ``makeInstance``.
|
- 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?
|
What does it look like?
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,102 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
|
||||||
|
*
|
||||||
|
* 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<int>
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue