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 |
||
| 30 | class DoctrineDbalRepository implements RepositoryInterface |
||
| 31 | { |
||
| 32 | use FilterUtilHelper; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Connection |
||
| 36 | */ |
||
| 37 | private $connection; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $tableName; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var RateInterface[] |
||
| 46 | */ |
||
| 47 | private $identityMap = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * DoctrineDbalRepository constructor. |
||
| 51 | * |
||
| 52 | * @param Connection $connection Dbal connection. |
||
| 53 | * @param string|null $tableName Table name in which rates will be stored, or NULL for 'runopencode_exchange_rate'. |
||
| 54 | */ |
||
| 55 | 10 | public function __construct(Connection $connection, $tableName = null) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 8 | public function save(array $rates) |
|
| 67 | { |
||
| 68 | 8 | $this->connection->beginTransaction(); |
|
| 69 | |||
| 70 | try { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var RateInterface $rate |
||
| 74 | */ |
||
| 75 | 8 | foreach ($rates as $rate) { |
|
| 76 | |||
| 77 | 8 | if ($this->has($rate->getSourceName(), $rate->getCurrencyCode(), $rate->getDate(), $rate->getRateType())) { |
|
| 78 | |||
| 79 | 1 | $this->connection->executeQuery(sprintf('UPDATE %s SET rate_value = :rate_value, modified_at = :modified_at WHERE source_name = :source_name AND currency_code = :currency_code AND rate_date = :rate_date AND rate_type = :rate_type;', $this->tableName), [ |
|
| 80 | 1 | 'rate_value' => (float) $rate->getValue(), |
|
| 81 | 1 | 'source_name' => $rate->getSourceName(), |
|
| 82 | 1 | 'currency_code' => $rate->getCurrencyCode(), |
|
| 83 | 1 | 'rate_date' => $rate->getDate()->format('Y-m-d'), |
|
| 84 | 1 | 'rate_type' => $rate->getRateType(), |
|
| 85 | 1 | 'modified_at' => date('Y-m-d H:i:s'), |
|
| 86 | ]); |
||
| 87 | |||
| 88 | 1 | continue; |
|
| 89 | } |
||
| 90 | |||
| 91 | 8 | $this->connection->executeQuery(sprintf('INSERT INTO %s (source_name, rate_value, currency_code, rate_type, rate_date, base_currency_code, created_at, modified_at) VALUES (:source_name, :rate_value, :currency_code, :rate_type, :rate_date, :base_currency_code, :created_at, :modified_at);', $this->tableName), [ |
|
| 92 | 8 | 'source_name' => $rate->getSourceName(), |
|
| 93 | 8 | 'rate_value' => (float) $rate->getValue(), |
|
| 94 | 8 | 'currency_code' => $rate->getCurrencyCode(), |
|
| 95 | 8 | 'rate_type' => $rate->getRateType(), |
|
| 96 | 8 | 'rate_date' => $rate->getDate()->format('Y-m-d'), |
|
| 97 | 8 | 'base_currency_code' => $rate->getBaseCurrencyCode(), |
|
| 98 | 8 | 'created_at' => date('Y-m-d H:i:s'), |
|
| 99 | 8 | 'modified_at' => date('Y-m-d H:i:s'), |
|
| 100 | ]); |
||
| 101 | } |
||
| 102 | |||
| 103 | 7 | $this->connection->commit(); |
|
| 104 | 7 | $this->identityMap = []; |
|
| 105 | 1 | } catch (\Exception $e) { |
|
| 106 | 1 | $this->connection->rollBack(); |
|
| 107 | 1 | throw new ExchangeRateException('Unable to save rates.', 0, $e); |
|
| 108 | } |
||
| 109 | 7 | } |
|
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 2 | public function delete(array $rates) |
|
| 115 | { |
||
| 116 | 2 | $this->connection->beginTransaction(); |
|
| 117 | |||
| 118 | try { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var RateInterface $rate |
||
| 122 | */ |
||
| 123 | 2 | foreach ($rates as $rate) { |
|
| 124 | 2 | $key = $this->getRateKey($rate->getCurrencyCode(), $rate->getDate(), $rate->getRateType(), $rate->getSourceName()); |
|
| 125 | |||
| 126 | 2 | if (isset($this->identityMap[$key])) { |
|
| 127 | 1 | unset($this->identityMap[$key]); |
|
| 128 | } |
||
| 129 | |||
| 130 | 2 | $this->connection->executeQuery(sprintf('DELETE FROM %s WHERE source_name = :source_name AND currency_code = :currency_code AND rate_date = :rate_date AND rate_type = :rate_type;', $this->tableName), [ |
|
| 131 | 2 | 'source_name' => $rate->getSourceName(), |
|
| 132 | 2 | 'currency_code' => $rate->getCurrencyCode(), |
|
| 133 | 2 | 'rate_date' => $rate->getDate()->format('Y-m-d'), |
|
| 134 | 2 | 'rate_type' => $rate->getRateType(), |
|
| 135 | ]); |
||
| 136 | } |
||
| 137 | |||
| 138 | 1 | $this->connection->commit(); |
|
| 139 | 1 | } catch (\Exception $e) { |
|
| 140 | 1 | $this->connection->rollBack(); |
|
| 141 | 1 | throw new ExchangeRateException('Unable to delete rates.', 0, $e); |
|
| 142 | } |
||
| 143 | 1 | } |
|
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | 8 | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | 8 | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | 2 | public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | 3 | public function all(array $criteria = array()) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | 1 | public function count() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Builds rate key to speed up search. |
||
| 318 | * |
||
| 319 | * @param string $currencyCode |
||
| 320 | * @param \DateTime $date |
||
| 321 | * @param string $rateType |
||
| 322 | * @param string $sourceName |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | 9 | View Code Duplication | protected function getRateKey($currencyCode, $date, $rateType, $sourceName) |
| 333 | |||
| 334 | /** |
||
| 335 | * Initialize table schema where rates would be stored. |
||
| 336 | */ |
||
| 337 | 10 | protected function initialize() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Build rate from table row data. |
||
| 362 | * |
||
| 363 | * @param array $row Row data. |
||
| 364 | * @return Rate |
||
| 365 | */ |
||
| 366 | 6 | private function buildRateFromTableRowData(array $row) |
|
| 379 | } |
||
| 380 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: