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 |
||
| 5 | abstract class Item |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Constructor |
||
| 9 | * @param \vfalies\tmdb\Tmdb $tmdb |
||
| 10 | * @param int $item_id |
||
| 11 | * @param array $options |
||
| 12 | * @param string $item_name |
||
| 13 | * @throws \Exception |
||
| 14 | */ |
||
| 15 | 71 | public function __construct(\vfalies\tmdb\Tmdb $tmdb, int $item_id, array $options, string $item_name) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Get item poster |
||
| 33 | * @param string $size |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | 12 | View Code Duplication | public function getPoster(string $size = 'w185'): string |
| 52 | |||
| 53 | /** |
||
| 54 | * Get item backdrop |
||
| 55 | * @param string $size |
||
| 56 | * @return string|null |
||
| 57 | */ |
||
| 58 | 12 | View Code Duplication | public function getBackdrop(string $size = 'w780'): string |
| 74 | |||
| 75 | } |
||
| 76 |
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: