|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* \PHPCompatibility\Sniffs\FunctionParameters\NegativeStringOffsetSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHPCompatibility |
|
9
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace PHPCompatibility\Sniffs\FunctionParameters; |
|
13
|
|
|
|
|
14
|
|
|
use PHPCompatibility\AbstractFunctionCallParameterSniff; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* \PHPCompatibility\Sniffs\FunctionParameters\NegativeStringOffsetSniff. |
|
18
|
|
|
* |
|
19
|
|
|
* Detect: negative string offsets as parameters passed to functions where this |
|
20
|
|
|
* was not allowed prior to PHP 7.1. |
|
21
|
|
|
* |
|
22
|
|
|
* PHP version 7.1 |
|
23
|
|
|
* |
|
24
|
|
|
* @category PHP |
|
25
|
|
|
* @package PHPCompatibility |
|
26
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class NegativeStringOffsetSniff extends AbstractFunctionCallParameterSniff |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Functions to check for. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array Function name => 1-based parameter offset of the affected parameters => parameter name. |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $targetFunctions = array( |
|
37
|
|
|
'file_get_contents' => array( |
|
38
|
|
|
4 => 'offset', |
|
39
|
|
|
), |
|
40
|
|
|
'grapheme_extract' => array( |
|
41
|
|
|
4 => 'start', |
|
42
|
|
|
), |
|
43
|
|
|
'grapheme_stripos' => array( |
|
44
|
|
|
3 => 'offset', |
|
45
|
|
|
), |
|
46
|
|
|
'grapheme_strpos' => array( |
|
47
|
|
|
3 => 'offset', |
|
48
|
|
|
), |
|
49
|
|
|
'iconv_strpos' => array( |
|
50
|
|
|
3 => 'offset', |
|
51
|
|
|
), |
|
52
|
|
|
'mb_ereg_search_setpos' => array( |
|
53
|
|
|
1 => 'position', |
|
54
|
|
|
), |
|
55
|
|
|
'mb_strimwidth' => array( |
|
56
|
|
|
2 => 'start', |
|
57
|
|
|
3 => 'width', |
|
58
|
|
|
), |
|
59
|
|
|
'mb_stripos' => array( |
|
60
|
|
|
3 => 'offset', |
|
61
|
|
|
), |
|
62
|
|
|
'mb_strpos' => array( |
|
63
|
|
|
3 => 'offset', |
|
64
|
|
|
), |
|
65
|
|
|
'stripos' => array( |
|
66
|
|
|
3 => 'offset', |
|
67
|
|
|
), |
|
68
|
|
|
'strpos' => array( |
|
69
|
|
|
3 => 'offset', |
|
70
|
|
|
), |
|
71
|
|
|
'substr_count' => array( |
|
72
|
|
|
3 => 'offset', |
|
73
|
|
|
4 => 'length', |
|
74
|
|
|
), |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Do a version check to determine if this sniff needs to run at all. |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function bowOutEarly() |
|
84
|
|
|
{ |
|
85
|
|
|
return ($this->supportsBelow('7.0') === false); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Process the parameters of a matched function. |
|
90
|
|
|
* |
|
91
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
92
|
|
|
* @param int $stackPtr The position of the current token in the stack. |
|
93
|
|
|
* @param string $functionName The token content (function name) which was matched. |
|
94
|
|
|
* @param array $parameters Array with information about the parameters. |
|
95
|
|
|
* |
|
96
|
|
|
* @return int|void Integer stack pointer to skip forward or void to continue |
|
97
|
|
|
* normal file processing. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters) |
|
100
|
|
|
{ |
|
101
|
|
|
$functionLC = strtolower($functionName); |
|
102
|
|
|
foreach ($this->targetFunctions[$functionLC] as $pos => $name) { |
|
103
|
|
|
if (isset($parameters[$pos]) === false) { |
|
104
|
|
|
continue; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$targetParam = $parameters[$pos]; |
|
108
|
|
|
|
|
109
|
|
|
if ($this->isNegativeNumber($phpcsFile, $targetParam['start'], $targetParam['end']) === false) { |
|
110
|
|
|
continue; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$phpcsFile->addError( |
|
114
|
|
|
'Negative string offsets were not supported for the $%s parameter in %s() in PHP 7.0 or lower. Found %s', |
|
115
|
|
|
$targetParam['start'], |
|
116
|
|
|
'Found', |
|
117
|
|
|
array( |
|
118
|
|
|
$name, |
|
119
|
|
|
$functionName, |
|
120
|
|
|
$targetParam['raw'], |
|
121
|
|
|
) |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
}//end class |
|
126
|
|
|
|