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 |
||
10 | class Field implements FieldAbstractorContract |
||
11 | { |
||
12 | /** |
||
13 | * @var Column |
||
14 | */ |
||
15 | protected $dbal; |
||
16 | /** |
||
17 | * @var FormManagerField |
||
18 | */ |
||
19 | protected $formField; |
||
20 | protected $name; |
||
21 | protected $value; |
||
22 | protected $presentation; |
||
23 | protected $validationRules; |
||
24 | protected $functions; |
||
25 | protected $options; |
||
26 | protected $hideValue; |
||
27 | protected $saveIfEmpty; |
||
28 | protected $noValidate; |
||
29 | |||
30 | 5 | public function __construct(Column $column, FormManagerField $formField, $name, $presentation = null) |
|
31 | { |
||
32 | 5 | $this->dbal = $column; |
|
33 | 5 | $this->formField = $formField; |
|
34 | 5 | $this->name = $name; |
|
35 | 5 | $this->presentation = $presentation; |
|
36 | 5 | $this->validationRules = []; |
|
37 | 5 | $this->functions = []; |
|
38 | 5 | $this->options = []; |
|
39 | 5 | $this->hideValue = false; |
|
40 | 5 | $this->saveIfEmpty = true; |
|
41 | 5 | $this->noValidate = false; |
|
42 | 5 | } |
|
43 | |||
44 | public function __clone() |
||
45 | { |
||
46 | $formField = clone $this->formField; |
||
47 | $this->formField = $formField; |
||
48 | $field = new self($this->dbal, $formField, $this->name, $this->presentation); |
||
49 | |||
50 | return $field; |
||
51 | } |
||
52 | |||
53 | public function getName() |
||
54 | { |
||
55 | return $this->name; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param string $name |
||
60 | */ |
||
61 | public function setName($name) |
||
62 | { |
||
63 | $this->name = $name; |
||
64 | } |
||
65 | |||
66 | 1 | View Code Duplication | public function presentation() |
|
|||
67 | { |
||
68 | 1 | if ($this->presentation) { |
|
69 | 1 | return transcrud($this->presentation); |
|
70 | } |
||
71 | |||
72 | $nameWithSpaces = str_replace('_', ' ', $this->name); |
||
73 | $namePieces = explode(' ', $nameWithSpaces); |
||
74 | $namePieces = array_filter(array_map('trim', $namePieces)); |
||
75 | |||
76 | return transcrud(ucfirst(implode(' ', $namePieces))); |
||
77 | } |
||
78 | |||
79 | 1 | public function type() |
|
80 | { |
||
81 | 1 | return $this->dbal->getType(); |
|
82 | } |
||
83 | |||
84 | public function setValidationRules($rules) |
||
85 | { |
||
86 | $this->validationRules = explode('|', $rules); |
||
87 | } |
||
88 | |||
89 | 1 | public function getValidationRules() |
|
90 | { |
||
91 | 1 | if (count($this->validationRules) === 0 && $this->noValidate() === false) { |
|
92 | 1 | if ($this->dbal->getNotnull()) { |
|
93 | 1 | $this->validationRules[] = 'required'; |
|
94 | 1 | } |
|
95 | 1 | } |
|
96 | |||
97 | 1 | return implode('|', $this->validationRules); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | */ |
||
103 | public function getValidationRulesArray() |
||
104 | { |
||
105 | return explode('|', $this->getValidationRules()); |
||
106 | } |
||
107 | |||
108 | public function setFunctions($functions) |
||
109 | { |
||
110 | if (!is_array($functions)) { |
||
111 | $functions = [$functions]; |
||
112 | } |
||
113 | |||
114 | $this->functions = $functions; |
||
115 | } |
||
116 | |||
117 | public function applyFunctions($value) |
||
118 | { |
||
119 | foreach ($this->functions as $function) { |
||
120 | if (!function_exists($function)) { |
||
121 | throw new \Exception('Function '.$function.' does not exist'); |
||
122 | } |
||
123 | |||
124 | $value = call_user_func($function, $value); |
||
125 | } |
||
126 | |||
127 | return $value; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $value |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | public function setValue($value) |
||
136 | { |
||
137 | $this->value = $value; |
||
138 | |||
139 | if (!$this->hideValue()) { |
||
140 | $this->formField->val($this->value); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return mixed |
||
146 | */ |
||
147 | public function getValue() |
||
148 | { |
||
149 | return $this->value; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param array $options |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public function setOptions(array $options) |
||
158 | { |
||
159 | $this->options = $options; |
||
160 | |||
161 | $this->formField->options($this->options); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return array |
||
166 | */ |
||
167 | public function getOptions() |
||
168 | { |
||
169 | return $this->options; |
||
170 | } |
||
171 | |||
172 | public function getFormField() |
||
173 | { |
||
174 | if (!$this->hideValue()) { |
||
175 | if (Request::old($this->name)) { |
||
176 | $this->formField->val(Request::old($this->name)); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return $this->formField; |
||
181 | } |
||
182 | |||
183 | public function hideValue($value = null) |
||
191 | |||
192 | public function saveIfEmpty($value = null) |
||
193 | { |
||
194 | if (!is_null($value)) { |
||
195 | $this->saveIfEmpty = $value; |
||
196 | } |
||
197 | |||
198 | return $this->saveIfEmpty; |
||
199 | } |
||
200 | |||
201 | 1 | public function noValidate($value = null) |
|
202 | { |
||
203 | 1 | if (!is_null($value)) { |
|
204 | $this->noValidate = $value; |
||
205 | } |
||
206 | |||
207 | 1 | return $this->noValidate; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param array $attributes |
||
212 | * |
||
213 | * @return void |
||
214 | */ |
||
215 | public function setFormElementAttributes(array $attributes) |
||
219 | } |
||
220 |
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.