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 CollectionAsserter 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 CollectionAsserter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class CollectionAsserter extends ObjectAsserter |
||
27 | { |
||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | protected $assertAll; |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | public function __get($asserter) |
||
57 | |||
58 | /** |
||
59 | * @return \mageekguy\atoum\stubs\asserters\integer |
||
60 | */ |
||
61 | public function size() |
||
68 | |||
69 | /** |
||
70 | * @param string $failMessage |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | View Code Duplication | public function isEmpty($failMessage = null) |
|
84 | |||
85 | /** |
||
86 | * @param string $failMessage |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function isNotEmpty($failMessage = null) |
||
100 | |||
101 | /** |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function hasAllElements() |
||
110 | |||
111 | /** |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function hasNoElements() |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function setWith($value, $checkType = true) |
||
138 | |||
139 | /** |
||
140 | * @param SpecificationInterface $criteria |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function thatMatchToCriteria(SpecificationInterface $criteria) |
||
157 | |||
158 | /** |
||
159 | * @param bool $result |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | private function checkMatchResult($result) |
||
178 | |||
179 | /** |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function isSorted() |
||
186 | |||
187 | /** |
||
188 | * @param ComparatorInterface $comparator |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | View Code Duplication | public function isSortedUsing(ComparatorInterface $comparator) |
|
205 | |||
206 | /** |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function isNotSorted() |
||
213 | |||
214 | /** |
||
215 | * @param ComparatorInterface $comparator |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | View Code Duplication | public function isNotSortedUsing(ComparatorInterface $comparator) |
|
230 | |||
231 | /** |
||
232 | * @param ComparatorInterface $comparator |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | private function checkIsSorted(ComparatorInterface $comparator) |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | protected function valueIsSet($message = 'Collection is undefined') |
||
256 | |||
257 | /** |
||
258 | * @throws LogicException |
||
259 | * |
||
260 | * @return \Cubiche\Core\Collections\CollectionInterface |
||
261 | */ |
||
262 | protected function valueAsCollection() |
||
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.