1 | <?php |
||
22 | class PHPCompatibility_Sniffs_PHP_PregReplaceEModifierSniff extends PHPCompatibility_Sniff |
||
|
|||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Functions to check for. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $functions = array( |
||
31 | 'preg_replace' => true, |
||
32 | 'preg_filter' => true, |
||
33 | ); |
||
34 | |||
35 | /** |
||
36 | * Regex bracket delimiters. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $doublesSeparators = array( |
||
41 | '{' => '}', |
||
42 | '[' => ']', |
||
43 | '(' => ')', |
||
44 | '<' => '>', |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * Returns an array of tokens this test wants to listen for. |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | public function register() |
||
56 | |||
57 | /** |
||
58 | * Processes this test, when one of its tokens is encountered. |
||
59 | * |
||
60 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
61 | * @param int $stackPtr The position of the current token in the |
||
62 | * stack passed in $tokens. |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
67 | { |
||
68 | if ($this->supportsAbove('5.5') === false) { |
||
69 | return; |
||
70 | } |
||
71 | |||
72 | $tokens = $phpcsFile->getTokens(); |
||
73 | $functionName = $tokens[$stackPtr]['content']; |
||
74 | $functionNameLc = strtolower($functionName); |
||
75 | |||
76 | // Bow out if not one of the functions we're targetting. |
||
77 | if ( isset($this->functions[$functionNameLc]) === false ) { |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | // Get the first parameter in the function call as that should contain the regex(es). |
||
82 | $firstParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, 1); |
||
83 | if ($firstParam === false) { |
||
84 | return; |
||
85 | } |
||
86 | |||
87 | // Differentiate between an array of patterns passed and a single pattern. |
||
88 | $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $firstParam['start'], ($firstParam['end'] +1), true); |
||
89 | if ($nextNonEmpty !== false && ($tokens[$nextNonEmpty]['code'] === T_ARRAY || $tokens[$nextNonEmpty]['code'] === T_OPEN_SHORT_ARRAY)) { |
||
90 | $arrayValues = $this->getFunctionCallParameters($phpcsFile, $nextNonEmpty); |
||
91 | foreach ($arrayValues as $value) { |
||
92 | $hasKey = $phpcsFile->findNext(T_DOUBLE_ARROW, $value['start'], ($value['end'] + 1)); |
||
93 | if ($hasKey !== false) { |
||
94 | $value['start'] = ($hasKey + 1); |
||
95 | $value['raw'] = trim($phpcsFile->getTokensAsString($value['start'], (($value['end'] + 1) - $value['start']))); |
||
96 | } |
||
97 | |||
98 | $this->processRegexPattern($value, $phpcsFile, $value['end'], $functionName); |
||
99 | } |
||
100 | } |
||
101 | else { |
||
102 | $this->processRegexPattern($firstParam, $phpcsFile, $stackPtr, $functionName); |
||
103 | } |
||
104 | |||
105 | }//end process() |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Analyse a potential regex pattern for usage of the /e modifier. |
||
110 | * |
||
111 | * @param array $pattern Array containing the start and end token |
||
112 | * pointer of the potential regex pattern and |
||
113 | * the raw string value of the pattern. |
||
114 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
115 | * @param int $stackPtr The position of the current token in the |
||
116 | * stack passed in $tokens. |
||
117 | * @param string $functionName The function which contained the pattern. |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | protected function processRegexPattern($pattern, $phpcsFile, $stackPtr, $functionName) |
||
177 | |||
178 | }//end class |
||
179 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.