Complex classes like UnusedLocalVariableTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UnusedLocalVariableTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class UnusedLocalVariableTest extends AbstractTest |
||
29 | { |
||
30 | /** |
||
31 | * testRuleAppliesToUnusedLocalVariable |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function testRuleAppliesToUnusedLocalVariable() |
||
42 | |||
43 | /** |
||
44 | * testInnerFunctionParametersDoNotHideUnusedVariables |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public function testInnerFunctionParametersDoNotHideUnusedVariables() |
||
55 | |||
56 | /** |
||
57 | * testRuleAppliesToLocalVariableWithSameNameAsStaticProperty |
||
58 | * |
||
59 | * <code> |
||
60 | * class Foo |
||
61 | * protected $baz = 42; |
||
62 | * function bar() { |
||
63 | * $baz = 23; |
||
64 | * return self::$baz; |
||
65 | * } |
||
66 | * } |
||
67 | * </code> |
||
68 | * |
||
69 | * @return void |
||
70 | */ |
||
71 | public function testRuleAppliesToLocalVariableWithSameNameAsStaticProperty() |
||
78 | |||
79 | /** |
||
80 | * testRuleAppliesToLocalVariableWithSameNameAsStaticArrayProperty |
||
81 | * |
||
82 | * <code> |
||
83 | * class Foo |
||
84 | * protected $baz = array(array(1=>42)); |
||
85 | * function bar() { |
||
86 | * $baz = 23; |
||
87 | * return self::$baz[0][1]; |
||
88 | * } |
||
89 | * } |
||
90 | * </code> |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | public function testRuleAppliesToLocalVariableWithSameNameAsStaticArrayProperty() |
||
101 | |||
102 | /** |
||
103 | * testRuleDoesApplyToCompoundVariableInString |
||
104 | * |
||
105 | * <code> |
||
106 | * class Foo |
||
107 | * function bar() { |
||
108 | * $baz = 23; |
||
109 | * return "${baz}_wibble"; |
||
110 | * } |
||
111 | * } |
||
112 | * </code> |
||
113 | * |
||
114 | * @return void |
||
115 | */ |
||
116 | public function testRuleDoesApplyToCompoundVariableInString() |
||
123 | |||
124 | /** |
||
125 | * testRuleDoesNotApplyToLocalVariableUsedInCompoundVariable |
||
126 | * |
||
127 | * <code> |
||
128 | * class Foo { |
||
129 | * protected static $bar = 42; |
||
130 | * public function baz() |
||
131 | * { |
||
132 | * $name = 'bar'; |
||
133 | * return self::${$name}; |
||
134 | * } |
||
135 | * } |
||
136 | * </code> |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | public function testRuleDoesNotApplyToLocalVariableUsedInCompoundVariable() |
||
147 | |||
148 | /** |
||
149 | * testRuleDoesNotApplyToThisVariable |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | public function testRuleDoesNotApplyToThisVariable() |
||
160 | |||
161 | /** |
||
162 | * testRuleDoesNotApplyToStaticProperty |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | public function testRuleDoesNotApplyToStaticProperty() |
||
173 | |||
174 | /** |
||
175 | * testRuleDoesNotApplyToStaticArrayProperty |
||
176 | * |
||
177 | * @return void |
||
178 | */ |
||
179 | public function testRuleDoesNotApplyToStaticArrayProperty() |
||
186 | |||
187 | /** |
||
188 | * testRuleDoesNotApplyToMethodArgument |
||
189 | * |
||
190 | * <code> |
||
191 | * class Foo { |
||
192 | * function bar() { |
||
193 | * $baz = 42; |
||
194 | * $this->foo($baz); |
||
195 | * } |
||
196 | * } |
||
197 | * </code> |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | public function testRuleDoesNotApplyToMethodArgument() |
||
208 | |||
209 | /** |
||
210 | * testRuleDoesNotApplyToStaticObjectProperty |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | public function testRuleDoesNotApplyToStaticObjectProperty() |
||
221 | |||
222 | /** |
||
223 | * testRuleDoesNotApplyToDynamicProperty |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | public function testRuleDoesNotApplyToDynamicProperty() |
||
234 | |||
235 | /** |
||
236 | * testRuleDoesNotApplyToUnusedParameters |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | public function testRuleDoesNotApplyToUnusedParameters() |
||
247 | |||
248 | /** |
||
249 | * testRuleDoesNotApplyToArgcSuperGlobal |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | public function testRuleDoesNotApplyToArgcSuperGlobal() |
||
260 | |||
261 | /** |
||
262 | * testRuleDoesNotApplyToArgvSuperGlobal |
||
263 | * |
||
264 | * @return void |
||
265 | */ |
||
266 | public function testRuleDoesNotApplyToArgvSuperGlobal() |
||
273 | |||
274 | /** |
||
275 | * testRuleDoesNotApplyToGlobalsSuperGlobal |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | public function testRuleDoesNotApplyToGlobalsSuperGlobal() |
||
286 | |||
287 | /** |
||
288 | * testRuleDoesNotApplyToCookieSuperGlobal |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | public function testRuleDoesNotApplyToCookieSuperGlobal() |
||
299 | |||
300 | /** |
||
301 | * testRuleDoesNotApplyToEnvSuperGlobal |
||
302 | * |
||
303 | * @return void |
||
304 | */ |
||
305 | public function testRuleDoesNotApplyToEnvSuperGlobal() |
||
312 | |||
313 | /** |
||
314 | * testRuleDoesNotApplyToFilesSuperGlobal |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | public function testRuleDoesNotApplyToFilesSuperGlobal() |
||
325 | |||
326 | /** |
||
327 | * testRuleDoesNotApplyToGetSuperGlobal |
||
328 | * |
||
329 | * @return void |
||
330 | */ |
||
331 | public function testRuleDoesNotApplyToGetSuperGlobal() |
||
338 | |||
339 | /** |
||
340 | * testRuleDoesNotApplyToPostSuperGlobal |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | public function testRuleDoesNotApplyToPostSuperGlobal() |
||
351 | |||
352 | /** |
||
353 | * testRuleDoesNotApplyToRequestSuperGlobal |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | public function testRuleDoesNotApplyToRequestSuperGlobal() |
||
364 | |||
365 | /** |
||
366 | * testRuleDoesNotApplyToSessionSuperGlobal |
||
367 | * |
||
368 | * @return void |
||
369 | */ |
||
370 | public function testRuleDoesNotApplyToSessionSuperGlobal() |
||
377 | |||
378 | /** |
||
379 | * testRuleDoesNotApplyToServerSuperGlobal |
||
380 | * |
||
381 | * @return void |
||
382 | */ |
||
383 | public function testRuleDoesNotApplyToServerSuperGlobal() |
||
390 | |||
391 | /** |
||
392 | * testRuleDoesNotApplyToHttpRawPostDataSuperGlobal |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | public function testRuleDoesNotApplyToHttpRawPostDataSuperGlobal() |
||
403 | |||
404 | /** |
||
405 | * testRuleDoesNotApplyToUnusedLocalVariableInFunction |
||
406 | * |
||
407 | * @return void |
||
408 | */ |
||
409 | public function testRuleDoesNotApplyToUnusedLocalVariableInFunction() |
||
416 | |||
417 | /** |
||
418 | * testRuleDoesNotApplyToUnusedLocalVariableInMethod |
||
419 | * |
||
420 | * @return void |
||
421 | */ |
||
422 | public function testRuleDoesNotApplyToUnusedLocalVariableInMethod() |
||
429 | |||
430 | /** |
||
431 | * testRuleDoesNotApplyToLocalVariableUsedAsArrayIndex |
||
432 | * |
||
433 | * <code> |
||
434 | * class Foo { |
||
435 | * public function bar() { |
||
436 | * foreach ($baz as $key) { |
||
437 | * self::$values[$key] = 42; |
||
438 | * } |
||
439 | * } |
||
440 | * } |
||
441 | * </code> |
||
442 | * |
||
443 | * @return void |
||
444 | */ |
||
445 | public function testRuleDoesNotApplyToLocalVariableUsedAsArrayIndex() |
||
452 | |||
453 | /** |
||
454 | * testRuleAppliesToUnusedForeachKeyWhenNotIgnored |
||
455 | * |
||
456 | * @return void |
||
457 | */ |
||
458 | public function testRuleAppliesToUnusedForeachKeyWhenNotIgnored() |
||
465 | |||
466 | /** |
||
467 | * testRuleAppliesToUnusedForeachValueWhenNotIgnored |
||
468 | * |
||
469 | * @return void |
||
470 | */ |
||
471 | public function testRuleAppliesToUnusedForeachValueWhenNotIgnored() |
||
478 | |||
479 | /** |
||
480 | * testRuleDoesNotApplyToUnusedForeachKeyWhenIgnored |
||
481 | * |
||
482 | * @return void |
||
483 | */ |
||
484 | public function testRuleDoesNotApplyToUnusedForeachKeyWhenIgnored() |
||
491 | |||
492 | /** |
||
493 | * testRuleDoesNotApplyToUnusedForeachKeyWhenWhitelisted |
||
494 | * |
||
495 | * @return void |
||
496 | */ |
||
497 | public function testRuleDoesNotApplyToUnusedForeachKeyWhenWhitelisted() |
||
505 | |||
506 | /** |
||
507 | * testRuleDoesNotAppliesToWhitelistedUnusedLocaleVariable |
||
508 | * |
||
509 | * @return void |
||
510 | */ |
||
511 | public function testRuleDoesNotAppliesToWhitelistedUnusedLocaleVariable() |
||
519 | |||
520 | /** |
||
521 | * testRuleStillAppliesWhenSomeUnusedLocaleAreWhitelisted |
||
522 | * |
||
523 | * @return void |
||
524 | */ |
||
525 | public function testRuleStillAppliesWhenSomeUnusedLocaleAreWhitelisted() |
||
533 | |||
534 | /** |
||
535 | * testRuleDoesNotApplyToUnusedForeachValueWhenIgnored |
||
536 | * |
||
537 | * @return void |
||
538 | */ |
||
539 | public function testRuleDoesNotApplyToUnusedForeachValueWhenIgnored() |
||
546 | |||
547 | /** |
||
548 | * testRuleDoesNotApplyToLocalVariableUsedAsStringIndex |
||
549 | * |
||
550 | * <code> |
||
551 | * class Foo { |
||
552 | * public function bar() { |
||
553 | * foreach ($baz as $key) { |
||
554 | * self::$string{$key} = 'a'; |
||
555 | * } |
||
556 | * } |
||
557 | * } |
||
558 | * </code> |
||
559 | * |
||
560 | * @return void |
||
561 | */ |
||
562 | public function testRuleDoesNotApplyToLocalVariableUsedAsStringIndex() |
||
569 | |||
570 | /** |
||
571 | * testRuleDoesNotApplyToCatchStatement |
||
572 | * |
||
573 | * <code> |
||
574 | * class Foo { |
||
575 | * public function bar() { |
||
576 | * try { |
||
577 | * } catch (Exception $e) { |
||
578 | * } |
||
579 | * } |
||
580 | * } |
||
581 | * </code> |
||
582 | * |
||
583 | * @return void |
||
584 | */ |
||
585 | public function testRuleDoesNotApplyToCatchStatement() |
||
592 | |||
593 | /** |
||
594 | * testRuleDoesNotApplyToCompactFunction |
||
595 | * |
||
596 | * <code> |
||
597 | * class Foo { |
||
598 | * public function bar() { |
||
599 | * $key = 'ok'; |
||
600 | * return compact('key'); |
||
601 | * } |
||
602 | * } |
||
603 | * </code> |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | public function testRuleDoesNotApplyToCompactFunction() |
||
614 | |||
615 | /** |
||
616 | * @return void |
||
617 | * @since 2.0.0 |
||
618 | */ |
||
619 | public function testCompactFunctionRuleWorksCaseInsensitive() |
||
626 | |||
627 | /** |
||
628 | * testRuleDoesNotApplyToNamespacedCompactFunction |
||
629 | * |
||
630 | * <code> |
||
631 | * namespace Baz; |
||
632 | * |
||
633 | * class Foo { |
||
634 | * public function bar() { |
||
635 | * $key = 'ok'; |
||
636 | * return compact('key'); |
||
637 | * } |
||
638 | * } |
||
639 | * </code> |
||
640 | * |
||
641 | * @return void |
||
642 | */ |
||
643 | public function testRuleDoesNotApplyToNamespacedCompactFunction() |
||
650 | |||
651 | /** |
||
652 | * @return void |
||
653 | * @since 2.0.1 |
||
654 | */ |
||
655 | public function testNamespacedCompactFunctionRuleWorksCaseInsensitive() |
||
662 | |||
663 | /** |
||
664 | * testRuleNotAppliesToPredefinedVariables.php |
||
665 | * |
||
666 | * @return void |
||
667 | */ |
||
668 | public function testRuleNotAppliesToPredefinedVariables() |
||
675 | } |
||
676 |