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 |
||
31 | class Manager implements ManagerInterface |
||
32 | { |
||
33 | use LoggerAwareTrait; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $baseCurrency; |
||
39 | |||
40 | /** |
||
41 | * @var RepositoryInterface |
||
42 | */ |
||
43 | protected $repository; |
||
44 | |||
45 | /** |
||
46 | * @var SourcesRegistryInterface |
||
47 | */ |
||
48 | protected $sources; |
||
49 | |||
50 | /** |
||
51 | * @var ProcessorsRegistryInterface |
||
52 | */ |
||
53 | protected $processors; |
||
54 | |||
55 | /** |
||
56 | * @var RatesConfigurationRegistryInterface |
||
57 | */ |
||
58 | protected $configurations; |
||
59 | |||
60 | /** |
||
61 | * Manager constructor. |
||
62 | * |
||
63 | * @param string $baseCurrency |
||
64 | * @param RepositoryInterface $repository |
||
65 | * @param SourcesRegistryInterface $sources |
||
66 | * @param ProcessorsRegistryInterface $processors |
||
67 | * @param RatesConfigurationRegistryInterface $configurations |
||
68 | * |
||
69 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
70 | */ |
||
71 | 5 | public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations) |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | * |
||
83 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
84 | */ |
||
85 | 4 | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | * |
||
93 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
94 | */ |
||
95 | 3 | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 1 | public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | * |
||
111 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
112 | */ |
||
113 | 2 | public function today($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | * |
||
134 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
135 | */ |
||
136 | 1 | public function historical($sourceName, $currencyCode, \DateTime $date, $rateType = RateType::MEDIAN) |
|
137 | { |
||
138 | 1 | $currencyCode = CurrencyCodeUtil::clean($currencyCode); |
|
139 | |||
140 | 1 | if ($this->has($sourceName, $currencyCode, $date, $rateType)) { |
|
141 | return $this->get($sourceName, $currencyCode, $date, $rateType); |
||
142 | } |
||
143 | |||
144 | 1 | if ((int) $date->format('N') === 6 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P1D')), $rateType)) { |
|
145 | return $this->get($sourceName, $currencyCode, $lastFriday, $rateType); |
||
146 | 1 | View Code Duplication | } elseif ((int) $date->format('N') === 7 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P2D')), $rateType)) { |
147 | 1 | return $this->get($sourceName, $currencyCode, $lastFriday, $rateType); |
|
148 | } |
||
149 | |||
150 | $message = sprintf('Rate for currency code "%s" of type "%s" from source "%s" is not available for historical date "%s".', $currencyCode, $rateType, $sourceName, $date->format('Y-m-d')); |
||
151 | $this->getLogger()->critical($message); |
||
152 | throw new ExchangeRateException($message); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 1 | public function fetch($sourceName = null, \DateTime $date = null) |
|
193 | |||
194 | /** |
||
195 | * Get base currency |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getBaseCurrency() |
||
203 | } |
||
204 |
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.