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 | 5 | public function __construct($pathToFile) |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 5 | public function save(array $rates) |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 1 | public function delete(array $rates) |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 2 | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 1 | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 1 | public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | 2 | public function all(array $criteria = array()) |
|
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | 1 | public function count() |
|
193 | |||
194 | /** |
||
195 | * Load all rates from file. |
||
196 | * |
||
197 | * @return RateInterface[] |
||
198 | */ |
||
199 | 5 | protected function load() |
|
227 | |||
228 | /** |
||
229 | * Builds rate key to speed up search. |
||
230 | * |
||
231 | * @param string $currencyCode |
||
232 | * @param \DateTime $date |
||
233 | * @param string $rateType |
||
234 | * @param string $sourceName |
||
235 | * @return string |
||
236 | */ |
||
237 | 5 | View Code Duplication | protected function getRateKey($currencyCode, $date, $rateType, $sourceName) |
|
|||
238 | { |
||
239 | 5 | return str_replace( |
|
240 | 5 | array('%currency_code%', '%date%', '%rate_type%', '%source_name%'), |
|
241 | 5 | array($currencyCode, $date->format('Y-m-d'), $rateType, $sourceName), |
|
242 | 5 | '%currency_code%_%date%_%rate_type%_%source_name%' |
|
243 | ); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Initializes file storage. |
||
248 | */ |
||
249 | 5 | protected function initialize() |
|
250 | { |
||
251 | /** @noinspection MkdirRaceConditionInspection */ |
||
252 | 5 | View Code Duplication | if (!file_exists(dirname($this->pathToFile)) && !mkdir(dirname($this->pathToFile), 0777, true)) { |
253 | throw new RuntimeException(sprintf('Could not create storage file on path "%s".', $this->pathToFile)); |
||
254 | } |
||
255 | |||
256 | 5 | View Code Duplication | if (!file_exists($this->pathToFile) && !(touch($this->pathToFile) && chmod($this->pathToFile, 0777))) { |
257 | throw new RuntimeException(sprintf('Could not create storage file on path "%s".', $this->pathToFile)); |
||
258 | } |
||
259 | |||
260 | 5 | if (!is_readable($this->pathToFile)) { |
|
261 | throw new RuntimeException(sprintf('File on path "%s" for storing rates must be readable.', $this->pathToFile)); |
||
262 | } |
||
263 | |||
264 | 5 | if (!is_writable($this->pathToFile)) { |
|
265 | throw new RuntimeException(sprintf('File on path "%s" for storing rates must be writeable.', $this->pathToFile)); |
||
266 | } |
||
267 | 5 | } |
|
268 | |||
269 | /** |
||
270 | * Serialize rate to JSON string. |
||
271 | * |
||
272 | * @param RateInterface $rate Rate to serialize. |
||
273 | * @return string JSON representation of rate. |
||
274 | */ |
||
275 | 5 | protected function toJson(RateInterface $rate) |
|
288 | |||
289 | /** |
||
290 | * Deserialize JSON string to Rate |
||
291 | * |
||
292 | * @param string $json Serialized rate. |
||
293 | * @return Rate Deserialized rate. |
||
294 | */ |
||
295 | 5 | protected function fromJson($json) |
|
310 | |||
311 | /** |
||
312 | * Extract requested page from filter criteria. |
||
313 | * |
||
314 | * @param array $rates Rates to filter for pagination. |
||
315 | * @param array $criteria Filter criteria. |
||
316 | * @return RateInterface[] Paginated rates. |
||
317 | */ |
||
318 | 2 | protected function paginate(array $rates, $criteria) |
|
334 | } |
||
335 |
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.