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 | * Return a new section object. |
||
186 | * |
||
187 | * @param array $data Dependencies. |
||
188 | */ |
||
189 | public function __construct(array $data = null) |
||
200 | |||
201 | /** |
||
202 | * Inject dependencies from a DI Container. |
||
203 | * |
||
204 | * @param Container $container A dependencies container instance. |
||
205 | * @return void |
||
206 | */ |
||
207 | protected function setDependencies(Container $container) |
||
214 | |||
215 | /** |
||
216 | * Determine if the model is for presentation or editing. |
||
217 | * |
||
218 | * @param boolean $presenter The presenter flag. |
||
219 | * @return boolean Returns TRUE if model is used for presentation; FALSE for editing. |
||
220 | */ |
||
221 | public function isPresentable($presenter = null) |
||
229 | |||
230 | /** |
||
231 | * Retrieve the attachment's container ID (if any). |
||
232 | * |
||
233 | * Useful when templating a container of nested attachments. |
||
234 | * |
||
235 | * @return mixed|null |
||
236 | */ |
||
237 | public function containerId() |
||
246 | |||
247 | /** |
||
248 | * Determine if the attachment belongs to a container. |
||
249 | * |
||
250 | * @return boolean |
||
251 | */ |
||
252 | public function hasContainerObj() |
||
256 | |||
257 | /** |
||
258 | * Retrieve the attachment's container instance. |
||
259 | * |
||
260 | * @return AttachmentContainerInterface|null |
||
261 | */ |
||
262 | public function containerObj() |
||
266 | |||
267 | /** |
||
268 | * Set the attachment's container instance. |
||
269 | * |
||
270 | * @param AttachmentContainerInterface|null $obj The container object or NULL. |
||
271 | * @throws InvalidArgumentException If the given object is invalid. |
||
272 | * @return Attachment |
||
273 | */ |
||
274 | public function setContainerObj($obj) |
||
301 | |||
302 | /** |
||
303 | * Retrieve the attachment type. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function type() |
||
315 | |||
316 | /** |
||
317 | * Set the attachment type. |
||
318 | * |
||
319 | * @param string $type The attachment type. |
||
320 | * @throws InvalidArgumentException If provided argument is not of type 'string'. |
||
321 | * @return string |
||
322 | */ |
||
323 | public function setType($type) |
||
333 | |||
334 | /** |
||
335 | * Retrieve the unqualified class name. |
||
336 | * |
||
337 | * @return string Returns the short name of the model's class, the part without the namespace. |
||
338 | */ |
||
339 | public function microType() |
||
351 | |||
352 | /** |
||
353 | * Retrieve the image attachment type. |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | public function imageType() |
||
361 | |||
362 | /** |
||
363 | * Retrieve the attachment's heading template. |
||
364 | * |
||
365 | * @return Translation|string|null |
||
366 | */ |
||
367 | public function heading() |
||
380 | |||
381 | /** |
||
382 | * Retrieve the attachment's heading as a raw value. |
||
383 | * |
||
384 | * @return Translation|string|null |
||
385 | */ |
||
386 | public function rawHeading() |
||
390 | |||
391 | /** |
||
392 | * Set the attachment's heading template. |
||
393 | * |
||
394 | * @param string $template The attachment heading. |
||
395 | * @return Attachment Chainable |
||
396 | */ |
||
397 | public function setHeading($template) |
||
403 | |||
404 | /** |
||
405 | * Retrieve the attachment's preview template. |
||
406 | * |
||
407 | * @return Translation|string|null |
||
408 | */ |
||
409 | public function preview() |
||
417 | |||
418 | /** |
||
419 | * Retrieve the attachment's preview as a raw value. |
||
420 | * |
||
421 | * @return Translation|string|null |
||
422 | */ |
||
423 | public function rawPreview() |
||
427 | |||
428 | /** |
||
429 | * Set the attachment's preview template. |
||
430 | * |
||
431 | * @param string $template The attachment preview. |
||
432 | * @return Attachment Chainable |
||
433 | */ |
||
434 | public function setPreview($template) |
||
440 | |||
441 | /** |
||
442 | * Determine if the attachment type is an image. |
||
443 | * |
||
444 | * @return boolean |
||
445 | */ |
||
446 | public function isImage() |
||
450 | |||
451 | /** |
||
452 | * Determine if the attachment type is an embed object. |
||
453 | * |
||
454 | * @return boolean |
||
455 | */ |
||
456 | public function isEmbed() |
||
460 | |||
461 | /** |
||
462 | * Determine if the attachment type is a video. |
||
463 | * |
||
464 | * @return boolean |
||
465 | */ |
||
466 | public function isVideo() |
||
470 | |||
471 | /** |
||
472 | * Determine if the attachment type is a file attachment. |
||
473 | * |
||
474 | * @return boolean |
||
475 | */ |
||
476 | public function isFile() |
||
480 | |||
481 | /** |
||
482 | * Determine if the attachment type is a text-area. |
||
483 | * |
||
484 | * @return boolean |
||
485 | */ |
||
486 | public function isText() |
||
490 | |||
491 | /** |
||
492 | * Determine if the attachment type is an image gallery. |
||
493 | * |
||
494 | * @return boolean |
||
495 | */ |
||
496 | public function isGallery() |
||
500 | |||
501 | /** |
||
502 | * Determine if the attachment type is an accordion. |
||
503 | * |
||
504 | * @return boolean |
||
505 | */ |
||
506 | public function isAccordion() |
||
510 | |||
511 | /** |
||
512 | * Determine if the attachment type is a link. |
||
513 | * |
||
514 | * @return boolean |
||
515 | */ |
||
516 | public function isLink() |
||
520 | |||
521 | /** |
||
522 | * Determine if this attachment is a container. |
||
523 | * |
||
524 | * @return boolean |
||
525 | */ |
||
526 | public function isAttachmentContainer() |
||
530 | |||
531 | // Setters |
||
532 | // ============================================================================= |
||
533 | |||
534 | /** |
||
535 | * Show/hide the attachment's title on the front-end. |
||
536 | * |
||
537 | * @param boolean $show Show (TRUE) or hide (FALSE) the title. |
||
538 | * @return UiItemInterface Chainable |
||
539 | */ |
||
540 | public function setShowTitle($show) |
||
546 | |||
547 | /** |
||
548 | * Set the attachment's title. |
||
549 | * |
||
550 | * @param string $title The object title. |
||
551 | * @return self |
||
552 | */ |
||
553 | public function setTitle($title) |
||
559 | |||
560 | /** |
||
561 | * Set the attachment's sub-title. |
||
562 | * |
||
563 | * @param string $title The object title. |
||
564 | * @return self |
||
565 | */ |
||
566 | public function setSubtitle($title) |
||
572 | |||
573 | /** |
||
574 | * Set the attachment's description. |
||
575 | * |
||
576 | * @param string $description The description of the object. |
||
577 | * @return self |
||
578 | */ |
||
579 | public function setDescription($description) |
||
591 | |||
592 | /** |
||
593 | * Set the attachment's keywords. |
||
594 | * |
||
595 | * @param string|string[] $keywords One or more entries. |
||
596 | * @return self |
||
597 | */ |
||
598 | public function setKeywords($keywords) |
||
604 | |||
605 | /** |
||
606 | * Set the path to the thumbnail associated with the object. |
||
607 | * |
||
608 | * @param string $path A path to an image. |
||
609 | * @return self |
||
610 | */ |
||
611 | public function setThumbnail($path) |
||
617 | |||
618 | /** |
||
619 | * Set the path to the attached file. |
||
620 | * |
||
621 | * @param string $path A path to a file. |
||
622 | * @return self |
||
623 | */ |
||
624 | public function setFile($path) |
||
630 | |||
631 | /** |
||
632 | * Set the URL. |
||
633 | * |
||
634 | * @param string $link An external url. |
||
635 | * @return self |
||
636 | */ |
||
637 | public function setLink($link) |
||
643 | |||
644 | /** |
||
645 | * Set the file label. |
||
646 | * |
||
647 | * @param string $label A descriptor. |
||
648 | * @return self |
||
649 | */ |
||
650 | public function setFileLabel($label) |
||
656 | |||
657 | /** |
||
658 | * Set the link label. |
||
659 | * |
||
660 | * @param string $label A descriptor. |
||
661 | * @return self |
||
662 | */ |
||
663 | public function setLinkLabel($label) |
||
669 | |||
670 | /** |
||
671 | * Set the size of the attached file. |
||
672 | * |
||
673 | * @param integer|float $size A file size in bytes; the one of the attached. |
||
674 | * @throws InvalidArgumentException If provided argument is not of type 'integer' or 'float'. |
||
675 | * @return self |
||
676 | */ |
||
677 | View Code Duplication | public function setFileSize($size) |
|
693 | |||
694 | /** |
||
695 | * Set file extension. |
||
696 | * |
||
697 | * @param string $type File extension. |
||
698 | * @return self |
||
699 | */ |
||
700 | public function setFileType($type) |
||
706 | |||
707 | /** |
||
708 | * Set the embed content. |
||
709 | * |
||
710 | * @param string $embed A URI or an HTML media element. |
||
711 | * @throws InvalidArgumentException If provided argument is not of type 'string'. |
||
712 | * @return self |
||
713 | */ |
||
714 | public function setEmbed($embed) |
||
720 | |||
721 | /** |
||
722 | * @param string|\string[] $categories Category elements. |
||
723 | * @return self |
||
724 | */ |
||
725 | public function setCategories($categories) |
||
731 | |||
732 | // Getters |
||
733 | // ============================================================================= |
||
734 | |||
735 | /** |
||
736 | * Determine if the title is to be displayed on the front-end. |
||
737 | * |
||
738 | * @return boolean |
||
739 | */ |
||
740 | public function showTitle() |
||
748 | |||
749 | /** |
||
750 | * Retrieve the attachment's title. |
||
751 | * |
||
752 | * @return Translation|string|null |
||
753 | */ |
||
754 | public function title() |
||
758 | |||
759 | /** |
||
760 | * Retrieve the attachment's sub-title. |
||
761 | * |
||
762 | * @return Translation|string|null |
||
763 | */ |
||
764 | public function subtitle() |
||
768 | |||
769 | /** |
||
770 | * Retrieve attachment's description. |
||
771 | * |
||
772 | * @return Translation|string|null |
||
773 | */ |
||
774 | public function description() |
||
778 | |||
779 | /** |
||
780 | * Retrieve the attachment's keywords. |
||
781 | * |
||
782 | * @return string[] |
||
783 | */ |
||
784 | public function keywords() |
||
788 | |||
789 | /** |
||
790 | * Retrieve the path to the thumbnail associated with the object. |
||
791 | * |
||
792 | * @return string|null |
||
793 | */ |
||
794 | public function thumbnail() |
||
798 | |||
799 | /** |
||
800 | * Retrieve the path to the attached file. |
||
801 | * |
||
802 | * @return Translation|string|null |
||
803 | */ |
||
804 | public function file() |
||
808 | |||
809 | /** |
||
810 | * Retrieve the attached link. |
||
811 | * |
||
812 | * @return Translation|string|null |
||
813 | */ |
||
814 | public function link() |
||
818 | |||
819 | /** |
||
820 | * Basename of the associated file. |
||
821 | * @return string Basename of file. |
||
822 | */ |
||
823 | public function basename() |
||
831 | |||
832 | /** |
||
833 | * Retrieve the file label. |
||
834 | * |
||
835 | * @return string|null |
||
836 | */ |
||
837 | public function fileLabel() |
||
841 | |||
842 | /** |
||
843 | * Retrieve the link label. |
||
844 | * |
||
845 | * @return string|null |
||
846 | */ |
||
847 | public function linkLabel() |
||
851 | |||
852 | /** |
||
853 | * Retrieve the attached file's size. |
||
854 | * |
||
855 | * @return integer Returns the size of the file in bytes, or FALSE in case of an error. |
||
856 | */ |
||
857 | public function fileSize() |
||
861 | |||
862 | /** |
||
863 | * File type / extension |
||
864 | * @return string File extension. |
||
865 | */ |
||
866 | public function fileType() |
||
870 | |||
871 | /** |
||
872 | * Retrieve the embed content. |
||
873 | * |
||
874 | * @return string |
||
875 | */ |
||
876 | public function embed() |
||
880 | |||
881 | /** |
||
882 | * @return string|\string[] |
||
883 | */ |
||
884 | public function categories() |
||
888 | |||
889 | // Events |
||
890 | // ============================================================================= |
||
891 | |||
892 | /** |
||
893 | * Event called before _deleting_ the attachment. |
||
894 | * |
||
895 | * @see Charcoal\Source\StorableTrait::preDelete() For the "create" Event. |
||
896 | * @see Charcoal\Attachment\Traits\AttachmentAwareTrait::removeJoins |
||
897 | * @return boolean |
||
898 | */ |
||
899 | public function preDelete() |
||
914 | |||
915 | // Utilities |
||
916 | // ============================================================================= |
||
917 | |||
918 | /** |
||
919 | * Set the base URI of the project. |
||
920 | * |
||
921 | * @see \Charcoal\Admin\Support\setBaseUrl::baseUrl() |
||
922 | * @param UriInterface $uri The base URI. |
||
923 | * @return self |
||
924 | */ |
||
925 | protected function setBaseUrl(UriInterface $uri) |
||
931 | |||
932 | /** |
||
933 | * Retrieve the base URI of the project. |
||
934 | * |
||
935 | * @throws RuntimeException If the base URI is missing. |
||
936 | * @return UriInterface|null |
||
937 | */ |
||
938 | public function baseUrl() |
||
949 | |||
950 | /** |
||
951 | * Prepend the base URI to the given path. |
||
952 | * |
||
953 | * @param string $uri A URI path to wrap. |
||
954 | * @return UriInterface|null |
||
955 | */ |
||
956 | protected function createAbsoluteUrl($uri) |
||
974 | |||
975 | /** |
||
976 | * Prepend the base URI to the given path. |
||
977 | * |
||
978 | * @param string $text A string to parse relative URIs. |
||
979 | * @return UriInterface|null |
||
980 | */ |
||
981 | protected function resolveUrls($text) |
||
1006 | |||
1007 | /** |
||
1008 | * Determine if the given URI is relative. |
||
1009 | * |
||
1010 | * @see \Charcoal\Admin\Support\BaseUrlTrait::isRelativeUri() |
||
1011 | * @param string $uri A URI path to test. |
||
1012 | * @return boolean |
||
1013 | */ |
||
1014 | protected function isRelativeUri($uri) |
||
1024 | |||
1025 | /** |
||
1026 | * Set a model collection loader. |
||
1027 | * |
||
1028 | * @param CollectionLoader $loader The collection loader. |
||
1029 | * @return self |
||
1030 | */ |
||
1031 | protected function setCollectionLoader(CollectionLoader $loader) |
||
1037 | |||
1038 | /** |
||
1039 | * Retrieve the model collection loader. |
||
1040 | * |
||
1041 | * @throws Exception If the collection loader was not previously set. |
||
1042 | * @return CollectionLoader |
||
1043 | */ |
||
1044 | public function collectionLoader() |
||
1055 | } |
||
1056 |
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: