Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Scanner 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 Scanner, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class Scanner extends MixinScanner |
||
9 | { |
||
10 | /** |
||
11 | * Helper to create tokens. |
||
12 | */ |
||
13 | protected function scan($regex, $type, $captureIndex = 1) |
||
21 | |||
22 | /** |
||
23 | * Scan comment from input & return it if found. |
||
24 | * |
||
25 | * @return object|null |
||
26 | */ |
||
27 | protected function scanComment() |
||
42 | |||
43 | /** |
||
44 | * @return object |
||
45 | */ |
||
46 | protected function scanInterpolation() |
||
50 | |||
51 | /** |
||
52 | * @return object |
||
53 | */ |
||
54 | protected function scanTag() |
||
75 | |||
76 | /** |
||
77 | * @return object |
||
78 | */ |
||
79 | protected function scanFilter() |
||
83 | |||
84 | /** |
||
85 | * @return object |
||
86 | */ |
||
87 | protected function scanDoctype() |
||
91 | |||
92 | /** |
||
93 | * @return object |
||
94 | */ |
||
95 | protected function scanId() |
||
99 | |||
100 | /** |
||
101 | * @return object |
||
102 | */ |
||
103 | protected function scanClassName() |
||
123 | |||
124 | /** |
||
125 | * @return object |
||
126 | */ |
||
127 | protected function scanText() |
||
131 | |||
132 | /** |
||
133 | * @return object |
||
134 | */ |
||
135 | protected function scanAssignment() |
||
143 | |||
144 | /** |
||
145 | * @return object |
||
146 | */ |
||
147 | protected function scanConditional() |
||
165 | |||
166 | /** |
||
167 | * @return object |
||
168 | */ |
||
169 | View Code Duplication | protected function scanEach() |
|
181 | |||
182 | /** |
||
183 | * @return object |
||
184 | */ |
||
185 | protected function scanCustomKeyword() |
||
201 | |||
202 | /** |
||
203 | * @return object |
||
204 | */ |
||
205 | protected function scanCode() |
||
219 | |||
220 | /** |
||
221 | * @throws \ErrorException |
||
222 | * |
||
223 | * @return object |
||
224 | */ |
||
225 | protected function scanAttributes() |
||
254 | |||
255 | /** |
||
256 | * @return object |
||
257 | */ |
||
258 | protected function scanPipelessText() |
||
274 | |||
275 | /** |
||
276 | * @return object |
||
277 | */ |
||
278 | protected function scanColon() |
||
282 | |||
283 | /** |
||
284 | * @return object |
||
285 | */ |
||
286 | protected function scanAndAttributes() |
||
290 | } |
||
291 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.