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 |
||
| 15 | class JSON extends Provider |
||
| 16 | { |
||
| 17 | /* @var string $path */ |
||
| 18 | private $path; |
||
| 19 | |||
| 20 | /* @var string $file */ |
||
| 21 | private $file; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * JSON constructor. |
||
| 25 | * |
||
| 26 | * @param string $xuid |
||
| 27 | * @param string $file |
||
| 28 | */ |
||
| 29 | public function __construct(string $xuid, string $file) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param array $data |
||
| 37 | */ |
||
| 38 | public function init(array $data = []): void |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param array $table |
||
| 47 | * |
||
| 48 | * @return void |
||
| 49 | */ |
||
| 50 | public function createTable(array $table = []): void |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | public function hasTable(): bool |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function deleteTable(): void |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $key |
||
| 78 | * @param bool|mixed $data |
||
| 79 | * |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public function set(string $key, $data): void |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $key |
||
| 91 | * |
||
| 92 | * @return bool|mixed |
||
| 93 | */ |
||
| 94 | public function get(string $key) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | public function getAll(): array |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getKeys(): array |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $key |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function exists(string $key): bool |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $key |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function remove(string $key): void |
||
| 145 | } |
||
| 146 |
If you suppress an error, we recommend checking for the error condition explicitly: