| Total Complexity | 54 | 
| Total Lines | 413 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| 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) | ||
| 58 |     { | ||
| 59 | $this->title_required = $bool; | ||
| 60 | |||
| 61 | return $this; | ||
| 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) | ||
| 132 |     { | ||
| 133 |         if (! is_callable($this->uploadPath)) { | ||
| 134 | return $this->defaultUploadPath($file); | ||
| 135 | } | ||
| 136 | |||
| 137 | return call_user_func($this->uploadPath, $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) | ||
| 172 |     { | ||
| 173 | $this->uploadFileName = $uploadFileName; | ||
| 174 | |||
| 175 | return $this; | ||
| 176 | } | ||
| 177 | |||
| 178 | /** | ||
| 179 | * @return array | ||
| 180 | */ | ||
| 181 | public function getUploadSettings() | ||
| 182 |     { | ||
| 183 |         if (empty($this->uploadSettings) && in_array(Upload::class, class_uses($this->getModel()))) { | ||
| 184 | return (array) array_get($this->getModel()->getUploadSettings(), $this->getPath()); | ||
| 185 | } | ||
| 186 | |||
| 187 | return $this->uploadSettings; | ||
| 188 | } | ||
| 189 | |||
| 190 | /** | ||
| 191 | * @param array $imageSettings | ||
| 192 | * | ||
| 193 | * @return $this | ||
| 194 | */ | ||
| 195 | public function setUploadSettings(array $imageSettings) | ||
| 196 |     { | ||
| 197 | $this->uploadSettings = $imageSettings; | ||
| 198 | |||
| 199 | return $this; | ||
| 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) | ||
| 208 |     { | ||
| 209 | $uploadRules = ['file', 'image', 'mime', 'size', 'dimensions', 'max', 'min', 'between']; | ||
| 210 | |||
| 211 |         foreach ($uploadRules as $uploadRule) { | ||
| 212 |             if (strpos($rule, $uploadRule) !== false) { | ||
| 213 | $this->uploadValidationRules[] = $rule; | ||
| 214 | |||
| 215 |                 if (is_null($message)) { | ||
| 216 | return $this; | ||
| 217 | } | ||
| 218 | |||
| 219 | return $this->addValidationMessage($rule, $message); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | return parent::addValidationRule($rule, $message); | ||
| 224 | } | ||
| 225 | |||
| 226 | /** | ||
| 227 | * @param int $size Max size in kilobytes | ||
| 228 | * | ||
| 229 | * @return $this | ||
| 230 | */ | ||
| 231 | public function maxSize($size) | ||
| 232 |     { | ||
| 233 |         $this->addValidationRule('max:'.(int) $size); | ||
| 234 | |||
| 235 | return $this; | ||
| 236 | } | ||
| 237 | |||
| 238 | /** | ||
| 239 | * @param int $size Max size in kilobytes | ||
| 240 | * | ||
| 241 | * @return $this | ||
| 242 | */ | ||
| 243 | public function minSize($size) | ||
| 244 |     { | ||
| 245 |         $this->addValidationRule('min:'.(int) $size); | ||
| 246 | |||
| 247 | return $this; | ||
| 248 | } | ||
| 249 | |||
| 250 | /** | ||
| 251 | * @param \Closure $callable | ||
| 252 | * @return $this | ||
| 253 | */ | ||
| 254 | public function setSaveCallback(\Closure $callable) | ||
| 255 |     { | ||
| 256 | $this->saveCallback = $callable; | ||
| 257 | |||
| 258 | return $this; | ||
| 259 | } | ||
| 260 | |||
| 261 | /** | ||
| 262 | * Return save callback. | ||
| 263 | * @return \Closure | ||
| 264 | */ | ||
| 265 | public function getSaveCallback() | ||
| 266 |     { | ||
| 267 | return $this->saveCallback; | ||
| 268 | } | ||
| 269 | |||
| 270 | /** | ||
| 271 | * @param UploadedFile $file | ||
| 272 | * @param string $path | ||
| 273 | * @param string $filename | ||
| 274 | * @param array $settings | ||
| 275 | * @return \Closure|array | ||
| 276 | */ | ||
| 277 | public function saveFile(UploadedFile $file, $path, $filename, array $settings) | ||
| 289 | } | ||
| 290 | |||
| 291 | /** | ||
| 292 | * @param \Illuminate\Validation\Validator $validator | ||
| 293 | */ | ||
| 294 | public function customValidation(\Illuminate\Validation\Validator $validator) | ||
| 295 |     { | ||
| 296 | } | ||
| 297 | |||
| 298 | /** | ||
| 299 | * @param UploadedFile $file | ||
| 300 | * | ||
| 301 | * @return string | ||
| 302 | */ | ||
| 303 | public function defaultUploadFilename(UploadedFile $file) | ||
| 304 |     { | ||
| 305 | return md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension(); | ||
| 306 | } | ||
| 307 | |||
| 308 | /** | ||
| 309 | * @param UploadedFile $file | ||
| 310 | * | ||
| 311 | * @return string | ||
| 312 | */ | ||
| 313 | public function defaultUploadPath(UploadedFile $file) | ||
| 314 |     { | ||
| 315 |         return config('sleeping_owl.filesUploadDirectory', 'files/uploads'); | ||
| 316 | } | ||
| 317 | |||
| 318 | /** | ||
| 319 | * @return array|mixed|string | ||
| 320 | */ | ||
| 321 | public function getValueFromModel() | ||
| 322 |     { | ||
| 323 |         $value = $this->model->{$this->name}; | ||
| 324 | $return = isset($value) && mb_strlen($value) >= 5 ? json_decode($value, true) : []; | ||
| 325 | |||
| 326 | return $return; | ||
| 327 | } | ||
| 328 | |||
| 329 | /** | ||
| 330 | * @param string $mode | ||
| 331 | * | ||
| 332 | * @return $this | ||
| 333 | */ | ||
| 334 | public function setListMode($mode) | ||
| 335 |     { | ||
| 336 |         if ($mode == 'vertical') { | ||
| 337 | $this->files_group_class = 'files-group-vertical'; | ||
| 338 |         } elseif ($mode == 'horizontal') { | ||
| 339 | $this->files_group_class = null; | ||
| 340 | } | ||
| 341 | |||
| 342 | return $this; | ||
| 343 | } | ||
| 344 | |||
| 345 | /** | ||
| 346 | * @return $this | ||
| 347 | */ | ||
| 348 | public function setVertical() | ||
| 349 |     { | ||
| 350 |         $this->setListMode('vertical'); | ||
| 351 | |||
| 352 | return $this; | ||
| 353 | } | ||
| 354 | |||
| 355 | /** | ||
| 356 | * @return $this | ||
| 357 | */ | ||
| 358 | public function setHorizontal() | ||
| 363 | } | ||
| 364 | |||
| 365 | /** | ||
| 366 | * @param Request $request | ||
| 367 | */ | ||
| 368 | public function save(Request $request) | ||
| 369 |     { | ||
| 370 | $name = $this->getName(); | ||
| 371 | $value = Arr::get($request->all(), $this->getNameKey()); | ||
| 372 | |||
| 373 |         if (is_array($value_array = json_decode($value, true)) && count($value_array)) { | ||
| 374 |             foreach ($value_array as $v => $array) { | ||
| 375 | $file = $array['url']; | ||
| 376 |                 if ($file && File::exists($file)) { | ||
| 377 |                     if (! isset($array['filesize'])) { | ||
| 378 | $array['filesize'] = File::size($file); | ||
| 379 | } | ||
| 380 |                     if (! isset($array['ext'])) { | ||
| 381 | $array['ext'] = File::extension($file); | ||
| 382 | } | ||
| 383 | $mime = File::mimeType($file); | ||
| 384 |                     if (! isset($array['mime'])) { | ||
| 406 | ); | ||
| 407 | } | ||
| 408 | |||
| 409 | /** | ||
| 410 | * @return array | ||
| 411 | */ | ||
| 412 | public function toArray() | ||
| 427 | 
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: