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 FileRepository 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 FileRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class FileRepository implements RepositoryInterface |
||
31 | { |
||
32 | /** |
||
33 | * File where all rates are persisted. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $pathToFile; |
||
38 | |||
39 | /** |
||
40 | * Collection of loaded rates. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $rates; |
||
45 | |||
46 | /** |
||
47 | * Collection of latest rates (to speed up search process). |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $latest; |
||
52 | |||
53 | /** |
||
54 | * FileRepository constructor. |
||
55 | * |
||
56 | * @param string $pathToFile |
||
57 | */ |
||
58 | 8 | public function __construct($pathToFile) |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 7 | public function save(array $rates) |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 1 | View Code Duplication | public function delete(array $rates) |
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 3 | View Code Duplication | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 3 | View Code Duplication | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 2 | View Code Duplication | public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN, \DateTimeInterface $date = null) |
165 | 3 | ||
166 | /** |
||
167 | 3 | * {@inheritdoc} |
|
168 | 1 | */ |
|
169 | View Code Duplication | public function all(array $criteria = array()) |
|
189 | 1 | ||
190 | /** |
||
191 | 1 | * {@inheritdoc} |
|
192 | */ |
||
193 | public function count() |
||
197 | |||
198 | /** |
||
199 | 8 | * Load all rates from file. |
|
200 | * |
||
201 | 8 | * @return RateInterface[] |
|
202 | 8 | */ |
|
203 | protected function load() |
||
231 | |||
232 | /** |
||
233 | * Builds rate key to speed up search. |
||
234 | * |
||
235 | * @param string $currencyCode |
||
236 | * @param \DateTime $date |
||
237 | 7 | * @param string $rateType |
|
238 | * @param string $sourceName |
||
239 | 7 | * @return string |
|
240 | 7 | */ |
|
241 | 7 | View Code Duplication | protected function getRateKey($currencyCode, $date, $rateType, $sourceName) |
249 | |||
250 | /** |
||
251 | * Initializes file storage. |
||
252 | * |
||
253 | * @codeCoverageIgnore |
||
254 | */ |
||
255 | protected function initialize() |
||
274 | |||
275 | /** |
||
276 | * Serialize rate to JSON string. |
||
277 | 7 | * |
|
278 | * @param RateInterface $rate Rate to serialize. |
||
279 | 7 | * @return string JSON representation of rate. |
|
280 | 7 | */ |
|
281 | 7 | protected function toJson(RateInterface $rate) |
|
294 | |||
295 | /** |
||
296 | * Deserialize JSON string to Rate |
||
297 | 7 | * |
|
298 | * @param string $json Serialized rate. |
||
299 | 7 | * @return Rate Deserialized rate. |
|
300 | */ |
||
301 | 7 | protected function fromJson($json) |
|
316 | |||
317 | /** |
||
318 | * Extract requested page from filter criteria. |
||
319 | * |
||
320 | 2 | * @param array $rates Rates to filter for pagination. |
|
321 | * @param array $criteria Filter criteria. |
||
322 | 2 | * @return RateInterface[] Paginated rates. |
|
323 | 1 | */ |
|
324 | View Code Duplication | protected function paginate(array $rates, $criteria) |
|
340 | } |
||
341 |
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.