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 |
||
49 | */ |
||
50 | class Form implements Renderable |
||
51 | { |
||
52 | /** |
||
53 | * @var Field[] |
||
54 | */ |
||
55 | protected $fields = []; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $data = []; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $attributes = []; |
||
66 | |||
67 | /** |
||
68 | * Form constructor. |
||
69 | * |
||
70 | * @param array $data |
||
71 | */ |
||
72 | public function __construct($data = []) |
||
73 | { |
||
74 | if ($data instanceof Arrayable) { |
||
75 | $data = $data->toArray(); |
||
76 | } |
||
77 | |||
78 | if (!empty($data)) { |
||
79 | $this->data = $data; |
||
80 | } |
||
81 | |||
82 | $this->initFormAttributes(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Initialize the form attributes. |
||
87 | */ |
||
88 | protected function initFormAttributes() |
||
89 | { |
||
90 | $this->attributes = [ |
||
91 | 'method' => 'POST', |
||
92 | 'action' => '', |
||
93 | 'class' => 'form-horizontal', |
||
94 | 'accept-charset' => 'UTF-8', |
||
95 | 'pjax-container' => true, |
||
96 | 'fieldWidth' => 8, |
||
97 | 'labelWidth' => 2, |
||
98 | ]; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Action uri of the form. |
||
103 | * |
||
104 | * @param string $action |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function action($action) |
||
109 | { |
||
110 | return $this->attribute('action', $action); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Method of the form. |
||
115 | * |
||
116 | * @param string $method |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function method($method = 'POST') |
||
121 | { |
||
122 | return $this->attribute('method', strtoupper($method)); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Add form attributes. |
||
127 | * |
||
128 | * @param string|array $attr |
||
129 | * @param string $value |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function attribute($attr, $value = '') |
||
134 | { |
||
135 | if (is_array($attr)) { |
||
136 | foreach ($attr as $key => $value) { |
||
137 | $this->attribute($key, $value); |
||
138 | } |
||
139 | } else { |
||
140 | $this->attributes[$attr] = $value; |
||
141 | } |
||
142 | |||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Disable Pjax. |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function disablePjax() |
||
152 | { |
||
153 | array_forget($this->attributes, 'pjax-container'); |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Set field and label width in current form. |
||
160 | * |
||
161 | * @param int $fieldWidth |
||
162 | * @param int $labelWidth |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function setWidth($fieldWidth = 8, $labelWidth = 2) |
||
167 | { |
||
168 | collect($this->fields)->each(function ($field) use ($fieldWidth, $labelWidth) { |
||
169 | /* @var Field $field */ |
||
170 | $field->setWidth($fieldWidth, $labelWidth); |
||
171 | }); |
||
172 | $this->attributes['fieldWidth'] = $fieldWidth; |
||
173 | $this->attributes['labelWidth'] = $labelWidth; |
||
174 | |||
175 | return $this; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Find field class with given name. |
||
180 | * |
||
181 | * @param string $method |
||
182 | * |
||
183 | * @return bool|string |
||
184 | */ |
||
185 | public static function findFieldClass($method) |
||
186 | { |
||
187 | $class = array_get(\Encore\Admin\Form::$availableFields, $method); |
||
188 | |||
189 | if (class_exists($class)) { |
||
190 | return $class; |
||
191 | } |
||
192 | |||
193 | return false; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Add a form field to form. |
||
198 | * |
||
199 | * @param Field $field |
||
200 | * |
||
201 | * @return $this |
||
202 | */ |
||
203 | protected function pushField(Field &$field) |
||
204 | { |
||
205 | array_push($this->fields, $field); |
||
206 | |||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get variables for render form. |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | protected function getVariables() |
||
216 | { |
||
217 | foreach ($this->fields as $field) { |
||
218 | $field->fill($this->data); |
||
219 | } |
||
220 | |||
221 | return [ |
||
222 | 'fields' => $this->fields, |
||
223 | 'attributes' => $this->formatAttribute(), |
||
224 | <<<<<<< HEAD |
||
225 | 'fieldWidth' => $this->attributes['fieldWidth'], |
||
226 | 'labelWidth' => $this->attributes['labelWidth'] |
||
227 | ======= |
||
228 | 'method' => $this->attributes['method'], |
||
229 | >>>>>>> 1.5 |
||
230 | ]; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Format form attributes form array to html. |
||
235 | * |
||
236 | * @param array $attributes |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | public function formatAttribute($attributes = []) |
||
241 | { |
||
242 | $attributes = $attributes ?: $this->attributes; |
||
243 | |||
244 | if ($this->hasFile()) { |
||
245 | $attributes['enctype'] = 'multipart/form-data'; |
||
246 | } |
||
247 | |||
248 | $html = []; |
||
249 | foreach ($attributes as $key => $val) { |
||
250 | $html[] = "$key=\"$val\""; |
||
251 | } |
||
252 | |||
253 | return implode(' ', $html) ?: ''; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Determine if form fields has files. |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function hasFile() |
||
262 | { |
||
263 | foreach ($this->fields as $field) { |
||
264 | if ($field instanceof Field\File) { |
||
265 | return true; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | return false; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Generate a Field object and add to form builder if Field exists. |
||
274 | * |
||
275 | * @param string $method |
||
276 | * @param array $arguments |
||
277 | * |
||
278 | * @return Field|null |
||
279 | */ |
||
280 | public function __call($method, $arguments) |
||
281 | { |
||
282 | if ($className = static::findFieldClass($method)) { |
||
283 | $name = array_get($arguments, 0, ''); |
||
284 | |||
285 | $element = new $className($name, array_slice($arguments, 1)); |
||
286 | |||
287 | $this->pushField($element); |
||
288 | |||
289 | return $element; |
||
290 | } |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Render the form. |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | public function render() |
||
299 | { |
||
300 | return view('admin::widgets.form', $this->getVariables())->render(); |
||
301 | } |
||
302 | |||
303 | /** |
||
313 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.