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 | class Form |
||
| 6 | { |
||
| 7 | /** @var array */ |
||
| 8 | protected $params; |
||
| 9 | |||
| 10 | /** @var string */ |
||
| 11 | public $action; |
||
| 12 | |||
| 13 | /** @var string */ |
||
| 14 | public $method; |
||
| 15 | |||
| 16 | /** @var bool */ |
||
| 17 | public $files; |
||
| 18 | |||
| 19 | /** @var Illuminate\Database\Eloquent\Model */ |
||
| 20 | public $model = null; |
||
| 21 | |||
| 22 | /** @var bool */ |
||
| 23 | public $autocomplete; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Will be called when the form is opened |
||
| 27 | */ |
||
| 28 | public function setup(array $params) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Will be called when the form is closed |
||
| 41 | */ |
||
| 42 | public function delete() |
||
| 50 | |||
| 51 | protected function setModel() |
||
| 57 | |||
| 58 | protected function setAction() |
||
| 64 | |||
| 65 | protected function setMethod() |
||
| 84 | |||
| 85 | protected function setFiles() |
||
| 91 | |||
| 92 | View Code Duplication | protected function setAutocomplete() |
|
| 101 | } |
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: