Complex classes like FileValidator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FileValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class FileValidator extends Validator |
||
28 | { |
||
29 | /** |
||
30 | * @var array|string a list of file name extensions that are allowed to be uploaded. |
||
31 | * This can be either an array or a string consisting of file extension names |
||
32 | * separated by space or comma (e.g. "gif, jpg"). |
||
33 | * Extension names are case-insensitive. Defaults to null, meaning all file name |
||
34 | * extensions are allowed. |
||
35 | * @see wrongExtension for the customized message for wrong file type. |
||
36 | */ |
||
37 | public $extensions; |
||
38 | /** |
||
39 | * @var boolean whether to check file type (extension) with mime-type. If extension produced by |
||
40 | * file mime-type check differs from uploaded file extension, the file will be considered as invalid. |
||
41 | */ |
||
42 | public $checkExtensionByMimeType = true; |
||
43 | /** |
||
44 | * @var array|string a list of file MIME types that are allowed to be uploaded. |
||
45 | * This can be either an array or a string consisting of file MIME types |
||
46 | * separated by space or comma (e.g. "text/plain, image/png"). |
||
47 | * The mask with the special character `*` can be used to match groups of mime types. |
||
48 | * For example `image/*` will pass all mime types, that begin with `image/` (e.g. `image/jpeg`, `image/png`). |
||
49 | * Mime type names are case-insensitive. Defaults to null, meaning all MIME types are allowed. |
||
50 | * @see wrongMimeType for the customized message for wrong MIME type. |
||
51 | */ |
||
52 | public $mimeTypes; |
||
53 | /** |
||
54 | * @var integer the minimum number of bytes required for the uploaded file. |
||
55 | * Defaults to null, meaning no limit. |
||
56 | * @see tooSmall for the customized message for a file that is too small. |
||
57 | */ |
||
58 | public $minSize; |
||
59 | /** |
||
60 | * @var integer the maximum number of bytes required for the uploaded file. |
||
61 | * Defaults to null, meaning no limit. |
||
62 | * Note, the size limit is also affected by `upload_max_filesize` and `post_max_size` INI setting |
||
63 | * and the 'MAX_FILE_SIZE' hidden field value. See [[getSizeLimit()]] for details. |
||
64 | * @see http://php.net/manual/en/ini.core.php#ini.upload-max-filesize |
||
65 | * @see http://php.net/post-max-size |
||
66 | * @see getSizeLimit |
||
67 | * @see tooBig for the customized message for a file that is too big. |
||
68 | */ |
||
69 | public $maxSize; |
||
70 | /** |
||
71 | * @var integer the maximum file count the given attribute can hold. |
||
72 | * Defaults to 1, meaning single file upload. By defining a higher number, |
||
73 | * multiple uploads become possible. Setting it to `0` means there is no limit on |
||
74 | * the number of files that can be uploaded simultaneously. |
||
75 | * |
||
76 | * > Note: The maximum number of files allowed to be uploaded simultaneously is |
||
77 | * also limited with PHP directive `max_file_uploads`, which defaults to 20. |
||
78 | * |
||
79 | * @see http://php.net/manual/en/ini.core.php#ini.max-file-uploads |
||
80 | * @see tooMany for the customized message when too many files are uploaded. |
||
81 | */ |
||
82 | public $maxFiles = 1; |
||
83 | /** |
||
84 | * @var string the error message used when a file is not uploaded correctly. |
||
85 | */ |
||
86 | public $message; |
||
87 | /** |
||
88 | * @var string the error message used when no file is uploaded. |
||
89 | * Note that this is the text of the validation error message. To make uploading files required, |
||
90 | * you have to set [[skipOnEmpty]] to `false`. |
||
91 | */ |
||
92 | public $uploadRequired; |
||
93 | /** |
||
94 | * @var string the error message used when the uploaded file is too large. |
||
95 | * You may use the following tokens in the message: |
||
96 | * |
||
97 | * - {attribute}: the attribute name |
||
98 | * - {file}: the uploaded file name |
||
99 | * - {limit}: the maximum size allowed (see [[getSizeLimit()]]) |
||
100 | * - {formattedLimit}: the maximum size formatted |
||
101 | * with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]] |
||
102 | */ |
||
103 | public $tooBig; |
||
104 | /** |
||
105 | * @var string the error message used when the uploaded file is too small. |
||
106 | * You may use the following tokens in the message: |
||
107 | * |
||
108 | * - {attribute}: the attribute name |
||
109 | * - {file}: the uploaded file name |
||
110 | * - {limit}: the value of [[minSize]] |
||
111 | * - {formattedLimit}: the value of [[minSize]] formatted |
||
112 | * with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()] |
||
113 | */ |
||
114 | public $tooSmall; |
||
115 | /** |
||
116 | * @var string the error message used if the count of multiple uploads exceeds limit. |
||
117 | * You may use the following tokens in the message: |
||
118 | * |
||
119 | * - {attribute}: the attribute name |
||
120 | * - {limit}: the value of [[maxFiles]] |
||
121 | */ |
||
122 | public $tooMany; |
||
123 | /** |
||
124 | * @var string the error message used when the uploaded file has an extension name |
||
125 | * that is not listed in [[extensions]]. You may use the following tokens in the message: |
||
126 | * |
||
127 | * - {attribute}: the attribute name |
||
128 | * - {file}: the uploaded file name |
||
129 | * - {extensions}: the list of the allowed extensions. |
||
130 | */ |
||
131 | public $wrongExtension; |
||
132 | /** |
||
133 | * @var string the error message used when the file has an mime type |
||
134 | * that is not allowed by [[mimeTypes]] property. |
||
135 | * You may use the following tokens in the message: |
||
136 | * |
||
137 | * - {attribute}: the attribute name |
||
138 | * - {file}: the uploaded file name |
||
139 | * - {mimeTypes}: the value of [[mimeTypes]] |
||
140 | */ |
||
141 | public $wrongMimeType; |
||
142 | |||
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | */ |
||
147 | 21 | public function init() |
|
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | 7 | public function validateAttribute($model, $attribute) |
|
221 | |||
222 | /** |
||
223 | * @inheritdoc |
||
224 | */ |
||
225 | 17 | protected function validateValue($file) |
|
283 | |||
284 | /** |
||
285 | * Returns the maximum size allowed for uploaded files. |
||
286 | * This is determined based on four factors: |
||
287 | * |
||
288 | * - 'upload_max_filesize' in php.ini |
||
289 | * - 'post_max_size' in php.ini |
||
290 | * - 'MAX_FILE_SIZE' hidden field |
||
291 | * - [[maxSize]] |
||
292 | * |
||
293 | * @return integer the size limit for uploaded files. |
||
294 | */ |
||
295 | 2 | public function getSizeLimit() |
|
313 | |||
314 | /** |
||
315 | * @inheritdoc |
||
316 | */ |
||
317 | 1 | public function isEmpty($value, $trim = false) |
|
322 | |||
323 | /** |
||
324 | * Converts php.ini style size to bytes |
||
325 | * |
||
326 | * @param string $sizeStr $sizeStr |
||
327 | * @return integer |
||
328 | */ |
||
329 | 2 | private function sizeToBytes($sizeStr) |
|
345 | |||
346 | /** |
||
347 | * Checks if given uploaded file have correct type (extension) according current validator settings. |
||
348 | * @param UploadedFile $file |
||
349 | * @return boolean |
||
350 | */ |
||
351 | 2 | protected function validateExtension($file) |
|
352 | { |
||
353 | 2 | $extension = mb_strtolower($file->extension, 'UTF-8'); |
|
354 | |||
355 | 2 | if ($this->checkExtensionByMimeType) { |
|
356 | |||
357 | $mimeType = FileHelper::getMimeType($file->tempName, null, false); |
||
358 | if ($mimeType === null) { |
||
359 | return false; |
||
360 | } |
||
361 | |||
362 | $extensionsByMimeType = FileHelper::getExtensionsByMimeType($mimeType); |
||
363 | |||
364 | if (!in_array($extension, $extensionsByMimeType, true)) { |
||
365 | return false; |
||
366 | } |
||
367 | } |
||
368 | |||
369 | 2 | if (!in_array($extension, $this->extensions, true)) { |
|
370 | 2 | return false; |
|
371 | } |
||
372 | |||
373 | 2 | return true; |
|
374 | } |
||
375 | |||
376 | /** |
||
377 | * @inheritdoc |
||
378 | */ |
||
379 | public function clientValidateAttribute($model, $attribute, $view) |
||
385 | |||
386 | /** |
||
387 | * Returns the client side validation options. |
||
388 | * @param \yii\base\Model $model the model being validated |
||
389 | * @param string $attribute the attribute name being validated |
||
390 | * @return array the client side validation options |
||
391 | */ |
||
392 | protected function getClientOptions($model, $attribute) |
||
459 | |||
460 | /** |
||
461 | * Builds the RegExp from the $mask |
||
462 | * |
||
463 | * @param string $mask |
||
464 | * @return string the regular expression |
||
465 | * @see mimeTypes |
||
466 | */ |
||
467 | 9 | private function buildMimeTypeRegexp($mask) |
|
471 | |||
472 | /** |
||
473 | * Checks the mimeType of the $file against the list in the [[mimeTypes]] property |
||
474 | * |
||
475 | * @param UploadedFile $file |
||
476 | * @return boolean whether the $file mimeType is allowed |
||
477 | * @throws \yii\base\InvalidConfigException |
||
478 | * @see mimeTypes |
||
479 | * @since 2.0.8 |
||
480 | */ |
||
481 | 10 | protected function validateMimeType($file) |
|
497 | } |
||
498 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.