TASK: Skip unnecessary function call

This commit is contained in:
Daniel Siepmann 2017-04-04 08:31:54 +02:00
parent 830584fa87
commit 034ab57a34
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4

View file

@ -40,13 +40,13 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
* *
* @var array * @var array
*/ */
protected static $removedFunctions = []; protected static $configuredFunctions = [];
/** /**
* Function for the current sniff instance. * Function for the current sniff instance.
* @var array * @var array
*/ */
private $removedFunction = []; private $removedFunctions = [];
/** /**
* TODO: Multiple files allowed, using glob ... * TODO: Multiple files allowed, using glob ...
@ -54,10 +54,10 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
*/ */
public function __construct() public function __construct()
{ {
if (static::$removedFunctions === []) { if (static::$configuredFunctions === []) {
foreach ($this->getRemovedFunctionConfigFiles() as $file) { foreach ($this->getRemovedFunctionConfigFiles() as $file) {
static::$removedFunctions = array_merge( static::$configuredFunctions = array_merge(
static::$removedFunctions, static::$configuredFunctions,
$this->prepareStructure(Yaml::parse(file_get_contents((string) $file))) $this->prepareStructure(Yaml::parse(file_get_contents((string) $file)))
); );
} }
@ -67,16 +67,15 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
/** /**
* Prepares structure from config for later usage. * Prepares structure from config for later usage.
* *
* @param array $oldStructure * @param array $typo3Versions
* @return array * @return array
*/ */
protected function prepareStructure(array $oldStructure) protected function prepareStructure(array $typo3Versions)
{ {
$typo3Versions = array_keys($oldStructure);
$newStructure = []; $newStructure = [];
foreach ($typo3Versions as $typo3Version) { foreach ($typo3Versions as $typo3Version => $functions) {
foreach ($oldStructure[$typo3Version] as $function => $config) { foreach ($functions as $function => $config) {
// Split static methods and methods. // Split static methods and methods.
$split = preg_split('/::|->/', $function); $split = preg_split('/::|->/', $function);
@ -164,13 +163,12 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
} }
} }
$this->detectRemovedFunctions($functionName, $class, $isStatic) $this->removedFunctions = $this->getMatchingRemovedFunctions($functionName, $class, $isStatic);
return $this->removedFunctions !== [];
return $this->removedFunction !== [];
} }
/** /**
* Detect removed functions for given arguments. * Returns all matching removed functions for given arguments.
* *
* @param string $functionName * @param string $functionName
* @param string $className The last part of the class name, splitted by namespaces. * @param string $className The last part of the class name, splitted by namespaces.
@ -178,7 +176,7 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
* *
* @return void * @return void
*/ */
protected function detectRemovedFunctions($functionName, $className, $isStatic) protected function getMatchingRemovedFunctions($functionName, $className, $isStatic)
{ {
// We will not match any static method, without the class name, at least for now. // We will not match any static method, without the class name, at least for now.
// Otherwise we could handle them the same way as instance methods. // Otherwise we could handle them the same way as instance methods.
@ -186,8 +184,8 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
return; return;
} }
$this->removedFunction = array_filter( return array_filter(
static::$removedFunctions, static::$configuredFunctions,
function ($config) use ($functionName, $isStatic, $className) { function ($config) use ($functionName, $isStatic, $className) {
return $functionName === $config['function'] return $functionName === $config['function']
&& $isStatic === $config['static'] && $isStatic === $config['static']
@ -200,20 +198,6 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
); );
} }
/**
* Returns configuration for currently checked function.
*
* @return array
*/
protected function getCurrentRemovedFunction()
{
$config = current($this->removedFunction);
// TODO: Add exception if something went wrong?
return $config;
}
/** /**
* Add warning for the given token position. * Add warning for the given token position.
* *
@ -224,27 +208,30 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
*/ */
protected function addWarning(PhpCsFile $phpcsFile, $tokenPosition) protected function addWarning(PhpCsFile $phpcsFile, $tokenPosition)
{ {
foreach ($this->removedFunctions as $function) {
$phpcsFile->addWarning( $phpcsFile->addWarning(
'Legacy function calls are not allowed; found %s. Removed in %s. %s. See: %s', 'Legacy function calls are not allowed; found %s. Removed in %s. %s. See: %s',
$tokenPosition, $tokenPosition,
$this->getFunctionIdentifier(), $this->getFunctionIdentifier($function),
[ [
$this->getOldfunctionCall(), $this->getOldfunctionCall($function),
$this->getRemovedVersion(), $this->getRemovedVersion($function),
$this->getNewFunctionCall(), $this->getNewFunctionCall($function),
$this->getDocsUrl(), $this->getDocsUrl($function),
] ]
); );
} }
}
/** /**
* Identifier for configuring this specific error / warning through PHPCS. * Identifier for configuring this specific error / warning through PHPCS.
* *
* @param array $config The converted structure for a single function.
*
* @return string * @return string
*/ */
protected function getFunctionIdentifier() protected function getFunctionIdentifier(array $config)
{ {
$config = $this->getCurrentRemovedFunction();
return $config['class'] . '.' . $config['function']; return $config['class'] . '.' . $config['function'];
} }
@ -255,11 +242,12 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
* you should provide an example, so users can check that this is the * you should provide an example, so users can check that this is the
* legacy one. * legacy one.
* *
* @param array $config The converted structure for a single function.
*
* @return string * @return string
*/ */
protected function getOldFunctionCall() protected function getOldFunctionCall(array $config)
{ {
$config = $this->getCurrentRemovedFunction();
$concat = '->'; $concat = '->';
if ($config['static']) { if ($config['static']) {
$concat = '::'; $concat = '::';
@ -272,11 +260,13 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
* *
* To let user decide whether this is important for him. * To let user decide whether this is important for him.
* *
* @param array $config The converted structure for a single function.
*
* @return string * @return string
*/ */
protected function getRemovedVersion() protected function getRemovedVersion(array $config)
{ {
return $this->getCurrentRemovedFunction()['version_removed']; return $config['version_removed'];
} }
/** /**
@ -284,11 +274,13 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
* *
* To provide feedback for user to ease migration. * To provide feedback for user to ease migration.
* *
* @param array $config The converted structure for a single function.
*
* @return string * @return string
*/ */
protected function getNewFunctionCall() protected function getNewFunctionCall(array $config)
{ {
$newCall = $this->getCurrentRemovedFunction()['newFunctionCall']; $newCall = $config['newFunctionCall'];
if ($newCall !== null) { if ($newCall !== null) {
return $newCall; return $newCall;
} }
@ -298,10 +290,12 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
/** /**
* Allow user to lookup the official docs related to this deprecation / breaking change. * Allow user to lookup the official docs related to this deprecation / breaking change.
* *
* @param array $config The converted structure for a single function.
*
* @return string * @return string
*/ */
protected function getDocsUrl() protected function getDocsUrl(array $config)
{ {
return $this->getCurrentRemovedFunction()['docsUrl']; return $config['docsUrl'];
} }
} }