1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* \PHPCompatibility\Sniffs\FunctionParameters\ArrayReduceInitialTypeSniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PHPCompatibility\Sniffs\FunctionParameters; |
11
|
|
|
|
12
|
|
|
use PHPCompatibility\AbstractFunctionCallParameterSniff; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* \PHPCompatibility\Sniffs\FunctionParameters\ArrayReduceInitialTypeSniff. |
16
|
|
|
* |
17
|
|
|
* Detect: In PHP 5.2 and lower, the $initial parameter had to be an integer. |
18
|
|
|
* |
19
|
|
|
* @category PHP |
20
|
|
|
* @package PHPCompatibility |
21
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ArrayReduceInitialTypeSniff extends AbstractFunctionCallParameterSniff |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Functions to check for. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $targetFunctions = array( |
32
|
|
|
'array_reduce' => true, |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Tokens which, for the purposes of this sniff, indicate that there is |
37
|
|
|
* a variable element to the value passed. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $variableValueTokens = array( |
42
|
|
|
T_VARIABLE, |
43
|
|
|
T_STRING, |
44
|
|
|
T_SELF, |
45
|
|
|
T_PARENT, |
46
|
|
|
T_STATIC, |
47
|
|
|
T_DOUBLE_QUOTED_STRING, |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Do a version check to determine if this sniff needs to run at all. |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
protected function bowOutEarly() |
57
|
|
|
{ |
58
|
|
|
return ($this->supportsBelow('5.2') === false); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Process the parameters of a matched function. |
64
|
|
|
* |
65
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
66
|
|
|
* @param int $stackPtr The position of the current token in the stack. |
67
|
|
|
* @param string $functionName The token content (function name) which was matched. |
68
|
|
|
* @param array $parameters Array with information about the parameters. |
69
|
|
|
* |
70
|
|
|
* @return int|void Integer stack pointer to skip forward or void to continue |
71
|
|
|
* normal file processing. |
72
|
|
|
*/ |
73
|
|
|
public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters) |
74
|
|
|
{ |
75
|
|
|
if (isset($parameters[3]) === false) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$targetParam = $parameters[3]; |
80
|
|
|
if ($this->isNumber($phpcsFile, $targetParam['start'], $targetParam['end'], true) !== false) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($this->isNumericCalculation($phpcsFile, $targetParam['start'], $targetParam['end']) === true) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$error = 'Passing a non-integer as the value for $initial to array_reduce() is not supported in PHP 5.2 or lower.'; |
89
|
|
|
if ($phpcsFile->findNext($this->variableValueTokens, $targetParam['start'], ($targetParam['end'] + 1)) === false) { |
90
|
|
|
$phpcsFile->addError( |
91
|
|
|
$error . ' Found %s', |
92
|
|
|
$targetParam['start'], |
93
|
|
|
'InvalidTypeFound', |
94
|
|
|
array($targetParam['raw']) |
95
|
|
|
); |
96
|
|
|
} else { |
97
|
|
|
$phpcsFile->addWarning( |
98
|
|
|
$error . ' Variable value found. Found %s', |
99
|
|
|
$targetParam['start'], |
100
|
|
|
'VariableFound', |
101
|
|
|
array($targetParam['raw']) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|