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 |
||
| 14 | class YAML extends Provider |
||
| 15 | { |
||
| 16 | /* @var string $path */ |
||
| 17 | private $path; |
||
| 18 | |||
| 19 | /* @var string $file */ |
||
| 20 | private $file; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * JSON constructor. |
||
| 24 | * |
||
| 25 | * @param string $file |
||
| 26 | */ |
||
| 27 | public function __construct(string $file = 'config') |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param array $data |
||
| 35 | */ |
||
| 36 | public function init(array $data = []): void |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param array $table |
||
| 45 | * |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | public function createTable(array $table = []): void |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public function hasTable(): bool |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | public function deleteTable(): bool |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string $key |
||
| 77 | * @param bool|mixed $data |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function set(string $key, $data): void |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $key |
||
| 90 | * |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | public function get(string $key) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | public function getAll(): array |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | public function getKeys(): array |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $key |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function exists(string $key): bool |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $key |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function remove(string $key): void |
||
| 144 | } |
||
| 145 |
If you suppress an error, we recommend checking for the error condition explicitly: