Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Mediafile 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 Mediafile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Mediafile extends ActiveRecord |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var Module |
||
| 36 | */ |
||
| 37 | private $_module; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public static function tableName() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | public function rules() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritdoc} |
||
| 112 | */ |
||
| 113 | public function attributeLabels() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return Module |
||
| 133 | */ |
||
| 134 | public function getModule(): Module |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Find model by url. |
||
| 150 | * |
||
| 151 | * @param string $url |
||
| 152 | * |
||
| 153 | * @return mixed |
||
| 154 | */ |
||
| 155 | public static function findByUrl(string $url) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Search models by file types. |
||
| 162 | * |
||
| 163 | * @param array $types |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | public static function findByTypes(array $types): array |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Add owner to mediafiles table. |
||
| 174 | * |
||
| 175 | * @param int $ownerId |
||
| 176 | * @param string $owner |
||
| 177 | * @param string $ownerAttribute |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function addOwner(int $ownerId, string $owner, string $ownerAttribute): bool |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return \yii\db\ActiveQuery |
||
| 188 | */ |
||
| 189 | public function getOwners() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get preview html. |
||
| 196 | * |
||
| 197 | * @param string $baseUrl Url to asset files which is formed on the BaseAsset. |
||
| 198 | * @param string $location Where is this function calling: |
||
| 199 | * existing - if view template renders files, which are exist; |
||
| 200 | * fileinfo - for "fileinfo/index" view template of "filemanager"; |
||
| 201 | * fileitem - for "_fileItem" view template of "filemanager". |
||
| 202 | * You can set new location attributes by custom in Module "previewOptions" |
||
| 203 | * before using in this getPreview() function inside the view template. |
||
| 204 | * It is necessary to set location attributes for certain types of mediafiles. |
||
| 205 | * See how it's done in "preview-options" config file as an example. |
||
| 206 | * @param array $options Options for the next tags, which must be the keys of the options array: |
||
| 207 | * 1. Main tag: |
||
| 208 | * mainTag - main preview html tag. Can contain different html tag options. |
||
| 209 | * And also can contain "alias" from the number of module constant aliases: |
||
| 210 | * default, original, small, medium, large. |
||
| 211 | * 2. Addition tags: |
||
| 212 | * leftTag - tag that is located to the left of the "mainTag"; |
||
| 213 | * rightTag - tag that is located to the right of the "mainTag"; |
||
| 214 | * externalTag - tag in which the mainTag with leftTag and rightTag are embedded. |
||
| 215 | * Importantly! Addition tag keys must contain the next attributes: |
||
| 216 | * name - the name of addition tag. |
||
| 217 | * options - html options of addition tag. |
||
| 218 | * By default, the mainTag is already in the "preview-options" configuration file. |
||
| 219 | * You can insert configurations values of addition tags through the third parameter of this function. |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getPreview(string $baseUrl = '', string $location = null, array $options = []): string |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get image preview. |
||
| 269 | * |
||
| 270 | * @param array $mainTagOptions |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getImagePreview(array $mainTagOptions): string |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get audio preview. |
||
| 289 | * |
||
| 290 | * @param array $mainTagOptions |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | View Code Duplication | public function getAudioPreview(array $mainTagOptions): string |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Get video preview. |
||
| 306 | * |
||
| 307 | * @param array $mainTagOptions |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | View Code Duplication | public function getVideoPreview(array $mainTagOptions): string |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get application preview. |
||
| 323 | * |
||
| 324 | * @param string $baseUrl |
||
| 325 | * @param array $mainTagOptions |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getAppPreview(string $baseUrl = '', array $mainTagOptions): string |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get text preview. |
||
| 336 | * |
||
| 337 | * @param string $baseUrl |
||
| 338 | * @param array $mainTagOptions |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getTextPreview(string $baseUrl = '', array $mainTagOptions): string |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get other preview. |
||
| 349 | * |
||
| 350 | * @param string $baseUrl |
||
| 351 | * @param array $mainTagOptions |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getOtherPreview(string $baseUrl = '', array $mainTagOptions): string |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get application preview url. |
||
| 362 | * |
||
| 363 | * @param string $baseUrl |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getAppPreviewUrl($baseUrl = ''): string |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get text preview url. |
||
| 394 | * |
||
| 395 | * @param string $baseUrl |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | View Code Duplication | public function getTextPreviewUrl($baseUrl = ''): string |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Get other preview url. |
||
| 412 | * |
||
| 413 | * @param string $baseUrl |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function getOtherPreviewUrl($baseUrl = ''): string |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Get thumbnails. |
||
| 431 | * |
||
| 432 | * @return array |
||
| 433 | */ |
||
| 434 | public function getThumbs(): array |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get thumb url. |
||
| 441 | * |
||
| 442 | * @param string $alias |
||
| 443 | * |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function getThumbUrl(string $alias): string |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Get thumb image. |
||
| 466 | * |
||
| 467 | * @param string $alias |
||
| 468 | * @param array $options |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getThumbImage(string $alias, array $options = []): string |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Get default thumbnail url. |
||
| 489 | * |
||
| 490 | * @param string $baseUrl |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public function getDefaultThumbUrl($baseUrl = ''): string |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return string file size |
||
| 512 | */ |
||
| 513 | public function getFileSize() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Check if the file is image. |
||
| 521 | * |
||
| 522 | * @return bool |
||
| 523 | */ |
||
| 524 | public function isImage(): bool |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Check if the file is audio. |
||
| 531 | * |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | public function isAudio(): bool |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Check if the file is video. |
||
| 541 | * |
||
| 542 | * @return bool |
||
| 543 | */ |
||
| 544 | public function isVideo(): bool |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Check if the file is text. |
||
| 551 | * |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | public function isText(): bool |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Check if the file is application. |
||
| 561 | * |
||
| 562 | * @return bool |
||
| 563 | */ |
||
| 564 | public function isApp(): bool |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Check if the file is excel. |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | */ |
||
| 574 | public function isExcel(): bool |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Check if the file is pdf. |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | public function isPdf(): bool |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Check if the file is word. |
||
| 591 | * |
||
| 592 | * @return bool |
||
| 593 | */ |
||
| 594 | public function isWord(): bool |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param string $url |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | public function getFullPublicUrl(string $url): string |
||
| 608 | |||
| 609 | /** |
||
| 610 | * If storage is local, public base url will be linked with a url from DB. |
||
| 611 | * It is useful for html templates. |
||
| 612 | * |
||
| 613 | * @param string|null $url |
||
| 614 | * |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | public function getViewUrl(string $url = null): string |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Get preview option by location and file type. |
||
| 626 | * |
||
| 627 | * @param array $options |
||
| 628 | * @param string|null $location |
||
| 629 | * @param Module $module |
||
| 630 | * @param string $fileType |
||
| 631 | * |
||
| 632 | * @return array |
||
| 633 | */ |
||
| 634 | private function getOptions( |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Compact addition tag with the main preview. |
||
| 646 | * |
||
| 647 | * @param string $preview |
||
| 648 | * @param string $additionTagType |
||
| 649 | * @param array $additionTagConfig |
||
| 650 | * |
||
| 651 | * @throws InvalidConfigException |
||
| 652 | * |
||
| 653 | * @return string |
||
| 654 | */ |
||
| 655 | private function compactPreviewAdditionTags( |
||
| 690 | } |
||
| 691 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.