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 |
||
| 9 | class Coins extends Api |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @return array |
||
| 13 | * @throws Exception |
||
| 14 | */ |
||
| 15 | 1 | public function getList(): array |
|
| 19 | |||
| 20 | /** |
||
| 21 | * @param string $vsCurrency |
||
| 22 | * @param array $params |
||
| 23 | * @return array |
||
| 24 | * @throws Exception |
||
| 25 | */ |
||
| 26 | 1 | public function getMarkets(string $vsCurrency, array $params = []): array |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $id |
||
| 35 | * @param array $params |
||
| 36 | * @return array |
||
| 37 | * @throws Exception |
||
| 38 | */ |
||
| 39 | 1 | public function getCoin(string $id, array $params = []): array |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $id |
||
| 46 | * @param array $params |
||
| 47 | * @return array |
||
| 48 | * @throws Exception |
||
| 49 | */ |
||
| 50 | 1 | public function getTickers(string $id, array $params = []): array |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $id |
||
| 57 | * @param string $date |
||
| 58 | * @param array $params |
||
| 59 | * @return array |
||
| 60 | * @throws Exception |
||
| 61 | */ |
||
| 62 | 1 | public function getHistory(string $id, string $date, array $params = []): array |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $id |
||
| 70 | * @param string $vsCurrency |
||
| 71 | * @param string $days |
||
| 72 | * @return array |
||
| 73 | * @throws Exception |
||
| 74 | */ |
||
| 75 | 1 | public function getMarketChart(string $id, string $vsCurrency, string $days): array |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $id |
||
| 84 | * @param string $vsCurrency |
||
| 85 | * @param string $from |
||
| 86 | * @param string $to |
||
| 87 | * @return array |
||
| 88 | * @throws Exception |
||
| 89 | */ |
||
| 90 | 1 | View Code Duplication | public function getMarketChartRange(string $id, string $vsCurrency, string $from, string $to): array |
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $id |
||
| 100 | * @param array $params |
||
| 101 | * @return array |
||
| 102 | * @throws Exception |
||
| 103 | */ |
||
| 104 | 1 | public function getStatusUpdates(string $id, array $params = []): array |
|
| 108 | } |
||
| 109 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.