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 | 38 | public function behaviors() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @inheritdoc |
||
| 99 | * @codeCoverageIgnore |
||
| 100 | * @internal |
||
| 101 | */ |
||
| 102 | public function events() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @internal |
||
| 111 | */ |
||
| 112 | 35 | public function beforeSave($insert) |
|
| 133 | |||
| 134 | 34 | private function fillUserInfo() |
|
| 141 | |||
| 142 | 34 | private function fillMetaInfo() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Get extension By MimeType |
||
| 158 | * |
||
| 159 | * @param string $mimeType MimeType of the file |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | 34 | private function getExtensionByMimeType($mimeType) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Set a storage |
||
| 184 | * |
||
| 185 | * @param Storage $storage The Strorage for the file |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 35 | public function setStorage(Storage $storage) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Get a storage |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | * @throws InvalidParamException |
||
| 201 | */ |
||
| 202 | 36 | public function getStorage() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Generate a name |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 34 | public function generateName() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Checks whether the file is protected |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | 35 | public function isProtected() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Checks whether the file is unprotected |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 3 | public function isUnprotected() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Checks whether the file is temp |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | 12 | public function isTmp() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Get date create of file in format `Ym` |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 34 | public function getDateOfFile() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Checks whether the owner of the file |
||
| 267 | * |
||
| 268 | * @param int $ownerId The id of the owner |
||
| 269 | * @param int $ownerType The type of the owner |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | 21 | public function isOwner($ownerId, $ownerType) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Find all by owner |
||
| 283 | * |
||
| 284 | * @param int $ownerId The id of the owner |
||
| 285 | * @param int $ownerType The type of the owner |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | 19 | public static function findAllByOwner($ownerId, $ownerType) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Find one by owner |
||
| 298 | * |
||
| 299 | * @param int $ownerId The id of the owner |
||
| 300 | * @param int $ownerType The type of the owner |
||
| 301 | * @return File|null |
||
| 302 | */ |
||
| 303 | 1 | public static function findOneByOwner($ownerId, $ownerType) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Delete by owner |
||
| 312 | * |
||
| 313 | * @param Storage $storage The storage of the file |
||
| 314 | * @param int $ownerId The id of the owner |
||
| 315 | * @param int $ownerType The type of the owner |
||
| 316 | */ |
||
| 317 | 3 | public function deleteByOwner($storage, $ownerId, $ownerType) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Deleting a file from the db and from the file system |
||
| 329 | * @internal |
||
| 330 | * |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | 8 | public function beforeDelete() |
|
| 338 | } |
||
| 339 |