Conditions | 27 |
Paths | 165 |
Total Lines | 132 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
102 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
103 | { |
||
104 | if ($this->supportsAbove('7.3') === false) { |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | $tokens = $phpcsFile->getTokens(); |
||
109 | |||
110 | if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) { |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | $switchOpener = $tokens[$stackPtr]['scope_opener']; |
||
115 | $switchCloser = $tokens[$stackPtr]['scope_closer']; |
||
116 | |||
117 | // Quick check whether we need to bother with the more complex logic. |
||
118 | $hasContinue = $phpcsFile->findNext(\T_CONTINUE, ($switchOpener + 1), $switchCloser); |
||
119 | if ($hasContinue === false) { |
||
120 | return; |
||
121 | } |
||
122 | |||
123 | $caseDefault = $switchOpener; |
||
124 | |||
125 | do { |
||
126 | $caseDefault = $phpcsFile->findNext($this->caseTokens, ($caseDefault + 1), $switchCloser); |
||
127 | if ($caseDefault === false) { |
||
128 | break; |
||
129 | } |
||
130 | |||
131 | if (isset($tokens[$caseDefault]['scope_opener']) === false) { |
||
132 | // Unknown start of the case, skip. |
||
133 | continue; |
||
134 | } |
||
135 | |||
136 | $caseOpener = $tokens[$caseDefault]['scope_opener']; |
||
137 | $nextCaseDefault = $phpcsFile->findNext($this->caseTokens, ($caseDefault + 1), $switchCloser); |
||
138 | if ($nextCaseDefault === false) { |
||
139 | $caseCloser = $switchCloser; |
||
140 | } else { |
||
141 | $caseCloser = $nextCaseDefault; |
||
142 | } |
||
143 | |||
144 | // Check for unscoped control structures within the case. |
||
145 | $controlStructure = $caseOpener; |
||
146 | $doCount = 0; |
||
147 | while (($controlStructure = $phpcsFile->findNext($this->loopStructures, ($controlStructure + 1), $caseCloser)) !== false) { |
||
148 | if ($tokens[$controlStructure]['code'] === \T_DO) { |
||
149 | $doCount++; |
||
150 | } |
||
151 | |||
152 | if (isset($tokens[$controlStructure]['scope_opener'], $tokens[$controlStructure]['scope_closer']) === false) { |
||
153 | if ($tokens[$controlStructure]['code'] === \T_WHILE && $doCount > 0) { |
||
154 | // While in a do-while construct. |
||
155 | $doCount--; |
||
156 | continue; |
||
157 | } |
||
158 | |||
159 | // Control structure without braces found within the case, ignore this case. |
||
160 | continue 2; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // Examine the contents of the case. |
||
165 | $continue = $caseOpener; |
||
166 | |||
167 | do { |
||
168 | $continue = $phpcsFile->findNext(\T_CONTINUE, ($continue + 1), $caseCloser); |
||
169 | if ($continue === false) { |
||
170 | break; |
||
171 | } |
||
172 | |||
173 | $nextSemicolon = $phpcsFile->findNext(array(\T_SEMICOLON, \T_CLOSE_TAG), ($continue + 1), $caseCloser); |
||
174 | $codeString = ''; |
||
175 | for ($i = ($continue + 1); $i < $nextSemicolon; $i++) { |
||
176 | if (isset($this->acceptedLevelTokens[$tokens[$i]['code']]) === false) { |
||
177 | // Function call/variable or other token which make numeric level impossible to determine. |
||
178 | continue 2; |
||
179 | } |
||
180 | |||
181 | if (isset($this->emptyTokens[$tokens[$i]['code']]) === true) { |
||
182 | continue; |
||
183 | } |
||
184 | |||
185 | $codeString .= $tokens[$i]['content']; |
||
186 | } |
||
187 | |||
188 | $level = null; |
||
189 | if ($codeString !== '') { |
||
190 | if (is_numeric($codeString)) { |
||
191 | $level = (int) $codeString; |
||
192 | } else { |
||
193 | // With the above logic, the string can only contain digits and operators, eval! |
||
194 | $level = eval("return ( $codeString );"); |
||
|
|||
195 | } |
||
196 | } |
||
197 | |||
198 | if (isset($level) === false || $level === 0) { |
||
199 | $level = 1; |
||
200 | } |
||
201 | |||
202 | // Examine which control structure is being targeted by the continue statement. |
||
203 | if (isset($tokens[$continue]['conditions']) === false) { |
||
204 | continue; |
||
205 | } |
||
206 | |||
207 | $conditions = array_reverse($tokens[$continue]['conditions'], true); |
||
208 | // PHPCS adds more structures to the conditions array than we want to take into |
||
209 | // consideration, so clean up the array. |
||
210 | foreach ($conditions as $tokenPtr => $tokenCode) { |
||
211 | if (isset($this->loopStructures[$tokenCode]) === false) { |
||
212 | unset($conditions[$tokenPtr]); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | $targetCondition = \array_slice($conditions, ($level - 1), 1, true); |
||
217 | if (empty($targetCondition)) { |
||
218 | continue; |
||
219 | } |
||
220 | |||
221 | $conditionToken = key($targetCondition); |
||
222 | if ($conditionToken === $stackPtr) { |
||
223 | $phpcsFile->addWarning( |
||
224 | "Targeting a 'switch' control structure with a 'continue' statement is strongly discouraged and will throw a warning as of PHP 7.3.", |
||
225 | $continue, |
||
226 | 'Found' |
||
227 | ); |
||
228 | } |
||
229 | |||
230 | } while ($continue < $caseCloser); |
||
231 | |||
232 | } while ($caseDefault < $switchCloser); |
||
233 | } |
||
234 | |||
236 |
On one hand,
eval
might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,eval
prevents some optimization that they perform.