Commit a02b7836 authored by jrfnl's avatar jrfnl
Browse files

Improve performance of the function call/array analysis sniffs.

This PR caches the results of the function call parameter / array parsing as this can be quite a "heavy" operation. The trade-off of doing this will be that the memory usage of the PHPCS run will go up.
parent 6a4ee6cc
......@@ -57,7 +57,6 @@ abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff
'hash' => 1,
);
/**
* List of functions which take an ini directive as parameter (always the first parameter).
*
......@@ -71,6 +70,16 @@ abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff
'ini_set' => 1,
);
/**
* Internal cache of the results of the `getFunctionCallParameters()` method.
*
* The results are stored in a multi-dimensional array by filename and $stackPtr of
* the function call/array token.
*
* @var array
*/
private $getFunctionCallParametersCache = array();
/**
* Get the testVersion configuration variable.
......@@ -478,7 +487,15 @@ abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff
*/
public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$currentFile = $phpcsFile->getFilename();
// Check if this function call/array has been parsed before and if so, use the cached results.
if (isset($this->getFunctionCallParametersCache[$currentFile][$stackPtr]) === true) {
return $this->getFunctionCallParametersCache[$currentFile][$stackPtr];
}
if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) {
$this->getFunctionCallParametersCache[$currentFile][$stackPtr] = array();
return array();
}
......@@ -552,6 +569,9 @@ abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff
$cnt++;
}
// Cache the results.
$this->getFunctionCallParametersCache[$currentFile][$stackPtr] = $parameters;
return $parameters;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment