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 Environment 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 Environment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Environment extends ValidatableArrayObject |
||
10 | { |
||
11 | /** |
||
12 | * {@inheritdoc} |
||
13 | * Example: |
||
14 | * // $node1, $node2, $node3, $node4 are instances of \Assimtech\Tempo\Node\NodeInterface |
||
15 | * new Environment(array( |
||
16 | * 'name' => 'test', |
||
17 | * 'nodes' => array( |
||
18 | * $node1, |
||
19 | * $node2, |
||
20 | * $node3, |
||
21 | * $node4, |
||
22 | * ), |
||
23 | * 'roles' => array( |
||
24 | * 'role1' => array( |
||
25 | * $node1, |
||
26 | * $node2, |
||
27 | * ), |
||
28 | * 'role2' => array( |
||
29 | * $node3, |
||
30 | * ), |
||
31 | * ), |
||
32 | * )) |
||
33 | */ |
||
34 | 25 | public function __construct($input = array(), $flags = 0, $iteratorClass = 'ArrayIterator') |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 25 | protected function validate($index = null) |
|
71 | |||
72 | /** |
||
73 | * @throws \InvalidArgumentException |
||
74 | */ |
||
75 | 25 | protected function validateName() |
|
81 | |||
82 | /** |
||
83 | * @throws \InvalidArgumentException |
||
84 | */ |
||
85 | 23 | protected function validateNodes() |
|
111 | |||
112 | /** |
||
113 | * @throws \InvalidArgumentException |
||
114 | */ |
||
115 | 23 | protected function validateRoles() |
|
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | 8 | public function __toString() |
|
142 | |||
143 | /** |
||
144 | * @param \Assimtech\Tempo\Node\NodeInterface $node |
||
145 | * @param string|array $roles Optional for grouping of like nodes e.g. fep, web, db |
||
146 | * @return self |
||
147 | * @throws \InvalidArgumentException |
||
148 | */ |
||
149 | 6 | public function addNode(Node\NodeInterface $node, $roles = array()) |
|
196 | |||
197 | /** |
||
198 | * @param \Assimtech\Tempo\Node\NodeInterface[] $nodes |
||
199 | * @param string|array $roles Optional for grouping of like nodes e.g. fep, web, db |
||
200 | * @return self |
||
201 | */ |
||
202 | 2 | public function addNodes($nodes, $roles = array()) |
|
210 | |||
211 | /** |
||
212 | * @param string $name Name is optional if exactly one node is in the environment |
||
213 | * @return \Assimtech\Tempo\Node\NodeInterface |
||
214 | * @throws \InvalidArgumentException |
||
215 | * @throws \OutOfBoundsException |
||
216 | */ |
||
217 | 5 | public function getNode($name = null) |
|
247 | |||
248 | 5 | public function getNodes($role = null) |
|
271 | } |
||
272 |
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.