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 Attachment 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 Attachment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class Attachment extends Content implements AttachableInterface |
||
44 | { |
||
45 | /** |
||
46 | * Default attachment types |
||
47 | */ |
||
48 | const FILE_TYPE = File::class; |
||
49 | const LINK_TYPE = Link::class; |
||
50 | const IMAGE_TYPE = Image::class; |
||
51 | const EMBED_TYPE = Embed::class; |
||
52 | const VIDEO_TYPE = Video::class; |
||
53 | const TEXT_TYPE = Text::class; |
||
54 | const GALLERY_TYPE = Gallery::class; |
||
55 | const ACCORDION_TYPE = Accordion::class; |
||
56 | const CONTAINER_TYPE = AttachmentContainer::class; |
||
57 | |||
58 | /** |
||
59 | * The attachment type. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $type; |
||
64 | |||
65 | /** |
||
66 | * The attachment heading template. |
||
67 | * |
||
68 | * @var Translation|string|null |
||
69 | */ |
||
70 | protected $heading; |
||
71 | |||
72 | /** |
||
73 | * The attachment preview template. |
||
74 | * |
||
75 | * @var Translation|string|null |
||
76 | */ |
||
77 | protected $preview; |
||
78 | |||
79 | /** |
||
80 | * Whether to show the title on the front-end. |
||
81 | * |
||
82 | * @var boolean |
||
83 | */ |
||
84 | protected $showTitle = true; |
||
85 | |||
86 | /** |
||
87 | * @var string|string[] |
||
88 | */ |
||
89 | protected $categories; |
||
90 | |||
91 | /** |
||
92 | * Generic information about the attachment. |
||
93 | * |
||
94 | * @var Translation|string|null $title The title of the attachment. |
||
95 | * @var Translation|string|null $subtitle The subtitle of the attachment. |
||
96 | * @var Translation|string|null $description The content of the attachment. |
||
97 | * @var Translation|string|null $keywords Keywords finding the attachment. |
||
98 | */ |
||
99 | protected $title; |
||
100 | protected $subtitle; |
||
101 | protected $description; |
||
102 | protected $keywords; |
||
103 | |||
104 | /** |
||
105 | * File related attachments. |
||
106 | * |
||
107 | * @var string $file The path of an attached file. |
||
108 | * @var string $fileLabel The label for the attached file. |
||
109 | * @var integer $fileSize The size of the attached file in bytes. |
||
110 | * @var string $fileType The content type of the attached file. |
||
111 | */ |
||
112 | protected $file; |
||
113 | protected $fileLabel; |
||
114 | protected $fileSize; |
||
115 | protected $fileType; |
||
116 | |||
117 | /** |
||
118 | * Link related attachments. |
||
119 | * |
||
120 | * @var string $link The URL related to the attachment. |
||
121 | * @var string $linkLabel The label for the attached link. |
||
122 | */ |
||
123 | protected $link; |
||
124 | protected $linkLabel; |
||
125 | |||
126 | /** |
||
127 | * Path to a thumbnail of the attached file. |
||
128 | * |
||
129 | * Auto-generated thumbnail if the attached file is an image. |
||
130 | * |
||
131 | * @var Translation|string|null |
||
132 | */ |
||
133 | protected $thumbnail; |
||
134 | |||
135 | /** |
||
136 | * Embedded content. |
||
137 | * |
||
138 | * @var Translation|string|null |
||
139 | */ |
||
140 | protected $embed; |
||
141 | |||
142 | /** |
||
143 | * The attachment's position amongst other attachments. |
||
144 | * |
||
145 | * @var integer |
||
146 | */ |
||
147 | protected $position; |
||
148 | |||
149 | /** |
||
150 | * The base URI. |
||
151 | * |
||
152 | * @var UriInterface|null |
||
153 | */ |
||
154 | private $baseUrl; |
||
155 | |||
156 | /** |
||
157 | * Whether the attachment acts like a presenter (TRUE) or data model (FALSE). |
||
158 | * |
||
159 | * @var boolean |
||
160 | */ |
||
161 | private $presentable = false; |
||
162 | |||
163 | /** |
||
164 | * The attachment's parent container instance. |
||
165 | * |
||
166 | * @var AttachmentContainerInterface|null |
||
167 | */ |
||
168 | protected $containerObj; |
||
169 | |||
170 | /** |
||
171 | * A store of resolved attachment types. |
||
172 | * |
||
173 | * @var array |
||
174 | */ |
||
175 | protected static $resolvedType = []; |
||
176 | |||
177 | /** |
||
178 | * Store the collection loader for the current class. |
||
179 | * |
||
180 | * @var CollectionLoader |
||
181 | */ |
||
182 | private $collectionLoader; |
||
183 | |||
184 | /** |
||
185 | * @var ModelInterface $presenter |
||
186 | */ |
||
187 | private $presenter; |
||
188 | |||
189 | /** |
||
190 | * Return a new section object. |
||
191 | * |
||
192 | * @param array $data Dependencies. |
||
193 | */ |
||
194 | public function __construct(array $data = null) |
||
205 | |||
206 | /** |
||
207 | * Inject dependencies from a DI Container. |
||
208 | * |
||
209 | * @param Container $container A dependencies container instance. |
||
210 | * @return void |
||
211 | */ |
||
212 | protected function setDependencies(Container $container) |
||
219 | |||
220 | /** |
||
221 | * Determine if the model is for presentation or editing. |
||
222 | * |
||
223 | * @param boolean $presenter The presenter flag. |
||
224 | * @return boolean Returns TRUE if model is used for presentation; FALSE for editing. |
||
225 | */ |
||
226 | public function isPresentable($presenter = null) |
||
234 | |||
235 | /** |
||
236 | * Retrieve the attachment's container ID (if any). |
||
237 | * |
||
238 | * Useful when templating a container of nested attachments. |
||
239 | * |
||
240 | * @return mixed|null |
||
241 | */ |
||
242 | public function containerId() |
||
251 | |||
252 | /** |
||
253 | * Determine if the attachment belongs to a container. |
||
254 | * |
||
255 | * @return boolean |
||
256 | */ |
||
257 | public function hasContainerObj() |
||
261 | |||
262 | /** |
||
263 | * Retrieve the attachment's container instance. |
||
264 | * |
||
265 | * @return AttachmentContainerInterface|null |
||
266 | */ |
||
267 | public function containerObj() |
||
271 | |||
272 | /** |
||
273 | * Set the attachment's container instance. |
||
274 | * |
||
275 | * @param AttachmentContainerInterface|null $obj The container object or NULL. |
||
276 | * @throws InvalidArgumentException If the given object is invalid. |
||
277 | * @return Attachment |
||
278 | */ |
||
279 | public function setContainerObj($obj) |
||
306 | |||
307 | /** |
||
308 | * Retrieve the attachment type. |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | public function type() |
||
320 | |||
321 | /** |
||
322 | * Set the attachment type. |
||
323 | * |
||
324 | * @param string $type The attachment type. |
||
325 | * @throws InvalidArgumentException If provided argument is not of type 'string'. |
||
326 | * @return string |
||
327 | */ |
||
328 | public function setType($type) |
||
338 | |||
339 | /** |
||
340 | * Retrieve the label of the attachment type. |
||
341 | * |
||
342 | * @return string Returns the translated attachment type or the short name. |
||
343 | */ |
||
344 | public function typeLabel() |
||
355 | |||
356 | /** |
||
357 | * Retrieve the unqualified class name. |
||
358 | * |
||
359 | * @return string Returns the short name of the model's class, the part without the namespace. |
||
360 | */ |
||
361 | public function microType() |
||
373 | |||
374 | /** |
||
375 | * Retrieve the image attachment type. |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | public function imageType() |
||
383 | |||
384 | /** |
||
385 | * Retrieve the attachment's heading template. |
||
386 | * |
||
387 | * @return Translation|string|null |
||
388 | */ |
||
389 | public function heading() |
||
402 | |||
403 | /** |
||
404 | * Retrieve the attachment's heading as a raw value. |
||
405 | * |
||
406 | * @return Translation|string|null |
||
407 | */ |
||
408 | public function rawHeading() |
||
412 | |||
413 | /** |
||
414 | * Set the attachment's heading template. |
||
415 | * |
||
416 | * @param string $template The attachment heading. |
||
417 | * @return Attachment Chainable |
||
418 | */ |
||
419 | public function setHeading($template) |
||
425 | |||
426 | /** |
||
427 | * Retrieve the attachment's preview template. |
||
428 | * |
||
429 | * @return Translation|string|null |
||
430 | */ |
||
431 | public function preview() |
||
439 | |||
440 | /** |
||
441 | * Retrieve the attachment's preview as a raw value. |
||
442 | * |
||
443 | * @return Translation|string|null |
||
444 | */ |
||
445 | public function rawPreview() |
||
449 | |||
450 | /** |
||
451 | * Set the attachment's preview template. |
||
452 | * |
||
453 | * @param string $template The attachment preview. |
||
454 | * @return Attachment Chainable |
||
455 | */ |
||
456 | public function setPreview($template) |
||
462 | |||
463 | /** |
||
464 | * Determine if the attachment type is an image. |
||
465 | * |
||
466 | * @return boolean |
||
467 | */ |
||
468 | public function isImage() |
||
472 | |||
473 | /** |
||
474 | * Determine if the attachment type is an embed object. |
||
475 | * |
||
476 | * @return boolean |
||
477 | */ |
||
478 | public function isEmbed() |
||
482 | |||
483 | /** |
||
484 | * Determine if the attachment type is a video. |
||
485 | * |
||
486 | * @return boolean |
||
487 | */ |
||
488 | public function isVideo() |
||
492 | |||
493 | /** |
||
494 | * Determine if the attachment type is a file attachment. |
||
495 | * |
||
496 | * @return boolean |
||
497 | */ |
||
498 | public function isFile() |
||
502 | |||
503 | /** |
||
504 | * Determine if the attachment type is a text-area. |
||
505 | * |
||
506 | * @return boolean |
||
507 | */ |
||
508 | public function isText() |
||
512 | |||
513 | /** |
||
514 | * Determine if the attachment type is an image gallery. |
||
515 | * |
||
516 | * @return boolean |
||
517 | */ |
||
518 | public function isGallery() |
||
522 | |||
523 | /** |
||
524 | * Determine if the attachment type is an accordion. |
||
525 | * |
||
526 | * @return boolean |
||
527 | */ |
||
528 | public function isAccordion() |
||
532 | |||
533 | /** |
||
534 | * Determine if the attachment type is a link. |
||
535 | * |
||
536 | * @return boolean |
||
537 | */ |
||
538 | public function isLink() |
||
542 | |||
543 | /** |
||
544 | * Determine if this attachment is a container. |
||
545 | * |
||
546 | * @return boolean |
||
547 | */ |
||
548 | public function isAttachmentContainer() |
||
552 | |||
553 | // Setters |
||
554 | // ============================================================================= |
||
555 | |||
556 | /** |
||
557 | * Show/hide the attachment's title on the front-end. |
||
558 | * |
||
559 | * @param boolean $show Show (TRUE) or hide (FALSE) the title. |
||
560 | * @return UiItemInterface Chainable |
||
561 | */ |
||
562 | public function setShowTitle($show) |
||
568 | |||
569 | /** |
||
570 | * Set the attachment's title. |
||
571 | * |
||
572 | * @param string $title The object title. |
||
573 | * @return self |
||
574 | */ |
||
575 | public function setTitle($title) |
||
581 | |||
582 | /** |
||
583 | * Set the attachment's sub-title. |
||
584 | * |
||
585 | * @param string $title The object title. |
||
586 | * @return self |
||
587 | */ |
||
588 | public function setSubtitle($title) |
||
594 | |||
595 | /** |
||
596 | * Set the attachment's description. |
||
597 | * |
||
598 | * @param string $description The description of the object. |
||
599 | * @return self |
||
600 | */ |
||
601 | public function setDescription($description) |
||
613 | |||
614 | /** |
||
615 | * Set the attachment's keywords. |
||
616 | * |
||
617 | * @param string|string[] $keywords One or more entries. |
||
618 | * @return self |
||
619 | */ |
||
620 | public function setKeywords($keywords) |
||
626 | |||
627 | /** |
||
628 | * Set the path to the thumbnail associated with the object. |
||
629 | * |
||
630 | * @param string $path A path to an image. |
||
631 | * @return self |
||
632 | */ |
||
633 | public function setThumbnail($path) |
||
639 | |||
640 | /** |
||
641 | * Set the path to the attached file. |
||
642 | * |
||
643 | * @param string $path A path to a file. |
||
644 | * @return self |
||
645 | */ |
||
646 | public function setFile($path) |
||
652 | |||
653 | /** |
||
654 | * Set the URL. |
||
655 | * |
||
656 | * @param string $link An external url. |
||
657 | * @return self |
||
658 | */ |
||
659 | public function setLink($link) |
||
665 | |||
666 | /** |
||
667 | * Set the file label. |
||
668 | * |
||
669 | * @param string $label A descriptor. |
||
670 | * @return self |
||
671 | */ |
||
672 | public function setFileLabel($label) |
||
678 | |||
679 | /** |
||
680 | * Set the link label. |
||
681 | * |
||
682 | * @param string $label A descriptor. |
||
683 | * @return self |
||
684 | */ |
||
685 | public function setLinkLabel($label) |
||
691 | |||
692 | /** |
||
693 | * Set the size of the attached file. |
||
694 | * |
||
695 | * @param integer|float $size A file size in bytes; the one of the attached. |
||
696 | * @throws InvalidArgumentException If provided argument is not of type 'integer' or 'float'. |
||
697 | * @return self |
||
698 | */ |
||
699 | View Code Duplication | public function setFileSize($size) |
|
715 | |||
716 | /** |
||
717 | * Set file extension. |
||
718 | * |
||
719 | * @param string $type File extension. |
||
720 | * @return self |
||
721 | */ |
||
722 | public function setFileType($type) |
||
728 | |||
729 | /** |
||
730 | * Set the embed content. |
||
731 | * |
||
732 | * @param string $embed A URI or an HTML media element. |
||
733 | * @throws InvalidArgumentException If provided argument is not of type 'string'. |
||
734 | * @return self |
||
735 | */ |
||
736 | public function setEmbed($embed) |
||
742 | |||
743 | /** |
||
744 | * @param string|\string[] $categories Category elements. |
||
745 | * @return self |
||
746 | */ |
||
747 | public function setCategories($categories) |
||
753 | |||
754 | // Getters |
||
755 | // ============================================================================= |
||
756 | |||
757 | /** |
||
758 | * Determine if the title is to be displayed on the front-end. |
||
759 | * |
||
760 | * @return boolean |
||
761 | */ |
||
762 | public function showTitle() |
||
770 | |||
771 | /** |
||
772 | * Retrieve the attachment's title. |
||
773 | * |
||
774 | * @return Translation|string|null |
||
775 | */ |
||
776 | public function title() |
||
780 | |||
781 | /** |
||
782 | * Retrieve the attachment's sub-title. |
||
783 | * |
||
784 | * @return Translation|string|null |
||
785 | */ |
||
786 | public function subtitle() |
||
790 | |||
791 | /** |
||
792 | * Retrieve attachment's description. |
||
793 | * |
||
794 | * @return Translation|string|null |
||
795 | */ |
||
796 | public function description() |
||
800 | |||
801 | /** |
||
802 | * Retrieve the attachment's keywords. |
||
803 | * |
||
804 | * @return string[] |
||
805 | */ |
||
806 | public function keywords() |
||
810 | |||
811 | /** |
||
812 | * Retrieve the path to the thumbnail associated with the object. |
||
813 | * |
||
814 | * @return string|null |
||
815 | */ |
||
816 | public function thumbnail() |
||
820 | |||
821 | /** |
||
822 | * Retrieve the path to the attached file. |
||
823 | * |
||
824 | * @return Translation|string|null |
||
825 | */ |
||
826 | public function file() |
||
830 | |||
831 | /** |
||
832 | * Retrieve the attached link. |
||
833 | * |
||
834 | * @return Translation|string|null |
||
835 | */ |
||
836 | public function link() |
||
840 | |||
841 | /** |
||
842 | * Retrieve either the attached file or link. |
||
843 | * |
||
844 | * @return Translation|string|null |
||
845 | */ |
||
846 | public function fileOrLink() |
||
850 | |||
851 | /** |
||
852 | * Retrieve the attached file(s) and link(s). |
||
853 | * |
||
854 | * @return string[]|null |
||
855 | */ |
||
856 | public function fileAndLink() |
||
868 | |||
869 | /** |
||
870 | * Basename of the associated file. |
||
871 | * @return string Basename of file. |
||
872 | */ |
||
873 | public function basename() |
||
881 | |||
882 | /** |
||
883 | * Retrieve the file label. |
||
884 | * |
||
885 | * @return string|null |
||
886 | */ |
||
887 | public function fileLabel() |
||
891 | |||
892 | /** |
||
893 | * Retrieve the link label. |
||
894 | * |
||
895 | * @return string|null |
||
896 | */ |
||
897 | public function linkLabel() |
||
901 | |||
902 | /** |
||
903 | * Retrieve the attached file's size. |
||
904 | * |
||
905 | * @return integer Returns the size of the file in bytes, or FALSE in case of an error. |
||
906 | */ |
||
907 | public function fileSize() |
||
911 | |||
912 | /** |
||
913 | * File type / extension |
||
914 | * @return string File extension. |
||
915 | */ |
||
916 | public function fileType() |
||
920 | |||
921 | /** |
||
922 | * Retrieve the embed content. |
||
923 | * |
||
924 | * @return string |
||
925 | */ |
||
926 | public function embed() |
||
930 | |||
931 | /** |
||
932 | * @return string|\string[] |
||
933 | */ |
||
934 | public function categories() |
||
938 | |||
939 | /** |
||
940 | * @return ModelInterface|mixed |
||
941 | */ |
||
942 | public function presenter() |
||
946 | |||
947 | /** |
||
948 | * @param ModelInterface|mixed $presenter Presenter for Attachment. |
||
949 | * @return self |
||
950 | */ |
||
951 | public function setPresenter($presenter) |
||
957 | |||
958 | // Events |
||
959 | // ============================================================================= |
||
960 | |||
961 | /** |
||
962 | * Event called before _deleting_ the attachment. |
||
963 | * |
||
964 | * @see Charcoal\Source\StorableTrait::preDelete() For the "create" Event. |
||
965 | * @see Charcoal\Attachment\Traits\AttachmentAwareTrait::removeJoins |
||
966 | * @return boolean |
||
967 | */ |
||
968 | public function preDelete() |
||
982 | |||
983 | // Utilities |
||
984 | // ============================================================================= |
||
985 | |||
986 | /** |
||
987 | * Set the base URI of the project. |
||
988 | * |
||
989 | * @see \Charcoal\Admin\Support\setBaseUrl::baseUrl() |
||
990 | * @param UriInterface $uri The base URI. |
||
991 | * @return self |
||
992 | */ |
||
993 | protected function setBaseUrl(UriInterface $uri) |
||
999 | |||
1000 | /** |
||
1001 | * Retrieve the base URI of the project. |
||
1002 | * |
||
1003 | * @throws RuntimeException If the base URI is missing. |
||
1004 | * @return UriInterface|null |
||
1005 | */ |
||
1006 | public function baseUrl() |
||
1017 | |||
1018 | /** |
||
1019 | * Prepend the base URI to the given path. |
||
1020 | * |
||
1021 | * @param string $uri A URI path to wrap. |
||
1022 | * @return UriInterface|null |
||
1023 | */ |
||
1024 | public function createAbsoluteUrl($uri) |
||
1042 | |||
1043 | /** |
||
1044 | * Prepend the base URI to the given path. |
||
1045 | * |
||
1046 | * @param string $text A string to parse relative URIs. |
||
1047 | * @return UriInterface|null |
||
1048 | */ |
||
1049 | protected function resolveUrls($text) |
||
1074 | |||
1075 | /** |
||
1076 | * Determine if the given URI is relative. |
||
1077 | * |
||
1078 | * @see \Charcoal\Admin\Support\BaseUrlTrait::isRelativeUri() |
||
1079 | * @param string $uri A URI path to test. |
||
1080 | * @return boolean |
||
1081 | */ |
||
1082 | protected function isRelativeUri($uri) |
||
1092 | |||
1093 | /** |
||
1094 | * Set a model collection loader. |
||
1095 | * |
||
1096 | * @param CollectionLoader $loader The collection loader. |
||
1097 | * @return self |
||
1098 | */ |
||
1099 | protected function setCollectionLoader(CollectionLoader $loader) |
||
1105 | |||
1106 | /** |
||
1107 | * Retrieve the model collection loader. |
||
1108 | * |
||
1109 | * @throws Exception If the collection loader was not previously set. |
||
1110 | * @return CollectionLoader |
||
1111 | */ |
||
1112 | public function collectionLoader() |
||
1123 | } |
||
1124 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: