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
*/
protected static $removedFunctions = [];
protected static $configuredFunctions = [];
/**
* Function for the current sniff instance.
* @var array
*/
private $removedFunction = [];
private $removedFunctions = [];
/**
* TODO: Multiple files allowed, using glob ...
@ -54,10 +54,10 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
*/
public function __construct()
{
if (static::$removedFunctions === []) {
if (static::$configuredFunctions === []) {
foreach ($this->getRemovedFunctionConfigFiles() as $file) {
static::$removedFunctions = array_merge(
static::$removedFunctions,
static::$configuredFunctions = array_merge(
static::$configuredFunctions,
$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.
*
* @param array $oldStructure
* @param array $typo3Versions
* @return array
*/
protected function prepareStructure(array $oldStructure)
protected function prepareStructure(array $typo3Versions)
{
$typo3Versions = array_keys($oldStructure);
$newStructure = [];
foreach ($typo3Versions as $typo3Version) {
foreach ($oldStructure[$typo3Version] as $function => $config) {
foreach ($typo3Versions as $typo3Version => $functions) {
foreach ($functions as $function => $config) {
// Split static methods and methods.
$split = preg_split('/::|->/', $function);
@ -164,13 +163,12 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
}
}
$this->detectRemovedFunctions($functionName, $class, $isStatic)
return $this->removedFunction !== [];
$this->removedFunctions = $this->getMatchingRemovedFunctions($functionName, $class, $isStatic);
return $this->removedFunctions !== [];
}
/**
* Detect removed functions for given arguments.
* Returns all matching removed functions for given arguments.
*
* @param string $functionName
* @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
*/
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.
// Otherwise we could handle them the same way as instance methods.
@ -186,8 +184,8 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
return;
}
$this->removedFunction = array_filter(
static::$removedFunctions,
return array_filter(
static::$configuredFunctions,
function ($config) use ($functionName, $isStatic, $className) {
return $functionName === $config['function']
&& $isStatic === $config['static']
@ -195,25 +193,11 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
$className === $config['class']
|| $className === false
)
;
;
}
);
}
/**
* 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.
*
@ -224,27 +208,30 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
*/
protected function addWarning(PhpCsFile $phpcsFile, $tokenPosition)
{
$phpcsFile->addWarning(
'Legacy function calls are not allowed; found %s. Removed in %s. %s. See: %s',
$tokenPosition,
$this->getFunctionIdentifier(),
[
$this->getOldfunctionCall(),
$this->getRemovedVersion(),
$this->getNewFunctionCall(),
$this->getDocsUrl(),
]
);
foreach ($this->removedFunctions as $function) {
$phpcsFile->addWarning(
'Legacy function calls are not allowed; found %s. Removed in %s. %s. See: %s',
$tokenPosition,
$this->getFunctionIdentifier($function),
[
$this->getOldfunctionCall($function),
$this->getRemovedVersion($function),
$this->getNewFunctionCall($function),
$this->getDocsUrl($function),
]
);
}
}
/**
* Identifier for configuring this specific error / warning through PHPCS.
*
* @param array $config The converted structure for a single function.
*
* @return string
*/
protected function getFunctionIdentifier()
protected function getFunctionIdentifier(array $config)
{
$config = $this->getCurrentRemovedFunction();
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
* legacy one.
*
* @param array $config The converted structure for a single function.
*
* @return string
*/
protected function getOldFunctionCall()
protected function getOldFunctionCall(array $config)
{
$config = $this->getCurrentRemovedFunction();
$concat = '->';
if ($config['static']) {
$concat = '::';
@ -272,11 +260,13 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff implements PhpCsSniff
*
* To let user decide whether this is important for him.
*
* @param array $config The converted structure for a single function.
*
* @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.
*
* @param array $config The converted structure for a single function.
*
* @return string
*/
protected function getNewFunctionCall()
protected function getNewFunctionCall(array $config)
{
$newCall = $this->getCurrentRemovedFunction()['newFunctionCall'];
$newCall = $config['newFunctionCall'];
if ($newCall !== null) {
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.
*
* @param array $config The converted structure for a single function.
*
* @return string
*/
protected function getDocsUrl()
protected function getDocsUrl(array $config)
{
return $this->getCurrentRemovedFunction()['docsUrl'];
return $config['docsUrl'];
}
}