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 | trait ArrayAsPrimary |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get key name |
||
| 20 | * |
||
| 21 | * @return mixed |
||
| 22 | */ |
||
| 23 | public function getKeyName() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Set the keys for a save update query. |
||
| 30 | * |
||
| 31 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 32 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 33 | */ |
||
| 34 | View Code Duplication | protected function setKeysForSaveQuery(Builder $query) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Get the primary key value for a save query. |
||
| 51 | * |
||
| 52 | * @return mixed |
||
| 53 | */ |
||
| 54 | protected function getKeyValueForSaveQuery($key) |
||
| 62 | |||
| 63 | } |
||
| 64 |
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: