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:
1 | <?php |
||
20 | View Code Duplication | class TaskNode extends ArrayNode |
|
|
|||
21 | { |
||
22 | protected $prepared = false; |
||
23 | protected $originalChildren = array(); |
||
24 | |||
25 | /** |
||
26 | * Constructor. |
||
27 | * |
||
28 | * @param string $name |
||
29 | * @param NodeInterface $parent |
||
30 | */ |
||
31 | 2 | public function __construct($name, NodeInterface $parent = null) |
|
37 | |||
38 | /** |
||
39 | * Set task factory |
||
40 | * |
||
41 | * @param TaskFactory|null $factory |
||
42 | */ |
||
43 | 2 | public function setTaskFactory($factory) |
|
47 | |||
48 | /** |
||
49 | * Takes child nodes from a ConfigurtionInterface instance |
||
50 | * and adds these to this node |
||
51 | * |
||
52 | * @param string $type |
||
53 | * @throws InvalidConfigurationException |
||
54 | */ |
||
55 | protected function prepareChildren($type) |
||
87 | |||
88 | /** |
||
89 | * We hook into the validateType method, this |
||
90 | * gets called form the normalize method. |
||
91 | * |
||
92 | * @param mixed $value |
||
93 | * |
||
94 | * @throws InvalidTypeException |
||
95 | */ |
||
96 | protected function validateType($value) |
||
108 | } |
||
109 |
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.