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 |
||
24 | class FileValidator extends Validator |
||
25 | { |
||
26 | /** |
||
27 | * @var array|string a list of file name extensions that are allowed to be uploaded. |
||
28 | * This can be either an array or a string consisting of file extension names |
||
29 | * separated by space or comma (e.g. "gif, jpg"). |
||
30 | * Extension names are case-insensitive. Defaults to null, meaning all file name |
||
31 | * extensions are allowed. |
||
32 | * @see wrongExtension for the customized message for wrong file type. |
||
33 | */ |
||
34 | public $extensions; |
||
35 | /** |
||
36 | * @var bool whether to check file type (extension) with mime-type. If extension produced by |
||
37 | * file mime-type check differs from uploaded file extension, the file will be considered as invalid. |
||
38 | */ |
||
39 | public $checkExtensionByMimeType = true; |
||
40 | /** |
||
41 | * @var array|string a list of file MIME types that are allowed to be uploaded. |
||
42 | * This can be either an array or a string consisting of file MIME types |
||
43 | * separated by space or comma (e.g. "text/plain, image/png"). |
||
44 | * The mask with the special character `*` can be used to match groups of mime types. |
||
45 | * For example `image/*` will pass all mime types, that begin with `image/` (e.g. `image/jpeg`, `image/png`). |
||
46 | * Mime type names are case-insensitive. Defaults to null, meaning all MIME types are allowed. |
||
47 | * @see wrongMimeType for the customized message for wrong MIME type. |
||
48 | */ |
||
49 | public $mimeTypes; |
||
50 | /** |
||
51 | * @var int the minimum number of bytes required for the uploaded file. |
||
52 | * Defaults to null, meaning no limit. |
||
53 | * @see tooSmall for the customized message for a file that is too small. |
||
54 | */ |
||
55 | public $minSize; |
||
56 | /** |
||
57 | * @var int the maximum number of bytes required for the uploaded file. |
||
58 | * Defaults to null, meaning no limit. |
||
59 | * Note, the size limit is also affected by `upload_max_filesize` and `post_max_size` INI setting |
||
60 | * and the 'MAX_FILE_SIZE' hidden field value. See [[getSizeLimit()]] for details. |
||
61 | * @see http://php.net/manual/en/ini.core.php#ini.upload-max-filesize |
||
62 | * @see http://php.net/post-max-size |
||
63 | * @see getSizeLimit |
||
64 | * @see tooBig for the customized message for a file that is too big. |
||
65 | */ |
||
66 | public $maxSize; |
||
67 | /** |
||
68 | * @var int the maximum file count the given attribute can hold. |
||
69 | * Defaults to 1, meaning single file upload. By defining a higher number, |
||
70 | * multiple uploads become possible. Setting it to `0` means there is no limit on |
||
71 | * the number of files that can be uploaded simultaneously. |
||
72 | * |
||
73 | * > Note: The maximum number of files allowed to be uploaded simultaneously is |
||
74 | * also limited with PHP directive `max_file_uploads`, which defaults to 20. |
||
75 | * |
||
76 | * @see http://php.net/manual/en/ini.core.php#ini.max-file-uploads |
||
77 | * @see tooMany for the customized message when too many files are uploaded. |
||
78 | */ |
||
79 | public $maxFiles = 1; |
||
80 | /** |
||
81 | * @var string the error message used when a file is not uploaded correctly. |
||
82 | */ |
||
83 | public $message; |
||
84 | /** |
||
85 | * @var string the error message used when no file is uploaded. |
||
86 | * Note that this is the text of the validation error message. To make uploading files required, |
||
87 | * you have to set [[skipOnEmpty]] to `false`. |
||
88 | */ |
||
89 | public $uploadRequired; |
||
90 | /** |
||
91 | * @var string the error message used when the uploaded file is too large. |
||
92 | * You may use the following tokens in the message: |
||
93 | * |
||
94 | * - {attribute}: the attribute name |
||
95 | * - {file}: the uploaded file name |
||
96 | * - {limit}: the maximum size allowed (see [[getSizeLimit()]]) |
||
97 | * - {formattedLimit}: the maximum size formatted |
||
98 | * with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()]] |
||
99 | */ |
||
100 | public $tooBig; |
||
101 | /** |
||
102 | * @var string the error message used when the uploaded file is too small. |
||
103 | * You may use the following tokens in the message: |
||
104 | * |
||
105 | * - {attribute}: the attribute name |
||
106 | * - {file}: the uploaded file name |
||
107 | * - {limit}: the value of [[minSize]] |
||
108 | * - {formattedLimit}: the value of [[minSize]] formatted |
||
109 | * with [[\yii\i18n\Formatter::asShortSize()|Formatter::asShortSize()] |
||
110 | */ |
||
111 | public $tooSmall; |
||
112 | /** |
||
113 | * @var string the error message used if the count of multiple uploads exceeds limit. |
||
114 | * You may use the following tokens in the message: |
||
115 | * |
||
116 | * - {attribute}: the attribute name |
||
117 | * - {limit}: the value of [[maxFiles]] |
||
118 | */ |
||
119 | public $tooMany; |
||
120 | /** |
||
121 | * @var string the error message used when the uploaded file has an extension name |
||
122 | * that is not listed in [[extensions]]. You may use the following tokens in the message: |
||
123 | * |
||
124 | * - {attribute}: the attribute name |
||
125 | * - {file}: the uploaded file name |
||
126 | * - {extensions}: the list of the allowed extensions. |
||
127 | */ |
||
128 | public $wrongExtension; |
||
129 | /** |
||
130 | * @var string the error message used when the file has an mime type |
||
131 | * that is not allowed by [[mimeTypes]] property. |
||
132 | * You may use the following tokens in the message: |
||
133 | * |
||
134 | * - {attribute}: the attribute name |
||
135 | * - {file}: the uploaded file name |
||
136 | * - {mimeTypes}: the value of [[mimeTypes]] |
||
137 | */ |
||
138 | public $wrongMimeType; |
||
139 | |||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | 36 | public function init() |
|
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | 7 | public function validateAttribute($model, $attribute) |
|
218 | |||
219 | /** |
||
220 | * @inheritdoc |
||
221 | */ |
||
222 | 31 | protected function validateValue($value) |
|
280 | |||
281 | /** |
||
282 | * Returns the maximum size allowed for uploaded files. |
||
283 | * |
||
284 | * This is determined based on four factors: |
||
285 | * |
||
286 | * - 'upload_max_filesize' in php.ini |
||
287 | * - 'post_max_size' in php.ini |
||
288 | * - 'MAX_FILE_SIZE' hidden field |
||
289 | * - [[maxSize]] |
||
290 | * |
||
291 | * @return int the size limit for uploaded files. |
||
292 | */ |
||
293 | 2 | public function getSizeLimit() |
|
315 | |||
316 | /** |
||
317 | * @inheritdoc |
||
318 | * @param bool $trim |
||
319 | */ |
||
320 | 1 | public function isEmpty($value, $trim = false) |
|
325 | |||
326 | /** |
||
327 | * Converts php.ini style size to bytes. |
||
328 | * |
||
329 | * @param string $sizeStr $sizeStr |
||
330 | * @return int |
||
331 | */ |
||
332 | 2 | private function sizeToBytes($sizeStr) |
|
348 | |||
349 | /** |
||
350 | * Checks if given uploaded file have correct type (extension) according current validator settings. |
||
351 | * @param UploadedFile $file |
||
352 | * @return bool |
||
353 | */ |
||
354 | 14 | protected function validateExtension($file) |
|
377 | |||
378 | /** |
||
379 | * Builds the RegExp from the $mask. |
||
380 | * |
||
381 | * @param string $mask |
||
382 | * @return string the regular expression |
||
383 | * @see mimeTypes |
||
384 | */ |
||
385 | 11 | public function buildMimeTypeRegexp($mask) |
|
389 | |||
390 | /** |
||
391 | * Checks the mimeType of the $file against the list in the [[mimeTypes]] property. |
||
392 | * |
||
393 | * @param UploadedFile $file |
||
394 | * @return bool whether the $file mimeType is allowed |
||
395 | * @throws \yii\base\InvalidConfigException |
||
396 | * @see mimeTypes |
||
397 | * @since 2.0.8 |
||
398 | */ |
||
399 | 12 | protected function validateMimeType($file) |
|
415 | } |
||
416 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.