Conditions | 46 |
Paths | 1698 |
Total Lines | 168 |
Lines | 20 |
Ratio | 11.9 % |
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 |
||
121 | public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters) |
||
122 | { |
||
123 | if (isset($parameters[2]) === false) { |
||
124 | // Only one parameter, this must be $pieces. Bow out. |
||
125 | return; |
||
126 | } |
||
127 | |||
128 | $tokens = $phpcsFile->getTokens(); |
||
129 | |||
130 | /* |
||
131 | * Examine the first parameter. |
||
132 | * If there is any indication that this is an array declaration, we have an error. |
||
133 | */ |
||
134 | |||
135 | $targetParam = $parameters[1]; |
||
136 | $start = $targetParam['start']; |
||
137 | $end = ($targetParam['end'] + 1); |
||
138 | $isOnlyText = true; |
||
139 | |||
140 | $firstNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $start, $end, true); |
||
141 | if ($firstNonEmpty === false) { |
||
142 | // Parse error. Shouldn't be possible. |
||
143 | return; |
||
144 | } |
||
145 | |||
146 | View Code Duplication | if ($tokens[$firstNonEmpty]['code'] === \T_OPEN_PARENTHESIS) { |
|
|
|||
147 | $start = ($firstNonEmpty + 1); |
||
148 | $end = $tokens[$firstNonEmpty]['parenthesis_closer']; |
||
149 | } |
||
150 | |||
151 | $hasTernary = $phpcsFile->findNext(\T_INLINE_THEN, $start, $end); |
||
152 | View Code Duplication | if ($hasTernary !== false |
|
153 | && isset($tokens[$start]['nested_parenthesis'], $tokens[$hasTernary]['nested_parenthesis']) |
||
154 | && count($tokens[$start]['nested_parenthesis']) === count($tokens[$hasTernary]['nested_parenthesis']) |
||
155 | ) { |
||
156 | $start = ($hasTernary + 1); |
||
157 | } |
||
158 | |||
159 | for ($i = $start; $i < $end; $i++) { |
||
160 | $tokenCode = $tokens[$i]['code']; |
||
161 | |||
162 | if (isset(Tokens::$emptyTokens[$tokenCode])) { |
||
163 | continue; |
||
164 | } |
||
165 | |||
166 | if ($tokenCode === \T_STRING && isset($this->constantStrings[$tokens[$i]['content']])) { |
||
167 | continue; |
||
168 | } |
||
169 | |||
170 | if ($hasTernary !== false && $tokenCode === \T_INLINE_ELSE) { |
||
171 | continue; |
||
172 | } |
||
173 | |||
174 | if (isset(Tokens::$stringTokens[$tokenCode]) === false) { |
||
175 | $isOnlyText = false; |
||
176 | } |
||
177 | |||
178 | if ($tokenCode === \T_ARRAY || $tokenCode === \T_OPEN_SHORT_ARRAY || $tokenCode === \T_ARRAY_CAST) { |
||
179 | $this->throwNotice($phpcsFile, $stackPtr, $functionName); |
||
180 | return; |
||
181 | } |
||
182 | |||
183 | if ($tokenCode === \T_STRING) { |
||
184 | /* |
||
185 | * Check for specific functions which return an array (i.e. $pieces). |
||
186 | */ |
||
187 | $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), $end, true); |
||
188 | if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { |
||
189 | continue; |
||
190 | } |
||
191 | |||
192 | $nameLc = strtolower($tokens[$i]['content']); |
||
193 | if (isset($this->arrayFunctions[$nameLc]) === false |
||
194 | && (strpos($nameLc, 'array_') !== 0 |
||
195 | || isset($this->arrayFunctionExceptions[$nameLc]) === true) |
||
196 | ) { |
||
197 | continue; |
||
198 | } |
||
199 | |||
200 | // Now make sure it's the PHP native function being called. |
||
201 | $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), $start, true); |
||
202 | if ($tokens[$prevNonEmpty]['code'] === \T_DOUBLE_COLON |
||
203 | || $tokens[$prevNonEmpty]['code'] === \T_OBJECT_OPERATOR |
||
204 | ) { |
||
205 | // Method call, not a call to the PHP native function. |
||
206 | continue; |
||
207 | } |
||
208 | |||
209 | if ($tokens[$prevNonEmpty]['code'] === \T_NS_SEPARATOR |
||
210 | && $tokens[$prevNonEmpty - 1]['code'] === \T_STRING |
||
211 | ) { |
||
212 | // Namespaced function. |
||
213 | continue; |
||
214 | } |
||
215 | |||
216 | // Ok, so we know that there is an array function in the first param. |
||
217 | // 99.9% chance that this is $pieces, not $glue. |
||
218 | $this->throwNotice($phpcsFile, $stackPtr, $functionName); |
||
219 | return; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | if ($isOnlyText === true) { |
||
224 | // First parameter only contained text string tokens, i.e. glue. |
||
225 | return; |
||
226 | } |
||
227 | |||
228 | /* |
||
229 | * Examine the second parameter. |
||
230 | */ |
||
231 | |||
232 | $targetParam = $parameters[2]; |
||
233 | $start = $targetParam['start']; |
||
234 | $end = ($targetParam['end'] + 1); |
||
235 | |||
236 | $firstNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $start, $end, true); |
||
237 | if ($firstNonEmpty === false) { |
||
238 | // Parse error. Shouldn't be possible. |
||
239 | return; |
||
240 | } |
||
241 | |||
242 | View Code Duplication | if ($tokens[$firstNonEmpty]['code'] === \T_OPEN_PARENTHESIS) { |
|
243 | $start = ($firstNonEmpty + 1); |
||
244 | $end = $tokens[$firstNonEmpty]['parenthesis_closer']; |
||
245 | } |
||
246 | |||
247 | $hasTernary = $phpcsFile->findNext(\T_INLINE_THEN, $start, $end); |
||
248 | View Code Duplication | if ($hasTernary !== false |
|
249 | && isset($tokens[$start]['nested_parenthesis'], $tokens[$hasTernary]['nested_parenthesis']) |
||
250 | && count($tokens[$start]['nested_parenthesis']) === count($tokens[$hasTernary]['nested_parenthesis']) |
||
251 | ) { |
||
252 | $start = ($hasTernary + 1); |
||
253 | } |
||
254 | |||
255 | for ($i = $start; $i < $end; $i++) { |
||
256 | $tokenCode = $tokens[$i]['code']; |
||
257 | |||
258 | if (isset(Tokens::$emptyTokens[$tokenCode])) { |
||
259 | continue; |
||
260 | } |
||
261 | |||
262 | if ($tokenCode === \T_ARRAY || $tokenCode === \T_OPEN_SHORT_ARRAY || $tokenCode === \T_ARRAY_CAST) { |
||
263 | // Found an array, $pieces is second. |
||
264 | return; |
||
265 | } |
||
266 | |||
267 | if ($tokenCode === \T_STRING && isset($this->constantStrings[$tokens[$i]['content']])) { |
||
268 | // One of the special cased, PHP native string constants found. |
||
269 | $this->throwNotice($phpcsFile, $stackPtr, $functionName); |
||
270 | return; |
||
271 | } |
||
272 | |||
273 | if ($tokenCode === \T_STRING || $tokenCode === \T_VARIABLE) { |
||
274 | // Function call, constant or variable encountered. |
||
275 | // No matter what this is combined with, we won't be able to reliably determine the value. |
||
276 | return; |
||
277 | } |
||
278 | |||
279 | if ($tokenCode === \T_CONSTANT_ENCAPSED_STRING |
||
280 | || $tokenCode === \T_DOUBLE_QUOTED_STRING |
||
281 | || $tokenCode === \T_HEREDOC |
||
282 | || $tokenCode === \T_NOWDOC |
||
283 | ) { |
||
284 | $this->throwNotice($phpcsFile, $stackPtr, $functionName); |
||
285 | return; |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
324 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.