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:
1 | <?php |
||
11 | class CachedExchangeRateProvider implements ExchangeRatesProviderInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var ExchangeRatesProviderInterface |
||
16 | */ |
||
17 | private $provider; |
||
18 | |||
19 | |||
20 | /** |
||
21 | * @var Cache |
||
22 | */ |
||
23 | private $cache; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $lifetime; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * CachedExchangeRateProvider constructor. |
||
33 | * @param ExchangeRatesProviderInterface $provider |
||
34 | * @param Cache $cache |
||
35 | * @param int $lifetime |
||
36 | */ |
||
37 | public function __construct(ExchangeRatesProviderInterface $provider, Cache $cache, $lifetime = 10800) |
||
43 | |||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function getAllRatesExchanges(\DateTime $date = null) |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | View Code Duplication | public function getRatesExchanges(array $codes, \DateTime $date = null) |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | View Code Duplication | public function getRateExchange($code, \DateTime $date = null) |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | View Code Duplication | public function getRatesExchangesDynamic($code, \DateTime $firstDate, \DateTime $lastDate) |
|
112 | |||
113 | |||
114 | /** |
||
115 | * Ключ массива |
||
116 | * |
||
117 | * @param $name |
||
118 | * @param \DateTime|null $date |
||
119 | * @return string |
||
120 | */ |
||
121 | private function createCacheKey($name, \DateTime $date = null) |
||
126 | |||
127 | |||
128 | } |
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.