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 |
||
| 21 | trait FieldsAwareConfigTrait |
||
| 22 | { |
||
| 23 | protected $fields = []; |
||
| 24 | |||
| 25 | 71 | public function buildFields() |
|
| 31 | |||
| 32 | /** |
||
| 33 | * Add fields from passed interface |
||
| 34 | * @param AbstractInterfaceType $interfaceType |
||
| 35 | * @return $this |
||
| 36 | */ |
||
| 37 | 1 | public function applyInterface(AbstractInterfaceType $interfaceType) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param array $fieldsList |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | 43 | public function addFields($fieldsList) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param AbstractField|string $field Field name or Field Object |
||
| 65 | * @param mixed $fieldInfo Field Type or Field Config array |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | 51 | public function addField($field, $fieldInfo = null) |
|
| 78 | |||
| 79 | 51 | View Code Duplication | protected function buildFieldConfig($name, $info = null) |
| 92 | |||
| 93 | /** |
||
| 94 | * @param $name |
||
| 95 | * |
||
| 96 | * @return Field |
||
| 97 | */ |
||
| 98 | 28 | public function getField($name) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param $name |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 33 | public function hasField($name) |
|
| 112 | |||
| 113 | 33 | public function hasFields() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return Field[] |
||
| 120 | */ |
||
| 121 | 33 | public function getFields() |
|
| 125 | |||
| 126 | 1 | public function removeField($name) |
|
| 134 | } |
||
| 135 |
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: