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 |
||
| 7 | class Form |
||
| 8 | { |
||
| 9 | use GluesAttributes; |
||
| 10 | |||
| 11 | /** @var array */ |
||
| 12 | protected $params; |
||
| 13 | |||
| 14 | /** @var string */ |
||
| 15 | public $action; |
||
| 16 | |||
| 17 | /** @var string */ |
||
| 18 | public $method; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | public $httpMethod; |
||
| 22 | |||
| 23 | /** @var bool */ |
||
| 24 | public $files; |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | public $enctype; |
||
| 28 | |||
| 29 | /** @var Illuminate\Database\Eloquent\Model */ |
||
| 30 | public $model = null; |
||
| 31 | |||
| 32 | /** @var bool */ |
||
| 33 | public $autocomplete; |
||
| 34 | |||
| 35 | /** @var array */ |
||
| 36 | public $attributesList = [ |
||
| 37 | 'action', 'method', 'enctype', 'autocomplete' |
||
| 38 | ]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Will be called when the form is opened. |
||
| 42 | */ |
||
| 43 | public function setup(array $params) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Will be called when the form is closed. |
||
| 56 | */ |
||
| 57 | public function delete() |
||
| 65 | |||
| 66 | protected function setModel() |
||
| 72 | |||
| 73 | protected function setAction() |
||
| 79 | |||
| 80 | protected function setMethod() |
||
| 105 | |||
| 106 | protected function setFiles() |
||
| 113 | |||
| 114 | View Code Duplication | protected function setAutocomplete() |
|
| 123 | } |
||
| 124 |
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: