1 | <?php |
||
30 | class PregReplaceEModifierSniff extends AbstractFunctionCallParameterSniff |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * Functions to check for. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $targetFunctions = array( |
||
39 | 'preg_replace' => true, |
||
40 | 'preg_filter' => true, |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * Regex bracket delimiters. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $doublesSeparators = array( |
||
49 | '{' => '}', |
||
50 | '[' => ']', |
||
51 | '(' => ')', |
||
52 | '<' => '>', |
||
53 | ); |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Process the parameters of a matched function. |
||
58 | * |
||
59 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
60 | * @param int $stackPtr The position of the current token in the stack. |
||
61 | * @param string $functionName The token content (function name) which was matched. |
||
62 | * @param array $parameters Array with information about the parameters. |
||
63 | * |
||
64 | * @return int|void Integer stack pointer to skip forward or void to continue |
||
65 | * normal file processing. |
||
66 | */ |
||
67 | public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters) |
||
68 | { |
||
69 | // Check the first parameter in the function call as that should contain the regex(es). |
||
70 | if (isset($parameters[1]) === false) { |
||
71 | return; |
||
72 | } |
||
73 | |||
74 | $tokens = $phpcsFile->getTokens(); |
||
75 | $functionNameLc = strtolower($functionName); |
||
76 | $firstParam = $parameters[1]; |
||
77 | |||
78 | // Differentiate between an array of patterns passed and a single pattern. |
||
79 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, $firstParam['start'], ($firstParam['end'] + 1), true); |
||
80 | if ($nextNonEmpty !== false && ($tokens[$nextNonEmpty]['code'] === T_ARRAY || $tokens[$nextNonEmpty]['code'] === T_OPEN_SHORT_ARRAY)) { |
||
81 | $arrayValues = $this->getFunctionCallParameters($phpcsFile, $nextNonEmpty); |
||
82 | if ($functionNameLc === 'preg_replace_callback_array') { |
||
83 | // For preg_replace_callback_array(), the patterns will be in the array keys. |
||
84 | foreach ($arrayValues as $value) { |
||
85 | $hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1)); |
||
86 | if ($hasKey === false) { |
||
87 | continue; |
||
88 | } |
||
89 | |||
90 | $value['end'] = ($hasKey - 1); |
||
91 | $value['raw'] = trim($phpcsFile->getTokensAsString($value['start'], ($hasKey - $value['start']))); |
||
92 | $this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName); |
||
93 | } |
||
94 | |||
95 | } else { |
||
96 | // Otherwise, the patterns will be in the array values. |
||
97 | foreach ($arrayValues as $value) { |
||
98 | $hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1)); |
||
99 | if ($hasKey !== false) { |
||
100 | $value['start'] = ($hasKey + 1); |
||
101 | $value['raw'] = trim($phpcsFile->getTokensAsString($value['start'], (($value['end'] + 1) - $value['start']))); |
||
102 | } |
||
103 | |||
104 | $this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | } else { |
||
109 | $this->processRegexPattern($firstParam, $phpcsFile, $stackPtr, $functionName); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Do a version check to determine if this sniff needs to run at all. |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | protected function bowOutEarly() |
||
123 | |||
124 | |||
125 | /** |
||
126 | * Analyse a potential regex pattern for usage of the /e modifier. |
||
127 | * |
||
128 | * @param array $pattern Array containing the start and end token |
||
129 | * pointer of the potential regex pattern and |
||
130 | * the raw string value of the pattern. |
||
131 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
132 | * @param int $stackPtr The position of the current token in the |
||
133 | * stack passed in $tokens. |
||
134 | * @param string $functionName The function which contained the pattern. |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | protected function processRegexPattern($pattern, \PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName) |
||
187 | |||
188 | |||
189 | /** |
||
190 | * Examine the regex modifier string. |
||
191 | * |
||
192 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
193 | * @param int $stackPtr The position of the current token in the |
||
194 | * stack passed in $tokens. |
||
195 | * @param string $functionName The function which contained the pattern. |
||
196 | * @param string $modifiers The regex modifiers found. |
||
197 | * |
||
198 | * @return void |
||
199 | */ |
||
200 | protected function examineModifiers(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $modifiers) |
||
217 | |||
218 | }//end class |
||
219 |