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