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 |
||
| 43 | class FieldsetCustomizationOptions extends AbstractOptions |
||
| 44 | { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Field specifications. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $fields = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Set the fields options. |
||
| 55 | * |
||
| 56 | * @param array $fields |
||
| 57 | * |
||
| 58 | * @return self |
||
| 59 | */ |
||
| 60 | public function setFields(array $fields) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get the field options. |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function getFields() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get the names of all customized fields. |
||
| 79 | * |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public function getFieldNames() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Has a field customized options? |
||
| 89 | * |
||
| 90 | * @param string $field |
||
| 91 | * |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | public function hasField($field) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Is a field enabled? |
||
| 101 | * |
||
| 102 | * Returns true, if |
||
| 103 | * - There is no customized $field. |
||
| 104 | * - The $field spec does not have the key 'enabled' |
||
| 105 | * - The key 'enabled' is NOT TRUE. |
||
| 106 | * |
||
| 107 | * @param string $field |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function isEnabled($field) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the field option array compatible with element spec. |
||
| 118 | * |
||
| 119 | * @param string $field |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function getFieldOptions($field) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the field flags array compatible with element flags. |
||
| 147 | * |
||
| 148 | * @param string $field |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function getFieldFlags($field) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Get input filter spec for a field. |
||
| 174 | * |
||
| 175 | * @param string $field |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | View Code Duplication | public function getFieldInputSpecification($field) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Copy specified keys from source to a new array. |
||
| 200 | * |
||
| 201 | * <pre> |
||
| 202 | * $keys = [ |
||
| 203 | * <name>, // copy $source[<name>] to $target[<name>] |
||
| 204 | * <name> => <key> // copy $source[<name>] to $target[<key][<name>] |
||
| 205 | * <name> => [<key>,<key>,..] // copy $source[<name>] to $target[<key>][<key>].. |
||
| 206 | * <name> => [ |
||
| 207 | * 'key' => <key>|[<key>,] // copy $source[<name>] to $target[<key]... |
||
| 208 | * // using '*' as <key> will be replaced by <name> |
||
| 209 | * 'value' => <mixed>, // do not use $source[<name>] but <mixed> as target value. |
||
| 210 | * 'if' => <mixed>, // only copy, if $source[<name>] equals <mixed> |
||
| 211 | * ] |
||
| 212 | * ] |
||
| 213 | * </pre> |
||
| 214 | * |
||
| 215 | * @param array $source |
||
| 216 | * @param array $keys |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | protected function copyArrayValues(array $source, array $keys) |
||
| 271 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.