|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* \PHPCompatibility\Sniffs\PHP\PCRENewModifiers. |
|
4
|
|
|
* |
|
5
|
|
|
* @category PHP |
|
6
|
|
|
* @package PHPCompatibility |
|
7
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace PHPCompatibility\Sniffs\PHP; |
|
11
|
|
|
|
|
12
|
|
|
use PHPCompatibility\Sniffs\PHP\PregReplaceEModifierSniff; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* \PHPCompatibility\Sniffs\PHP\PCRENewModifiers. |
|
16
|
|
|
* |
|
17
|
|
|
* Check for usage of newly added regex modifiers for PCRE functions. |
|
18
|
|
|
* |
|
19
|
|
|
* @category PHP |
|
20
|
|
|
* @package PHPCompatibility |
|
21
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class PCRENewModifiersSniff extends PregReplaceEModifierSniff |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Functions to check for. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $functions = array( |
|
32
|
|
|
'preg_replace' => true, |
|
33
|
|
|
'preg_filter' => true, |
|
34
|
|
|
'preg_grep' => true, |
|
35
|
|
|
'preg_match_all' => true, |
|
36
|
|
|
'preg_match' => true, |
|
37
|
|
|
'preg_replace_callback_array' => true, |
|
38
|
|
|
'preg_replace_callback' => true, |
|
39
|
|
|
'preg_replace' => true, |
|
40
|
|
|
'preg_split' => true, |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Array listing newly introduced regex modifiers. |
|
45
|
|
|
* |
|
46
|
|
|
* The key should be the modifier (case-sensitive!). |
|
47
|
|
|
* The value should be the PHP version in which the modifier was introduced. |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $newModifiers = array( |
|
52
|
|
|
'J' => array( |
|
53
|
|
|
'7.1' => false, |
|
54
|
|
|
'7.2' => true, |
|
55
|
|
|
), |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Do a version check to determine if this sniff needs to run at all. |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function bowOutEarly() |
|
65
|
|
|
{ |
|
66
|
|
|
// Version used here should be the highest version from the `$newModifiers` array, |
|
67
|
|
|
// i.e. the last PHP version in which a new modifier was introduced. |
|
68
|
|
|
return ($this->supportsBelow('7.2') === false); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Examine the regex modifier string. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
76
|
|
|
* @param int $stackPtr The position of the current token in the |
|
77
|
|
|
* stack passed in $tokens. |
|
78
|
|
|
* @param string $functionName The function which contained the pattern. |
|
79
|
|
|
* @param string $modifiers The regex modifiers found. |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function examineModifiers(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $modifiers) |
|
84
|
|
|
{ |
|
85
|
|
|
$error = 'The PCRE regex modifier "%s" is not present in PHP version %s or earlier'; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($this->newModifiers as $modifier => $versionArray) { |
|
88
|
|
|
if (strpos($modifiers, $modifier) === false) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$notInVersion = ''; |
|
93
|
|
|
foreach ($versionArray as $version => $present) { |
|
94
|
|
|
if ($notInVersion === '' && $present === false |
|
95
|
|
|
&& $this->supportsBelow($version) === true |
|
96
|
|
|
) { |
|
97
|
|
|
$notInVersion = $version; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($notInVersion === '') { |
|
102
|
|
|
continue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$errorCode = $modifier . 'ModifierFound'; |
|
106
|
|
|
$data = array( |
|
107
|
|
|
$modifier, |
|
108
|
|
|
$notInVersion, |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$phpcsFile->addError($error, $stackPtr, $errorCode, $data); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
}//end class |
|
116
|
|
|
|