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 | 10 | public function __construct($pathToFile) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 10 | public function save(array $rates) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 2 | public function delete(array $rates) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 4 | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::DEFAULT) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 2 | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::DEFAULT) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 2 | public function latest($sourceName, $currencyCode, $rateType = RateType::DEFAULT) |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 4 | public function all(array $criteria = array()) |
|
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | 2 | public function count() |
|
188 | |||
189 | /** |
||
190 | * Load all rates from file. |
||
191 | * |
||
192 | * @return RateInterface[] |
||
193 | */ |
||
194 | 10 | protected function load() |
|
224 | |||
225 | /** |
||
226 | * Builds rate key to speed up search. |
||
227 | * |
||
228 | * @param string|null|RateInterface $currencyCode |
||
229 | * @param \DateTime|null $date |
||
230 | * @param string|null $rateType |
||
231 | * @param string|null $sourceName |
||
232 | * @return string |
||
233 | */ |
||
234 | 10 | protected function getRateKey($currencyCode = null, $date = null, $rateType = null, $sourceName = null) |
|
249 | |||
250 | /** |
||
251 | * Initializes file storage. |
||
252 | */ |
||
253 | 10 | protected function initStorage() |
|
272 | |||
273 | /** |
||
274 | * Serialize rate to JSON string. |
||
275 | * |
||
276 | * @param RateInterface $rate Rate to serialize. |
||
277 | * @return string JSON representation of rate. |
||
278 | */ |
||
279 | 10 | protected function toJson(RateInterface $rate) |
|
292 | |||
293 | /** |
||
294 | * Deserialize JSON string to Rate |
||
295 | * |
||
296 | * @param string $json Serialized rate. |
||
297 | * @return Rate Deserialized rate. |
||
298 | */ |
||
299 | 10 | protected function fromJson($json) |
|
314 | |||
315 | /** |
||
316 | * Extract requested page from filter criteria. |
||
317 | * |
||
318 | * @param array $rates Rates to filter for pagination. |
||
319 | * @param array $criteria Filter criteria. |
||
320 | * @return RateInterface[] Paginated rates. |
||
321 | */ |
||
322 | 4 | protected function paginate(array $rates, $criteria) |
|
338 | } |
||
339 |