Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class File extends NamedElement |
||
11 | { |
||
12 | protected $type = 'file'; |
||
13 | |||
14 | protected $actionUrl; |
||
15 | |||
16 | protected $multiSelect = false; |
||
17 | |||
18 | protected $multiFile = true; |
||
19 | |||
20 | protected $showFileList = true; |
||
21 | |||
22 | protected $withCredentials = false; |
||
23 | |||
24 | protected $maxFileSize; |
||
25 | |||
26 | protected $fileUploadsLimit = 0; |
||
27 | |||
28 | protected $fileExtensions; |
||
29 | |||
30 | protected $listType = 'text'; |
||
31 | |||
32 | protected $disk; |
||
33 | |||
34 | /** |
||
35 | * @var string|\Closure|null |
||
36 | */ |
||
37 | protected $uploadPath; |
||
38 | |||
39 | /** |
||
40 | * @var \Closure|null |
||
41 | */ |
||
42 | protected $uploadFileNameRule; |
||
43 | |||
44 | public function getValue() |
||
56 | |||
57 | public function getActionUrl() |
||
76 | |||
77 | public function setActionUrl($value) |
||
83 | |||
84 | public function isMultiSelect() |
||
88 | |||
89 | public function enableMultiSelect() |
||
95 | |||
96 | /** |
||
97 | * Show file list |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function disableFileList() |
||
107 | |||
108 | /** |
||
109 | * Indicates whether or not cross-site Access-Control requests |
||
110 | * should be made using credentials |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function withCredentials() |
||
120 | |||
121 | /** |
||
122 | * The maximum size of an uploaded file in bytes |
||
123 | * If didn't set maximum size, return maximum size as configured in php.ini. |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | public function getMaxFileSize() |
||
134 | |||
135 | /** |
||
136 | * The maximum size allowed for an uploaded file in bytes |
||
137 | * |
||
138 | * @param int $value |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function setMaxFileSize($value) |
||
148 | |||
149 | public function getFileExtensions() |
||
157 | |||
158 | /** |
||
159 | * A list of allowable extensions that can be uploaded. |
||
160 | * |
||
161 | * @param string $value |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function setFileExtensions($value) |
||
171 | |||
172 | protected function getDefaultExtensions() |
||
176 | |||
177 | public function getFileUploadsLimit() |
||
181 | |||
182 | /** |
||
183 | * The maximum number of files that can be uploaded. |
||
184 | * |
||
185 | * @param int $value |
||
186 | * |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function setFileUploadsLimit($value) |
||
195 | |||
196 | public function getListType() |
||
200 | |||
201 | public function toArray() |
||
213 | |||
214 | public function getDisk() |
||
222 | |||
223 | public function setDisk($value) |
||
229 | |||
230 | protected function getDefaultUploadPath(UploadedFile $file) |
||
234 | |||
235 | public function getUploadPath(UploadedFile $file) |
||
246 | |||
247 | /** |
||
248 | * The path of file save |
||
249 | * |
||
250 | * @param string|\Closure $value |
||
251 | * |
||
252 | * @return $this |
||
253 | */ |
||
254 | public function setUploadPath($value) |
||
260 | |||
261 | public function getUploadFileName(UploadedFile $file) |
||
269 | |||
270 | protected function getDefaultFileName(UploadedFile $file) |
||
275 | |||
276 | public function setUploadFileNameRule(\Closure $value) |
||
282 | |||
283 | public function saveFile(UploadedFile $file) |
||
300 | |||
301 | protected function prepareValue($value) |
||
308 | |||
309 | protected function existsFile($path) |
||
313 | |||
314 | protected function getFileUrl($path) |
||
318 | |||
319 | protected function getFileInfo($path) |
||
327 | } |
||
328 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.