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 |
||
36 | class File extends \yii\db\ActiveRecord |
||
37 | { |
||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | public $path; |
||
42 | /** |
||
43 | * @var Storage |
||
44 | */ |
||
45 | private $storage; |
||
46 | |||
47 | /** |
||
48 | * @inheritdoc |
||
49 | * @codeCoverageIgnore |
||
50 | * @internal |
||
51 | */ |
||
52 | public static function tableName() |
||
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | * @codeCoverageIgnore |
||
60 | * @internal |
||
61 | */ |
||
62 | public function attributeLabels() |
||
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | * @internal |
||
84 | */ |
||
85 | 37 | public function behaviors() |
|
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | * @codeCoverageIgnore |
||
100 | * @internal |
||
101 | */ |
||
102 | public function events() |
||
108 | |||
109 | /** |
||
110 | * @internal |
||
111 | */ |
||
112 | 34 | public function beforeSave($insert) |
|
133 | |||
134 | 33 | private function fillUserInfo() |
|
141 | |||
142 | 33 | private function fillMetaInfo() |
|
155 | |||
156 | 33 | private function getExtensionByMimeType($mimeType) |
|
177 | |||
178 | /** |
||
179 | * Set a storage |
||
180 | * |
||
181 | * @param Storage $storage The Strorage for the file |
||
182 | * @return string |
||
183 | */ |
||
184 | 34 | public function setStorage(Storage $storage) |
|
191 | |||
192 | /** |
||
193 | * Get a storage |
||
194 | * |
||
195 | * @return string |
||
196 | * @throws InvalidParamException |
||
197 | */ |
||
198 | 35 | public function getStorage() |
|
206 | |||
207 | /** |
||
208 | * Generate a name |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | 33 | public function generateName() |
|
217 | |||
218 | /** |
||
219 | * Checks whether the file is protected |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | 34 | public function isProtected() |
|
227 | |||
228 | /** |
||
229 | * Checks whether the file is unprotected |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 3 | public function isUnprotected() |
|
237 | |||
238 | /** |
||
239 | * Checks whether the file is temp |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | 12 | public function isTmp() |
|
247 | |||
248 | /** |
||
249 | * Get date create of file in format `Ym` |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | 33 | public function getDateOfFile() |
|
261 | |||
262 | /** |
||
263 | * Checks whether the owner of the file |
||
264 | * |
||
265 | * @param int $ownerId The id of the owner |
||
266 | * @param int $ownerType The type of the owner |
||
267 | * @return bool |
||
268 | */ |
||
269 | 20 | public function isOwner($ownerId, $ownerType) |
|
277 | |||
278 | /** |
||
279 | * Find all by owner |
||
280 | * |
||
281 | * @param int $ownerId The id of the owner |
||
282 | * @param int $ownerType The type of the owner |
||
283 | * @return array |
||
284 | */ |
||
285 | 18 | public static function findAllByOwner($ownerId, $ownerType) |
|
292 | |||
293 | /** |
||
294 | * Find one by owner |
||
295 | * |
||
296 | * @param int $ownerId The id of the owner |
||
297 | * @param int $ownerType The type of the owner |
||
298 | * @return File|null |
||
299 | */ |
||
300 | 1 | public static function findOneByOwner($ownerId, $ownerType) |
|
306 | |||
307 | /** |
||
308 | * Delete by owner |
||
309 | * |
||
310 | * @param Storage $storage The storage of the file |
||
311 | * @param int $ownerId The id of the owner |
||
312 | * @param int $ownerType The type of the owner |
||
313 | */ |
||
314 | 3 | public function deleteByOwner($storage, $ownerId, $ownerType) |
|
323 | |||
324 | /** |
||
325 | * Deleting a file from the db and from the file system |
||
326 | * @internal |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | 7 | public function beforeDelete() |
|
335 | } |
||
336 |