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 XMLReader 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 XMLReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class XMLReader implements DriverInterface { |
||
11 | |||
12 | /** |
||
13 | * @var \XMLReader |
||
14 | */ |
||
15 | private $xml; |
||
16 | |||
17 | /** |
||
18 | * Link to stored xml file. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private $filename; |
||
23 | |||
24 | /** |
||
25 | * Gets categories. |
||
26 | * |
||
27 | * @return arry Array of \YMLParser\Node\Category instances or empty array |
||
28 | */ |
||
29 | View Code Duplication | public function getCategories() { |
|
59 | |||
60 | /** |
||
61 | * Gets currencies. |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | View Code Duplication | public function getCurrencies() { |
|
96 | |||
97 | /** |
||
98 | * Gets offers. |
||
99 | * |
||
100 | * @param \Closure $filter |
||
101 | * |
||
102 | * @return \Iterator Array of \YMLParser\Node\Offer instances or empty array |
||
103 | */ |
||
104 | public function getOffers(\Closure $filter = null) { |
||
152 | |||
153 | /** |
||
154 | * Gets attributes from element. |
||
155 | * |
||
156 | * @param \XMLReader $element |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | private function getElementAttributes(\XMLReader $element) { |
||
171 | |||
172 | /** |
||
173 | * Opens file. |
||
174 | * |
||
175 | * @param type $filename |
||
176 | * |
||
177 | * @throws \Exception |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function open($filename) { |
||
187 | |||
188 | /** |
||
189 | * Gets amount of offers. |
||
190 | * |
||
191 | * @param \Closure $filter |
||
192 | * |
||
193 | * @return int |
||
194 | */ |
||
195 | public function countOffers(\Closure $filter = null) { |
||
204 | |||
205 | /** |
||
206 | * Rewinds cursor to the first element. |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | private function moveToStart() { |
||
215 | |||
216 | } |
||
217 |
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.