| Total Complexity | 56 | 
| Total Lines | 417 | 
| 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) | ||
| 38 | } | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @param bool $bool | ||
| 42 | * | ||
| 43 | * @return $this | ||
| 44 | */ | ||
| 45 | public function showDescription($bool) | ||
| 50 | } | ||
| 51 | |||
| 52 | /** | ||
| 53 | * @param bool $bool | ||
| 54 | * | ||
| 55 | * @return $this | ||
| 56 | */ | ||
| 57 | public function setTitleRequired($bool) | ||
| 58 |     { | ||
| 59 |         if ($this->show_title) { | ||
| 60 | $this->title_required = $bool; | ||
| 61 | } | ||
| 62 | |||
| 63 | return $this; | ||
| 64 | } | ||
| 65 | |||
| 66 | /** | ||
| 67 | * @param bool $bool | ||
| 68 | * | ||
| 69 | * @return $this | ||
| 70 | */ | ||
| 71 | public function setDescriptionRequired($bool) | ||
| 72 |     { | ||
| 73 |         if ($this->show_description) { | ||
| 74 | $this->description_required = $bool; | ||
| 75 | } | ||
| 76 | |||
| 77 | return $this; | ||
| 78 | } | ||
| 79 | |||
| 80 | /** | ||
| 81 | * @return array | ||
| 82 | */ | ||
| 83 | public function getUploadValidationMessages() | ||
| 84 |     { | ||
| 85 | $messages = []; | ||
| 86 |         foreach ($this->validationMessages as $rule => $message) { | ||
| 87 |             $messages["file.{$rule}"] = $message; | ||
| 88 | } | ||
| 89 | |||
| 90 | return $messages; | ||
| 91 | } | ||
| 92 | |||
| 93 | /** | ||
| 94 | * @return array | ||
| 95 | */ | ||
| 96 | public function getUploadValidationLabels() | ||
| 97 |     { | ||
| 98 | return ['file' => $this->getLabel()]; | ||
| 99 | } | ||
| 100 | |||
| 101 | /** | ||
| 102 | * @param $driver | ||
| 103 | * @param array $driverOptions | ||
| 104 | * @return $this | ||
| 105 | */ | ||
| 106 | public function setDriver($driver, $driverOptions = []) | ||
| 107 |     { | ||
| 108 | $this->driver = $driver; | ||
| 109 | $this->driverOptions = $driverOptions; | ||
| 110 | |||
| 111 | return $this; | ||
| 112 | } | ||
| 113 | |||
| 114 | /** | ||
| 115 | * @return array | ||
| 116 | */ | ||
| 117 | public function getDriver() | ||
| 118 |     { | ||
| 119 | return ['driver' => $this->driver, 'driverOptions' => $this->driverOptions]; | ||
| 120 | } | ||
| 121 | |||
| 122 | /** | ||
| 123 | * @return array | ||
| 124 | */ | ||
| 125 | public function getUploadValidationRules() | ||
| 126 |     { | ||
| 127 | return ['file' => array_unique($this->uploadValidationRules)]; | ||
| 128 | } | ||
| 129 | |||
| 130 | /** | ||
| 131 | * @param UploadedFile $file | ||
| 132 | * | ||
| 133 | * @return mixed | ||
| 134 | */ | ||
| 135 | public function getUploadPath(UploadedFile $file) | ||
| 136 |     { | ||
| 137 |         if (! is_callable($this->uploadPath)) { | ||
| 138 | return $this->defaultUploadPath($file); | ||
| 139 | } | ||
| 140 | |||
| 141 | return call_user_func($this->uploadPath, $file); | ||
| 142 | } | ||
| 143 | |||
| 144 | /** | ||
| 145 | * @param Closure $uploadPath | ||
| 146 | * | ||
| 147 | * @return $this | ||
| 148 | */ | ||
| 149 | public function setUploadPath(Closure $uploadPath) | ||
| 150 |     { | ||
| 151 | $this->uploadPath = $uploadPath; | ||
| 152 | |||
| 153 | return $this; | ||
| 154 | } | ||
| 155 | |||
| 156 | /** | ||
| 157 | * @param UploadedFile $file | ||
| 158 | * | ||
| 159 | * @return string | ||
| 160 | */ | ||
| 161 | public function getUploadFileName(UploadedFile $file) | ||
| 162 |     { | ||
| 163 |         if (! is_callable($this->uploadFileName)) { | ||
| 164 | return $this->defaultUploadFilename($file); | ||
| 165 | } | ||
| 166 | |||
| 167 | return call_user_func($this->uploadFileName, $file); | ||
| 168 | } | ||
| 169 | |||
| 170 | /** | ||
| 171 | * @param Closure $uploadFileName | ||
| 172 | * | ||
| 173 | * @return $this | ||
| 174 | */ | ||
| 175 | public function setUploadFileName(Closure $uploadFileName) | ||
| 176 |     { | ||
| 177 | $this->uploadFileName = $uploadFileName; | ||
| 178 | |||
| 179 | return $this; | ||
| 180 | } | ||
| 181 | |||
| 182 | /** | ||
| 183 | * @return array | ||
| 184 | */ | ||
| 185 | public function getUploadSettings() | ||
| 186 |     { | ||
| 187 |         if (empty($this->uploadSettings) && in_array(Upload::class, class_uses($this->getModel()))) { | ||
| 188 | return (array) array_get($this->getModel()->getUploadSettings(), $this->getPath()); | ||
| 189 | } | ||
| 190 | |||
| 191 | return $this->uploadSettings; | ||
| 192 | } | ||
| 193 | |||
| 194 | /** | ||
| 195 | * @param array $imageSettings | ||
| 196 | * | ||
| 197 | * @return $this | ||
| 198 | */ | ||
| 199 | public function setUploadSettings(array $imageSettings) | ||
| 200 |     { | ||
| 201 | $this->uploadSettings = $imageSettings; | ||
| 202 | |||
| 203 | return $this; | ||
| 204 | } | ||
| 205 | |||
| 206 | /** | ||
| 207 | * @param string $rule | ||
| 208 | * @param null $message | ||
| 209 | * @return $this|\SleepingOwl\Admin\Form\Element\File|\SleepingOwl\Admin\Form\Element\NamedFormElement | ||
| 210 | */ | ||
| 211 | public function addValidationRule($rule, $message = null) | ||
| 212 |     { | ||
| 213 | $uploadRules = ['file', 'image', 'mime', 'size', 'dimensions', 'max', 'min', 'between']; | ||
| 214 | |||
| 215 |         foreach ($uploadRules as $uploadRule) { | ||
| 216 |             if (strpos($rule, $uploadRule) !== false) { | ||
| 217 | $this->uploadValidationRules[] = $rule; | ||
| 218 | |||
| 219 |                 if (is_null($message)) { | ||
| 220 | return $this; | ||
| 221 | } | ||
| 222 | |||
| 223 | return $this->addValidationMessage($rule, $message); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | return parent::addValidationRule($rule, $message); | ||
| 228 | } | ||
| 229 | |||
| 230 | /** | ||
| 231 | * @param int $size Max size in kilobytes | ||
| 232 | * | ||
| 233 | * @return $this | ||
| 234 | */ | ||
| 235 | public function maxSize($size) | ||
| 236 |     { | ||
| 237 |         $this->addValidationRule('max:'.(int) $size); | ||
| 238 | |||
| 239 | return $this; | ||
| 240 | } | ||
| 241 | |||
| 242 | /** | ||
| 243 | * @param int $size Max size in kilobytes | ||
| 244 | * | ||
| 245 | * @return $this | ||
| 246 | */ | ||
| 247 | public function minSize($size) | ||
| 248 |     { | ||
| 249 |         $this->addValidationRule('min:'.(int) $size); | ||
| 250 | |||
| 251 | return $this; | ||
| 252 | } | ||
| 253 | |||
| 254 | /** | ||
| 255 | * @param \Closure $callable | ||
| 256 | * @return $this | ||
| 257 | */ | ||
| 258 | public function setSaveCallback(\Closure $callable) | ||
| 259 |     { | ||
| 260 | $this->saveCallback = $callable; | ||
| 261 | |||
| 262 | return $this; | ||
| 263 | } | ||
| 264 | |||
| 265 | /** | ||
| 266 | * Return save callback. | ||
| 267 | * @return \Closure | ||
| 268 | */ | ||
| 269 | public function getSaveCallback() | ||
| 270 |     { | ||
| 271 | return $this->saveCallback; | ||
| 272 | } | ||
| 273 | |||
| 274 | /** | ||
| 275 | * @param UploadedFile $file | ||
| 276 | * @param string $path | ||
| 277 | * @param string $filename | ||
| 278 | * @param array $settings | ||
| 279 | * @return \Closure|array | ||
| 280 | */ | ||
| 281 | public function saveFile(UploadedFile $file, $path, $filename, array $settings) | ||
| 293 | } | ||
| 294 | |||
| 295 | /** | ||
| 296 | * @param \Illuminate\Validation\Validator $validator | ||
| 297 | */ | ||
| 298 | public function customValidation(\Illuminate\Validation\Validator $validator) | ||
| 299 |     { | ||
| 300 | } | ||
| 301 | |||
| 302 | /** | ||
| 303 | * @param UploadedFile $file | ||
| 304 | * | ||
| 305 | * @return string | ||
| 306 | */ | ||
| 307 | public function defaultUploadFilename(UploadedFile $file) | ||
| 308 |     { | ||
| 309 | return md5(time().$file->getClientOriginalName()).'.'.$file->getClientOriginalExtension(); | ||
| 310 | } | ||
| 311 | |||
| 312 | /** | ||
| 313 | * @param UploadedFile $file | ||
| 314 | * | ||
| 315 | * @return string | ||
| 316 | */ | ||
| 317 | public function defaultUploadPath(UploadedFile $file) | ||
| 318 |     { | ||
| 319 |         return config('sleeping_owl.filesUploadDirectory', 'files/uploads'); | ||
| 320 | } | ||
| 321 | |||
| 322 | /** | ||
| 323 | * @return array|mixed|string | ||
| 324 | */ | ||
| 325 | public function getValueFromModel() | ||
| 326 |     { | ||
| 327 |         $value = $this->model->{$this->name}; | ||
| 328 | $return = isset($value) && mb_strlen($value) >= 5 ? json_decode($value, true) : []; | ||
| 329 | |||
| 330 | return $return; | ||
| 331 | } | ||
| 332 | |||
| 333 | /** | ||
| 334 | * @param string $mode | ||
| 335 | * | ||
| 336 | * @return $this | ||
| 337 | */ | ||
| 338 | public function setListMode($mode) | ||
| 339 |     { | ||
| 340 |         if ($mode == 'vertical') { | ||
| 341 | $this->files_group_class = 'files-group-vertical'; | ||
| 342 |         } elseif ($mode == 'horizontal') { | ||
| 343 | $this->files_group_class = null; | ||
| 344 | } | ||
| 345 | |||
| 346 | return $this; | ||
| 347 | } | ||
| 348 | |||
| 349 | /** | ||
| 350 | * @return $this | ||
| 351 | */ | ||
| 352 | public function setVertical() | ||
| 353 |     { | ||
| 354 |         $this->setListMode('vertical'); | ||
| 355 | |||
| 356 | return $this; | ||
| 357 | } | ||
| 358 | |||
| 359 | /** | ||
| 360 | * @return $this | ||
| 361 | */ | ||
| 362 | public function setHorizontal() | ||
| 367 | } | ||
| 368 | |||
| 369 | /** | ||
| 370 | * @param Request $request | ||
| 371 | */ | ||
| 372 | public function save(Request $request) | ||
| 410 | ); | ||
| 411 | } | ||
| 412 | |||
| 413 | /** | ||
| 414 | * @return array | ||
| 415 | */ | ||
| 416 | public function toArray() | ||
| 429 | } | ||
| 430 | } | ||
| 431 | 
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: