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 FbpParser 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 FbpParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | final class FbpParser implements FbpDefinitionsInterface |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $source; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $settings; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $schema; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $linecount; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $linecountOverall; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private $definition; |
||
54 | |||
55 | /** |
||
56 | * FbpParser constructor. |
||
57 | * |
||
58 | * @param string $source optional for initializing |
||
59 | * @param array $settings optional settings for parser |
||
60 | */ |
||
61 | 7 | public function __construct($source = '', $settings = []) |
|
80 | |||
81 | /** |
||
82 | * @param string $source |
||
83 | * @return FbpDefinition |
||
84 | * @throws ParserException |
||
85 | */ |
||
86 | 7 | public function run($source = '') |
|
121 | |||
122 | /** |
||
123 | * @param string $line |
||
124 | * @return array |
||
125 | * @throws ParserDefinitionException |
||
126 | */ |
||
127 | 6 | private function examineSubset($line) |
|
212 | |||
213 | /** |
||
214 | * Check if array has a specific key and is not empty. |
||
215 | * |
||
216 | * @param array $check |
||
217 | * @param string $value |
||
218 | * @return bool |
||
219 | */ |
||
220 | 6 | private function hasValue(array $check, $value) |
|
228 | |||
229 | /** |
||
230 | * @param array $definition |
||
231 | * @param string $label |
||
232 | * @return array |
||
233 | */ |
||
234 | 6 | private function addPort(array $definition, $label) |
|
241 | |||
242 | /** |
||
243 | * @param string $line |
||
244 | * @return array |
||
245 | * @throws ParserDefinitionException |
||
246 | */ |
||
247 | 6 | private function examineDefinition($line) |
|
270 | |||
271 | /** |
||
272 | * Add entry to processes. |
||
273 | * |
||
274 | * @param array $process |
||
275 | */ |
||
276 | 6 | private function examineProcess(array $process) |
|
292 | |||
293 | /** |
||
294 | * Add name to definition |
||
295 | * |
||
296 | * @param string $line |
||
297 | */ |
||
298 | 1 | private function addName($line) |
|
302 | |||
303 | /** |
||
304 | * Check if line is empty or has comment. |
||
305 | * In case of comments, add name to definition. |
||
306 | * |
||
307 | * @param string $line |
||
308 | * @return bool |
||
309 | */ |
||
310 | 6 | private function doSkip($line) |
|
329 | |||
330 | /** |
||
331 | * @param array $subset |
||
332 | * @param string $line |
||
333 | */ |
||
334 | 6 | private function validate(array $subset, $line) |
|
346 | |||
347 | /** |
||
348 | * @param string $line |
||
349 | * @param string $port |
||
350 | * @throws ParserException |
||
351 | */ |
||
352 | 1 | private function validationError($line, $port) |
|
358 | } |
||
359 |