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 | 121 | public function buildFields() |
|
| 26 | { |
||
| 27 | 121 | if (!empty($this->data['fields'])) { |
|
| 28 | 85 | $this->addFields($this->data['fields']); |
|
|
|
|||
| 29 | 85 | } |
|
| 30 | 121 | } |
|
| 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 | 88 | public function addFields($fieldsList) |
|
| 49 | { |
||
| 50 | 88 | foreach ($fieldsList as $fieldName => $fieldConfig) { |
|
| 51 | |||
| 52 | 88 | if ($fieldConfig instanceof FieldInterface) { |
|
| 53 | 9 | $this->fields[$fieldConfig->getName()] = $fieldConfig; |
|
| 54 | 9 | continue; |
|
| 55 | 82 | } elseif($fieldConfig instanceof InputFieldInterface) { |
|
| 56 | 2 | $this->fields[$fieldConfig->getName()] = $fieldConfig; |
|
| 57 | 2 | continue; |
|
| 58 | } else { |
||
| 59 | 82 | $this->addField($fieldName, $this->buildFieldConfig($fieldName, $fieldConfig)); |
|
| 60 | } |
||
| 61 | 88 | } |
|
| 62 | |||
| 63 | 88 | return $this; |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param FieldInterface|string $field Field name or Field Object |
||
| 68 | * @param mixed $fieldInfo Field Type or Field Config array |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | 98 | public function addField($field, $fieldInfo = null) |
|
| 72 | { |
||
| 73 | 98 | if (!($field instanceof FieldInterface)) { |
|
| 74 | 92 | $field = new Field($this->buildFieldConfig($field, $fieldInfo)); |
|
| 75 | 92 | } |
|
| 76 | |||
| 77 | 98 | $this->fields[$field->getName()] = $field; |
|
| 78 | |||
| 79 | 98 | return $this; |
|
| 80 | } |
||
| 81 | |||
| 82 | 92 | View Code Duplication | protected function buildFieldConfig($name, $info = null) |
| 83 | { |
||
| 84 | 92 | if (!is_array($info)) { |
|
| 85 | $info = [ |
||
| 86 | 68 | 'type' => $info, |
|
| 87 | 68 | 'name' => $name, |
|
| 88 | 68 | ]; |
|
| 89 | 92 | } elseif (empty($info['name'])) { |
|
| 90 | 76 | $info['name'] = $name; |
|
| 91 | 76 | } |
|
| 92 | |||
| 93 | 92 | return $info; |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $name |
||
| 98 | * |
||
| 99 | * @return Field |
||
| 100 | */ |
||
| 101 | 76 | public function getField($name) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @param $name |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | 80 | public function hasField($name) |
|
| 115 | |||
| 116 | 84 | public function hasFields() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return Field[] |
||
| 123 | */ |
||
| 124 | 84 | public function getFields() |
|
| 128 | |||
| 129 | 1 | public function removeField($name) |
|
| 130 | { |
||
| 131 | 1 | if ($this->hasField($name)) { |
|
| 132 | 1 | unset($this->fields[$name]); |
|
| 137 | } |
||
| 138 |
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: