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 Tokenizer 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 Tokenizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class Tokenizer |
||
20 | { |
||
21 | /** |
||
22 | * parse and tokenize input string |
||
23 | * @param string $plainData |
||
24 | * @throws SyntaxErrorException |
||
25 | * @throws BadMethodCallException |
||
26 | * @return array |
||
27 | * @throws \Hoa\Ustring\Exception |
||
28 | 9 | * @throws \InvalidArgumentException |
|
29 | * @throws \LogicException |
||
30 | 9 | */ |
|
31 | public static function tokenize(string $plainData) : array |
||
41 | 9 | ||
42 | 9 | /** |
|
43 | 9 | * SphinxConfigurationParser constructor. |
|
44 | * @internal |
||
45 | * @param string $string |
||
46 | * @throws BadMethodCallException |
||
47 | * @throws \Hoa\Ustring\Exception |
||
48 | */ |
||
49 | private function __construct(string $string) |
||
54 | |||
55 | /** |
||
56 | * @internal |
||
57 | * @param string $string |
||
58 | * @return string |
||
59 | */ |
||
60 | 9 | private function removeComments(string $string) : string |
|
64 | 2 | ||
65 | /** |
||
66 | 2 | * @internal |
|
67 | * @return Tokenizer |
||
68 | 2 | * @throws \LogicException |
|
69 | * @throws \InvalidArgumentException |
||
70 | * @throws SyntaxErrorException |
||
71 | */ |
||
72 | private function tokenizeInternal() : Tokenizer |
||
82 | 8 | ||
83 | /** |
||
84 | 8 | * @internal |
|
85 | 7 | * @throws SyntaxErrorException |
|
86 | * @throws \InvalidArgumentException |
||
87 | * @throws \LogicException |
||
88 | */ |
||
89 | 2 | private function extractSection() |
|
117 | |||
118 | /** |
||
119 | * @internal |
||
120 | * @throws SyntaxErrorException |
||
121 | * @throws \InvalidArgumentException |
||
122 | * @throws \LogicException |
||
123 | */ |
||
124 | 8 | private function extractSectionType() |
|
138 | 1 | ||
139 | /** |
||
140 | 1 | * @internal |
|
141 | * @throws SyntaxErrorException |
||
142 | * @throws \InvalidArgumentException |
||
143 | * @throws \LogicException |
||
144 | */ |
||
145 | View Code Duplication | private function extractSectionName() |
|
164 | 1 | ||
165 | /** |
||
166 | 3 | * @internal |
|
167 | * @throws SyntaxErrorException |
||
168 | * @throws \InvalidArgumentException |
||
169 | * @throws \LogicException |
||
170 | */ |
||
171 | private function extractInheritance() |
||
189 | |||
190 | /** |
||
191 | * @internal |
||
192 | * @throws SyntaxErrorException |
||
193 | 7 | * @throws \InvalidArgumentException |
|
194 | * @throws \LogicException |
||
195 | 7 | */ |
|
196 | View Code Duplication | private function extractInheritanceName() |
|
215 | 1 | ||
216 | /** |
||
217 | * @internal |
||
218 | * @throws SyntaxErrorException |
||
219 | * @throws \LogicException |
||
220 | * @throws \InvalidArgumentException |
||
221 | */ |
||
222 | private function extractOptions() |
||
245 | 6 | ||
246 | 6 | /** |
|
247 | * @internal |
||
248 | 2 | * @throws SyntaxErrorException |
|
249 | * @throws \InvalidArgumentException |
||
250 | * @throws \LogicException |
||
251 | */ |
||
252 | private function extractOption() |
||
267 | 5 | ||
268 | /** |
||
269 | * @internal |
||
270 | 5 | * @throws SyntaxErrorException |
|
271 | 5 | * @throws \InvalidArgumentException |
|
272 | * @throws \LogicException |
||
273 | 5 | */ |
|
274 | private function extractOptionName() |
||
289 | 1 | ||
290 | /** |
||
291 | * @internal |
||
292 | 5 | * @throws SyntaxErrorException |
|
293 | 5 | * @throws \LogicException |
|
294 | * @throws \InvalidArgumentException |
||
295 | 5 | */ |
|
296 | 5 | private function extractOptionValue() |
|
330 | |||
331 | /** |
||
332 | * @internal |
||
333 | */ |
||
334 | private function saveCurrentSection() |
||
340 | |||
341 | /** |
||
342 | 5 | * @internal |
|
343 | */ |
||
344 | private function saveCurrentOption() |
||
349 | |||
350 | /** |
||
351 | * @internal |
||
352 | * @return array |
||
353 | */ |
||
354 | private function getEmptySectionData() : array |
||
363 | |||
364 | /** |
||
365 | * @internal |
||
366 | * @return array |
||
367 | */ |
||
368 | private function getEmptyOptionData() : array |
||
375 | |||
376 | /** |
||
377 | * @var StringStream |
||
378 | */ |
||
379 | private $stream; |
||
380 | |||
381 | /** |
||
382 | * Result of tokenize input string |
||
383 | * @var array |
||
384 | */ |
||
385 | private $tokens = []; |
||
386 | |||
387 | /** |
||
388 | * temporary storage of tokens for one section |
||
389 | * @var array |
||
390 | */ |
||
391 | private $currentSection = [ |
||
392 | 'type' => '', |
||
393 | 'name' => '', |
||
394 | 'inheritance' => '', |
||
395 | 'options' => [] |
||
396 | ]; |
||
397 | /** |
||
398 | * temporary storage of tokens for one option |
||
399 | * @var array |
||
400 | */ |
||
401 | private $currentOption = [ |
||
402 | 'name' => '', |
||
403 | 'value' => '' |
||
404 | ]; |
||
405 | } |
||
406 |
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.