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 |
||
25 | class MemoryRepository implements RepositoryInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var RateInterface[] |
||
29 | */ |
||
30 | private $rates; |
||
31 | |||
32 | 8 | public function __construct() |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 7 | public function save(array $rates) |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 1 | View Code Duplication | public function delete(array $rates) |
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 3 | View Code Duplication | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 3 | View Code Duplication | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
83 | { |
||
84 | 3 | if ($date === null) { |
|
85 | 3 | $date = new \DateTime('now'); |
|
86 | } |
||
87 | |||
88 | 3 | if ($this->has($sourceName, $currencyCode, $date, $rateType)) { |
|
89 | 2 | return $this->rates[$this->getRateKey($currencyCode, $date, $rateType, $sourceName)]; |
|
90 | } |
||
91 | |||
92 | 1 | throw new ExchangeRateException(sprintf('Could not fetch rate for rate currency code "%s" and rate type "%s" on date "%s".', $currencyCode, $rateType, $date->format('Y-m-d'))); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 2 | View Code Duplication | public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
99 | { |
||
100 | /** |
||
101 | * @var RateInterface $rate |
||
102 | */ |
||
103 | 2 | foreach ($this->rates as $rate) { |
|
104 | |||
105 | if ( |
||
106 | 1 | $rate->getSourceName() === $sourceName |
|
107 | && |
||
108 | 1 | $rate->getCurrencyCode() === $currencyCode |
|
109 | && |
||
110 | 1 | $rate->getRateType() === $rateType |
|
111 | ) { |
||
112 | 1 | return $rate; |
|
113 | } |
||
114 | } |
||
115 | |||
116 | 1 | throw new ExchangeRateException(sprintf('Could not fetch latest rate for rate currency code "%s" and rate type "%s" from source "%s".', $currencyCode, $rateType, $sourceName)); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 3 | View Code Duplication | public function all(array $criteria = array()) |
123 | { |
||
124 | 3 | if (count($criteria) == 0) { |
|
125 | 1 | return $this->rates; |
|
126 | } |
||
127 | |||
128 | 2 | $result = array(); |
|
129 | |||
130 | /** |
||
131 | * @var RateInterface $rate |
||
132 | */ |
||
133 | 2 | foreach ($this->rates as $rate) { |
|
134 | |||
135 | 2 | if (RateFilterUtil::matches($rate, $criteria)) { |
|
136 | 2 | $result[] = $rate; |
|
137 | } |
||
138 | } |
||
139 | |||
140 | 2 | return $this->paginate($result, $criteria); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 1 | public function count() |
|
150 | |||
151 | /** |
||
152 | * Builds rate key to speed up search. |
||
153 | * |
||
154 | * @param string $currencyCode |
||
155 | * @param \DateTime $date |
||
156 | * @param string $rateType |
||
157 | * @param string $sourceName |
||
158 | * @return string |
||
159 | */ |
||
160 | 7 | View Code Duplication | protected function getRateKey($currencyCode, $date, $rateType, $sourceName) |
168 | |||
169 | /** |
||
170 | * Extract requested page from filter criteria. |
||
171 | * |
||
172 | * @param array $rates Rates to filter for pagination. |
||
173 | * @param array $criteria Filter criteria. |
||
174 | * @return RateInterface[] Paginated rates. |
||
175 | */ |
||
176 | 2 | View Code Duplication | protected function paginate(array $rates, $criteria) |
192 | } |
||
193 |
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.