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 |
||
| 19 | class Database extends Medoo |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var array|null |
||
| 23 | */ |
||
| 24 | protected $config = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Database constructor. |
||
| 28 | * |
||
| 29 | * @param array $options |
||
| 30 | */ |
||
| 31 | 37 | public function __construct(array $options = null) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * reconnect database. |
||
| 43 | */ |
||
| 44 | public function reconnect() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param $query |
||
| 51 | * |
||
| 52 | * @return bool|\PDOStatement |
||
| 53 | */ |
||
| 54 | 2 | View Code Duplication | public function query($query) |
| 66 | |||
| 67 | /** |
||
| 68 | * @param $query |
||
| 69 | * |
||
| 70 | * @return bool|int |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function exec($query) |
|
| 84 | } |
||
| 85 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: