Total Complexity | 52 |
Total Lines | 389 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Files often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Files, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Files extends Images |
||
13 | { |
||
14 | protected $uploadValidationRules = ['required']; |
||
15 | |||
16 | protected $view = 'form.element.files'; |
||
17 | |||
18 | protected $files_group_class = null; |
||
19 | |||
20 | protected $show_title = true; |
||
21 | |||
22 | protected $show_description = true; |
||
23 | |||
24 | protected $title_required = false; |
||
25 | |||
26 | protected $description_required = false; |
||
27 | |||
28 | /** |
||
29 | * @param bool $bool |
||
30 | * |
||
31 | * @return $this |
||
32 | */ |
||
33 | public function showTitle($bool) |
||
34 | { |
||
35 | $this->show_title = $bool; |
||
36 | |||
37 | return $this; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param bool $bool |
||
42 | * |
||
43 | * @return $this |
||
44 | */ |
||
45 | public function showDescription($bool) |
||
46 | { |
||
47 | $this->show_description = $bool; |
||
48 | |||
49 | return $this; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param bool $bool |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function setTitleRequired($bool) |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param bool $bool |
||
66 | * |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function setDescriptionRequired($bool) |
||
70 | { |
||
71 | $this->description_required = $bool; |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getUploadValidationMessages() |
||
80 | { |
||
81 | $messages = []; |
||
82 | foreach ($this->validationMessages as $rule => $message) { |
||
83 | $messages["file.{$rule}"] = $message; |
||
84 | } |
||
85 | |||
86 | return $messages; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getUploadValidationLabels() |
||
93 | { |
||
94 | return ['file' => $this->getLabel()]; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param $driver |
||
99 | * @param array $driverOptions |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function setDriver($driver, $driverOptions = []) |
||
103 | { |
||
104 | $this->driver = $driver; |
||
105 | $this->driverOptions = $driverOptions; |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return array |
||
112 | */ |
||
113 | public function getDriver() |
||
114 | { |
||
115 | return ['driver' => $this->driver, 'driverOptions' => $this->driverOptions]; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return array |
||
120 | */ |
||
121 | public function getUploadValidationRules() |
||
122 | { |
||
123 | return ['file' => array_unique($this->uploadValidationRules)]; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param UploadedFile $file |
||
128 | * |
||
129 | * @return mixed |
||
130 | */ |
||
131 | public function getUploadPath(UploadedFile $file) |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param Closure $uploadPath |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function setUploadPath(Closure $uploadPath) |
||
146 | { |
||
147 | $this->uploadPath = $uploadPath; |
||
148 | |||
149 | return $this; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param UploadedFile $file |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getUploadFileName(UploadedFile $file) |
||
158 | { |
||
159 | if (! is_callable($this->uploadFileName)) { |
||
160 | return $this->defaultUploadFilename($file); |
||
161 | } |
||
162 | |||
163 | return call_user_func($this->uploadFileName, $file); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param Closure $uploadFileName |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function setUploadFileName(Closure $uploadFileName) |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return array |
||
180 | */ |
||
181 | public function getUploadSettings() |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param array $imageSettings |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setUploadSettings(array $imageSettings) |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param string $rule |
||
204 | * @param null $message |
||
205 | * @return $this|\SleepingOwl\Admin\Form\Element\File|\SleepingOwl\Admin\Form\Element\NamedFormElement |
||
206 | */ |
||
207 | public function addValidationRule($rule, $message = null) |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param \Closure $callable |
||
228 | * @return $this |
||
229 | */ |
||
230 | public function setSaveCallback(\Closure $callable) |
||
231 | { |
||
232 | $this->saveCallback = $callable; |
||
233 | |||
234 | return $this; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Return save callback. |
||
239 | * @return \Closure |
||
240 | */ |
||
241 | public function getSaveCallback() |
||
242 | { |
||
243 | return $this->saveCallback; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @param UploadedFile $file |
||
248 | * @param string $path |
||
249 | * @param string $filename |
||
250 | * @param array $settings |
||
251 | * @return \Closure|array |
||
252 | */ |
||
253 | public function saveFile(UploadedFile $file, $path, $filename, array $settings) |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param \Illuminate\Validation\Validator $validator |
||
269 | */ |
||
270 | public function customValidation(\Illuminate\Validation\Validator $validator) |
||
271 | { |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param UploadedFile $file |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | public function defaultUploadFilename(UploadedFile $file) |
||
280 | { |
||
281 | return md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension(); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param UploadedFile $file |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | public function defaultUploadPath(UploadedFile $file) |
||
290 | { |
||
291 | return config('sleeping_owl.filesUploadDirectory', 'files/uploads'); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @return array|mixed|string |
||
296 | */ |
||
297 | public function getValueFromModel() |
||
298 | { |
||
299 | $value = $this->model->{$this->name}; |
||
300 | $return = isset($value) && mb_strlen($value) >= 5 ? json_decode($value, true) : []; |
||
301 | |||
302 | return $return; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param string $mode |
||
307 | * |
||
308 | * @return $this |
||
309 | */ |
||
310 | public function setListMode($mode) |
||
311 | { |
||
312 | if ($mode == 'vertical') { |
||
313 | $this->files_group_class = 'files-group-vertical'; |
||
314 | } elseif ($mode == 'horizontal') { |
||
315 | $this->files_group_class = null; |
||
316 | } |
||
317 | |||
318 | return $this; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @return $this |
||
323 | */ |
||
324 | public function setVertical() |
||
325 | { |
||
326 | $this->setListMode('vertical'); |
||
327 | |||
328 | return $this; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @return $this |
||
333 | */ |
||
334 | public function setHorizontal() |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param Request $request |
||
343 | */ |
||
344 | public function save(Request $request) |
||
382 | ); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return array |
||
387 | */ |
||
388 | public function toArray() |
||
401 | } |
||
402 | } |
||
403 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: