FEATURE: Migrate legacy class names for insteanceof
* Check classes after instanceof token and migrate them when possible Resolves: #7
This commit is contained in:
parent
1a17e160b5
commit
097fd7522c
2 changed files with 119 additions and 0 deletions
|
@ -33,6 +33,16 @@ trait ClassnameCheckerTrait
|
||||||
*/
|
*/
|
||||||
private $legacyClassnames = [];
|
private $legacyClassnames = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of extension names that might contain legacy class names.
|
||||||
|
* Used to check clas names for warnings.
|
||||||
|
*
|
||||||
|
* @var array<string>
|
||||||
|
*/
|
||||||
|
private $legacyExtensions = [
|
||||||
|
'Extbase',
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $mappingFile File containing php array for mapping.
|
* @param string $mappingFile File containing php array for mapping.
|
||||||
*/
|
*/
|
||||||
|
@ -59,6 +69,31 @@ trait ClassnameCheckerTrait
|
||||||
return isset($this->legacyClassnames[strtolower($classname)]);
|
return isset($this->legacyClassnames[strtolower($classname)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guesses whether the given classname is legacy. Will not check
|
||||||
|
* isLegacyClassname
|
||||||
|
*
|
||||||
|
* @param string $classname
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isMaybeLegacyClassname($classname)
|
||||||
|
{
|
||||||
|
if (strpos($classname, 'Tx_') === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$extensionName = call_user_func(function ($classname) {
|
||||||
|
$nameParts = explode('_', $classname);
|
||||||
|
return $nameParts[1];
|
||||||
|
}, $classname);
|
||||||
|
|
||||||
|
if (!in_array($extensionName, $this->legacyExtensions)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $classname
|
* @param string $classname
|
||||||
* @return string
|
* @return string
|
||||||
|
@ -78,6 +113,8 @@ trait ClassnameCheckerTrait
|
||||||
*/
|
*/
|
||||||
public function addFixableError(PhpcsFile $phpcsFile, $classnamePosition, $classname)
|
public function addFixableError(PhpcsFile $phpcsFile, $classnamePosition, $classname)
|
||||||
{
|
{
|
||||||
|
$this->addMaybeWarning($phpcsFile, $classnamePosition, $classname);
|
||||||
|
|
||||||
if ($this->isLegacyClassname($classname) === false) {
|
if ($this->isLegacyClassname($classname) === false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -97,6 +134,27 @@ trait ClassnameCheckerTrait
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an warning if given $classname is maybe legacy.
|
||||||
|
*
|
||||||
|
* @param PhpcsFile $phpcsFile
|
||||||
|
* @param int $classnamePosition
|
||||||
|
* @param string $classname
|
||||||
|
*/
|
||||||
|
private function addMaybeWarning(PhpcsFile $phpcsFile, $classnamePosition, $classname)
|
||||||
|
{
|
||||||
|
if ($this->isLegacyClassname($classname) || $this->isMaybeLegacyClassname($classname) === false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$phpcsFile->addWarning(
|
||||||
|
'Legacy classes are not allowed; found %s that might be a legacy class that does not exist anymore',
|
||||||
|
$classnamePosition,
|
||||||
|
'mightBeLegacyClassname',
|
||||||
|
[$classname]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String to use for replacing / fixing the token.
|
* String to use for replacing / fixing the token.
|
||||||
* Default is class name itself, can be overwritten in sniff for special behaviour.
|
* Default is class name itself, can be overwritten in sniff for special behaviour.
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
<?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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detect and migrate extend and implement of old legacy classnames.
|
||||||
|
*/
|
||||||
|
class Typo3Update_Sniffs_LegacyClassnames_InstanceofSniff implements PHP_CodeSniffer_Sniff
|
||||||
|
{
|
||||||
|
use \Typo3Update\Sniffs\LegacyClassnames\ClassnameCheckerTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the token types that this sniff is interested in.
|
||||||
|
*
|
||||||
|
* @return array<int>
|
||||||
|
*/
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
T_INSTANCEOF,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue