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 |
||
9 | class Form |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $args; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $dependencies; |
||
20 | |||
21 | /** |
||
22 | * @var Field |
||
23 | */ |
||
24 | protected $field; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $fields; |
||
30 | |||
31 | /** |
||
32 | * @var Normalizer |
||
33 | */ |
||
34 | protected $normalize; |
||
35 | |||
36 | public function __construct( Field $field, Normalizer $normalize ) |
||
44 | |||
45 | /** |
||
46 | * @param string $property |
||
47 | * |
||
48 | * @return mixed |
||
49 | * @throws Exception |
||
50 | */ |
||
51 | View Code Duplication | public function __get( $property ) |
|
61 | |||
62 | /** |
||
63 | * @param string $property |
||
64 | * @param string $value |
||
65 | * |
||
66 | * @return void |
||
67 | * @throws Exception |
||
68 | */ |
||
69 | View Code Duplication | public function __set( $property, $value ) |
|
81 | |||
82 | /** |
||
83 | * Add a field to the form |
||
84 | * |
||
85 | * @return Form |
||
86 | */ |
||
87 | public function addField( array $args = [] ) |
||
100 | |||
101 | /** |
||
102 | * Normalize the form arguments |
||
103 | * |
||
104 | * @return Form |
||
105 | */ |
||
106 | public function normalize( array $args = [] ) |
||
127 | |||
128 | /** |
||
129 | * Render the form |
||
130 | * |
||
131 | * @param mixed $print |
||
132 | * |
||
133 | * @return string|void |
||
134 | */ |
||
135 | public function render( $print = true ) |
||
149 | |||
150 | /** |
||
151 | * Reset the Form |
||
152 | * |
||
153 | * @return Form |
||
154 | */ |
||
155 | public function reset() |
||
162 | |||
163 | /** |
||
164 | * Generate the form fields |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | protected function generateFields() |
||
213 | |||
214 | /** |
||
215 | * Generate the form submit button |
||
216 | * |
||
217 | * @return null|string |
||
218 | */ |
||
219 | protected function generateSubmitButton() |
||
239 | |||
240 | /** |
||
241 | * @param object $field GeminiLabs\Castor\Form\Fields\* |
||
242 | * |
||
243 | * @return bool|null |
||
244 | */ |
||
245 | protected function isFieldHidden( $field ) |
||
259 | } |
||
260 |
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.