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 | View Code Duplication | 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 | View Code Duplication | public function setWith($value, $checkType = true) |
|
125 | { |
||
126 | parent::setWith($value, $checkType); |
||
127 | |||
128 | if ($checkType === true) { |
||
129 | if ($this->value instanceof CollectionInterface) { |
||
130 | $this->pass(); |
||
131 | } else { |
||
132 | $this->fail($this->getLocale()->_('%s is not a collection', $this)); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param SpecificationInterface $criteria |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function thatMatchToCriteria(SpecificationInterface $criteria) |
||
145 | { |
||
146 | $collection = $this->valueAsCollection(); |
||
147 | foreach ($collection as $item) { |
||
148 | if (!$this->checkMatchResult($criteria->evaluate($item))) { |
||
149 | return $this; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $this->pass(); |
||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param bool $result |
||
159 | * @return bool |
||
160 | */ |
||
161 | private function checkMatchResult($result) |
||
162 | { |
||
163 | View Code Duplication | if($result === true && $this->assertAll === false) { |
|
164 | $this->fail($this->_('At least one items that match with the given criteria')); |
||
165 | return false; |
||
166 | } |
||
167 | View Code Duplication | if($result === false && $this->assertAll === true) { |
|
168 | $this->fail($this->_('At least one items that not match with the given criteria')); |
||
169 | return false; |
||
170 | } |
||
171 | |||
172 | return true; |
||
173 | } |
||
174 | |||
175 | |||
176 | /** |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function isSorted() |
||
183 | |||
184 | /** |
||
185 | * @param ComparatorInterface $comparator |
||
186 | * |
||
187 | * @return $this |
||
188 | */ |
||
189 | View Code Duplication | public function isSortedUsing(ComparatorInterface $comparator) |
|
202 | |||
203 | /** |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function isNotSorted() |
||
210 | |||
211 | /** |
||
212 | * @param ComparatorInterface $comparator |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | View Code Duplication | public function isNotSortedUsing(ComparatorInterface $comparator) |
|
227 | |||
228 | /** |
||
229 | * @param ComparatorInterface $comparator |
||
230 | * @return array |
||
231 | */ |
||
232 | private function checkIsSorted(ComparatorInterface $comparator) |
||
244 | |||
245 | /** |
||
246 | * @param mixed $value |
||
247 | * |
||
248 | * @return mixed |
||
249 | */ |
||
250 | View Code Duplication | public function contains($value) |
|
261 | |||
262 | /** |
||
263 | * @param array|\Traversable $values |
||
264 | * |
||
265 | * @return mixed |
||
266 | */ |
||
267 | public function containsValues($values) |
||
275 | |||
276 | /** |
||
277 | * @param mixed $value |
||
278 | * |
||
279 | * @return mixed |
||
280 | */ |
||
281 | View Code Duplication | public function notContains($value) |
|
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | protected function valueIsSet($message = 'Collection is undefined') |
||
300 | |||
301 | /** |
||
302 | * @throws LogicException |
||
303 | * |
||
304 | * @return \Cubiche\Core\Collections\CollectionInterface |
||
305 | */ |
||
306 | protected function valueAsCollection() |
||
315 | } |
||
316 |
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.