Complex classes like AbstractNode 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 AbstractNode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | abstract class AbstractNode implements NodeInterface |
||
31 | { |
||
32 | /** |
||
33 | * name max lenght. |
||
34 | */ |
||
35 | const MAX_NAME_LENGTH = 255; |
||
36 | |||
37 | /** |
||
38 | * Unique id. |
||
39 | * |
||
40 | * @var ObjectId |
||
41 | */ |
||
42 | protected $_id; |
||
43 | |||
44 | /** |
||
45 | * Node name. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $name = ''; |
||
50 | |||
51 | /** |
||
52 | * Owner. |
||
53 | * |
||
54 | * @var ObjectId |
||
55 | */ |
||
56 | protected $owner; |
||
57 | |||
58 | /** |
||
59 | * Mime. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $mime; |
||
64 | |||
65 | /** |
||
66 | * Meta attributes. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $meta = []; |
||
71 | |||
72 | /** |
||
73 | * Parent collection. |
||
74 | * |
||
75 | * @var ObjectId |
||
76 | */ |
||
77 | protected $parent; |
||
78 | |||
79 | /** |
||
80 | * Is file deleted. |
||
81 | * |
||
82 | * @var bool|UTCDateTime |
||
83 | */ |
||
84 | protected $deleted = false; |
||
85 | |||
86 | /** |
||
87 | * Is shared? |
||
88 | * |
||
89 | * @var bool |
||
90 | */ |
||
91 | protected $shared = false; |
||
92 | |||
93 | /** |
||
94 | * Destory at a certain time. |
||
95 | * |
||
96 | * @var UTCDateTime |
||
97 | */ |
||
98 | protected $destroy; |
||
99 | |||
100 | /** |
||
101 | * Changed timestamp. |
||
102 | * |
||
103 | * @var UTCDateTime |
||
104 | */ |
||
105 | protected $changed; |
||
106 | |||
107 | /** |
||
108 | * Created timestamp. |
||
109 | * |
||
110 | * @var UTCDateTime |
||
111 | */ |
||
112 | protected $created; |
||
113 | |||
114 | /** |
||
115 | * Point to antother node (Means this node is reference to $reference). |
||
116 | * |
||
117 | * @var ObjectId |
||
118 | */ |
||
119 | protected $reference; |
||
120 | |||
121 | /** |
||
122 | * Raw attributes before any processing or modifications. |
||
123 | * |
||
124 | * @var array |
||
125 | */ |
||
126 | protected $raw_attributes; |
||
127 | |||
128 | /** |
||
129 | * Readonly flag. |
||
130 | * |
||
131 | * @var bool |
||
132 | */ |
||
133 | protected $readonly = false; |
||
134 | |||
135 | /** |
||
136 | * App attributes. |
||
137 | * |
||
138 | * @var array |
||
139 | */ |
||
140 | protected $app = []; |
||
141 | |||
142 | /** |
||
143 | * Filesystem. |
||
144 | * |
||
145 | * @var Filesystem |
||
146 | */ |
||
147 | protected $_fs; |
||
148 | |||
149 | /** |
||
150 | * Database. |
||
151 | * |
||
152 | * @var Database |
||
153 | */ |
||
154 | protected $_db; |
||
155 | |||
156 | /** |
||
157 | * User. |
||
158 | * |
||
159 | * @var User |
||
160 | */ |
||
161 | protected $_user; |
||
162 | |||
163 | /** |
||
164 | * Logger. |
||
165 | * |
||
166 | * @var LoggerInterface |
||
167 | */ |
||
168 | protected $_logger; |
||
169 | |||
170 | /** |
||
171 | * Server. |
||
172 | * |
||
173 | * @var Server |
||
174 | */ |
||
175 | protected $_server; |
||
176 | |||
177 | /** |
||
178 | * Hook. |
||
179 | * |
||
180 | * @var Hook |
||
181 | */ |
||
182 | protected $_hook; |
||
183 | |||
184 | /** |
||
185 | * Acl. |
||
186 | * |
||
187 | * @var Acl |
||
188 | */ |
||
189 | protected $_acl; |
||
190 | |||
191 | /** |
||
192 | * Storage adapter. |
||
193 | * |
||
194 | * @var string |
||
195 | */ |
||
196 | protected $storage_adapter; |
||
197 | |||
198 | /** |
||
199 | * Acl. |
||
200 | * |
||
201 | * @var Acl |
||
202 | */ |
||
203 | protected $acl; |
||
204 | |||
205 | /** |
||
206 | * Convert to filename. |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function __toString() |
||
214 | |||
215 | /** |
||
216 | * Get owner. |
||
217 | * |
||
218 | * @return ObjectId |
||
219 | */ |
||
220 | public function getOwner(): ObjectId |
||
224 | |||
225 | /** |
||
226 | * Set filesystem. |
||
227 | * |
||
228 | * @param Filesystem $fs |
||
229 | * |
||
230 | * @return NodeInterface |
||
231 | */ |
||
232 | public function setFilesystem(Filesystem $fs): NodeInterface |
||
239 | |||
240 | /** |
||
241 | * Get filesystem. |
||
242 | * |
||
243 | * @return Filesystem |
||
244 | */ |
||
245 | public function getFilesystem(): Filesystem |
||
249 | |||
250 | /** |
||
251 | * Check if $node is a sub node of any parent nodes of this node. |
||
252 | * |
||
253 | * @param NodeInterface $node |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function isSubNode(NodeInterface $node): bool |
||
275 | |||
276 | /** |
||
277 | * Move node. |
||
278 | * |
||
279 | * @param Collection $parent |
||
280 | * @param int $conflict |
||
281 | * |
||
282 | * @return NodeInterface |
||
283 | */ |
||
284 | public function setParent(Collection $parent, int $conflict = NodeInterface::CONFLICT_NOACTION): NodeInterface |
||
362 | |||
363 | /** |
||
364 | * Get share id. |
||
365 | * |
||
366 | * @param bool $reference |
||
367 | * |
||
368 | * @return ObjectId |
||
369 | */ |
||
370 | public function getShareId(bool $reference = false): ?ObjectId |
||
390 | |||
391 | /** |
||
392 | * Get share node. |
||
393 | * |
||
394 | * @return Collection |
||
395 | */ |
||
396 | public function getShareNode(): ?Collection |
||
408 | |||
409 | /** |
||
410 | * Is node marked as readonly? |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | public function isReadonly(): bool |
||
418 | |||
419 | /** |
||
420 | * Request is from node owner? |
||
421 | * |
||
422 | * @return bool |
||
423 | */ |
||
424 | public function isOwnerRequest(): bool |
||
428 | |||
429 | /** |
||
430 | * Check if node is kind of special. |
||
431 | * |
||
432 | * @return bool |
||
433 | */ |
||
434 | public function isSpecial(): bool |
||
448 | |||
449 | /** |
||
450 | * Check if node is a sub node of a share. |
||
451 | * |
||
452 | * @return bool |
||
453 | */ |
||
454 | public function isShareMember(): bool |
||
458 | |||
459 | /** |
||
460 | * Is share. |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | public function isShare(): bool |
||
468 | |||
469 | /** |
||
470 | * Is share (Reference or master share). |
||
471 | * |
||
472 | * @return bool |
||
473 | */ |
||
474 | public function isShared(): bool |
||
482 | |||
483 | /** |
||
484 | * Set the name. |
||
485 | * |
||
486 | * @param string $name |
||
487 | * |
||
488 | * @return bool |
||
489 | */ |
||
490 | public function setName($name): bool |
||
510 | |||
511 | /** |
||
512 | * Check name. |
||
513 | * |
||
514 | * @param string $name |
||
515 | * |
||
516 | * @return string |
||
517 | */ |
||
518 | public function checkName(string $name): string |
||
533 | |||
534 | /** |
||
535 | * Get the name. |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | public function getName(): string |
||
543 | |||
544 | /** |
||
545 | * Undelete. |
||
546 | * |
||
547 | * @param int $conflict |
||
548 | * @param string $recursion |
||
549 | * @param bool $recursion_first |
||
550 | * |
||
551 | * @return bool |
||
552 | */ |
||
553 | public function undelete(int $conflict = NodeInterface::CONFLICT_NOACTION, ?string $recursion = null, bool $recursion_first = true): bool |
||
636 | |||
637 | /** |
||
638 | * Is node deleted? |
||
639 | * |
||
640 | * @return bool |
||
641 | */ |
||
642 | public function isDeleted(): bool |
||
646 | |||
647 | /** |
||
648 | * Get last modified timestamp. |
||
649 | * |
||
650 | * @return int |
||
651 | */ |
||
652 | public function getLastModified(): int |
||
660 | |||
661 | /** |
||
662 | * Get unique id. |
||
663 | * |
||
664 | * @return ObjectId |
||
665 | */ |
||
666 | 1 | public function getId(): ?ObjectId |
|
670 | |||
671 | /** |
||
672 | * Get parent. |
||
673 | * |
||
674 | * @return Collection |
||
675 | */ |
||
676 | public function getParent(): ?Collection |
||
706 | |||
707 | /** |
||
708 | * Get parents. |
||
709 | * |
||
710 | * @param array $parents |
||
711 | * |
||
712 | * @return array |
||
713 | */ |
||
714 | public function getParents(?NodeInterface $node = null, array $parents = []): array |
||
728 | |||
729 | /** |
||
730 | * Get as zip. |
||
731 | */ |
||
732 | public function getZip(): void |
||
738 | |||
739 | /** |
||
740 | * Create zip. |
||
741 | * |
||
742 | * @param ZipStream $archive |
||
743 | * @param bool $self true means that the zip represents the collection itself instead a child of the zip |
||
744 | * @param NodeInterface $parent |
||
745 | * @param string $path |
||
746 | * @param int $depth |
||
747 | * |
||
748 | * @return bool |
||
749 | */ |
||
750 | public function zip(ZipStream $archive, bool $self = true, ?NodeInterface $parent = null, string $path = '', int $depth = 0): bool |
||
795 | |||
796 | /** |
||
797 | * Get mime type. |
||
798 | * |
||
799 | * @return string |
||
800 | */ |
||
801 | 1 | public function getContentType(): string |
|
805 | |||
806 | /** |
||
807 | * Is reference. |
||
808 | * |
||
809 | * @return bool |
||
810 | */ |
||
811 | public function isReference(): bool |
||
815 | |||
816 | /** |
||
817 | * Set app attributes. |
||
818 | * |
||
819 | * @param AppInterface $app |
||
820 | * @param array $attributes |
||
821 | * |
||
822 | * @return NodeInterface |
||
823 | */ |
||
824 | public function setAppAttributes(string $namespace, array $attributes): NodeInterface |
||
831 | |||
832 | /** |
||
833 | * Set app attribute. |
||
834 | * |
||
835 | * @param AppInterface $app |
||
836 | * @param string $attribute |
||
837 | * @param mixed $value |
||
838 | * |
||
839 | * @return NodeInterface |
||
840 | */ |
||
841 | public function setAppAttribute(string $namespace, string $attribute, $value): NodeInterface |
||
852 | |||
853 | /** |
||
854 | * Remove app attribute. |
||
855 | * |
||
856 | * @param AppInterface $app |
||
857 | * |
||
858 | * @return NodeInterface |
||
859 | */ |
||
860 | public function unsetAppAttributes(string $namespace): NodeInterface |
||
869 | |||
870 | /** |
||
871 | * Remove app attribute. |
||
872 | * |
||
873 | * @param AppInterface $app |
||
874 | * @param string $attribute |
||
875 | * |
||
876 | * @return NodeInterface |
||
877 | */ |
||
878 | public function unsetAppAttribute(string $namespace, string $attribute): NodeInterface |
||
887 | |||
888 | /** |
||
889 | * Get app attribute. |
||
890 | * |
||
891 | * @param AppInterface $app |
||
892 | * @param string $attribute |
||
893 | * |
||
894 | * @return mixed |
||
895 | */ |
||
896 | public function getAppAttribute(string $namespace, string $attribute) |
||
904 | |||
905 | /** |
||
906 | * Get app attributes. |
||
907 | * |
||
908 | * @param AppInterface $app |
||
909 | * |
||
910 | * @return array |
||
911 | */ |
||
912 | public function getAppAttributes(string $namespace): array |
||
920 | |||
921 | /** |
||
922 | * Set meta attributes. |
||
923 | * |
||
924 | * @param array $attributes |
||
925 | * |
||
926 | * @return NodeInterface |
||
927 | */ |
||
928 | public function setMetaAttributes(array $attributes): NodeInterface |
||
943 | |||
944 | /** |
||
945 | * Get meta attributes as array. |
||
946 | * |
||
947 | * @param array $attribute Specify attributes to return |
||
948 | * |
||
949 | * @return array |
||
950 | */ |
||
951 | public function getMetaAttributes(array $attributes = []): array |
||
960 | |||
961 | /** |
||
962 | * Mark node as readonly. |
||
963 | * |
||
964 | * @param bool $readonly |
||
965 | * |
||
966 | * @return bool |
||
967 | */ |
||
968 | public function setReadonly(bool $readonly = true): bool |
||
974 | |||
975 | /** |
||
976 | * Mark node as self-destroyable. |
||
977 | * |
||
978 | * @param UTCDateTime $ts |
||
979 | * |
||
980 | * @return bool |
||
981 | */ |
||
982 | public function setDestroyable(?UTCDateTime $ts): bool |
||
992 | |||
993 | /** |
||
994 | * Get original raw attributes before any processing. |
||
995 | * |
||
996 | * @return array |
||
997 | */ |
||
998 | public function getRawAttributes(): array |
||
1002 | |||
1003 | /** |
||
1004 | * Check if node is in root. |
||
1005 | * |
||
1006 | * @return bool |
||
1007 | */ |
||
1008 | public function isInRoot(): bool |
||
1012 | |||
1013 | /** |
||
1014 | * Check if node is an instance of the actual root collection. |
||
1015 | * |
||
1016 | * @return bool |
||
1017 | */ |
||
1018 | public function isRoot(): bool |
||
1022 | |||
1023 | /** |
||
1024 | * Resolve node path. |
||
1025 | * |
||
1026 | * @return string |
||
1027 | */ |
||
1028 | public function getPath(): string |
||
1039 | |||
1040 | /** |
||
1041 | * Save node attributes. |
||
1042 | * |
||
1043 | * @param array|string $attributes |
||
1044 | * @param array|string $remove |
||
1045 | * @param string $recursion |
||
1046 | * @param bool $recursion_first |
||
1047 | * |
||
1048 | * @return bool |
||
1049 | */ |
||
1050 | public function save($attributes = [], $remove = [], ?string $recursion = null, bool $recursion_first = true): bool |
||
1114 | |||
1115 | /** |
||
1116 | * Get array value via string path. |
||
1117 | * |
||
1118 | * @param iterable $arr |
||
1119 | * @param string $path |
||
1120 | * @param string $seperator |
||
1121 | * |
||
1122 | * @return mixed |
||
1123 | */ |
||
1124 | protected function getArrayValue(Iterable $array, string $path, string $separator = '.') |
||
1141 | |||
1142 | /** |
||
1143 | * Validate meta attributes. |
||
1144 | * |
||
1145 | * @param array $attributes |
||
1146 | * |
||
1147 | * @return array |
||
1148 | */ |
||
1149 | protected function validateMetaAttributes(array $attributes): array |
||
1168 | |||
1169 | /** |
||
1170 | * Duplicate name with a uniqid within name. |
||
1171 | */ |
||
1172 | protected function getDuplicateName(?string $name = null, ?string $class = null): string |
||
1195 | |||
1196 | /** |
||
1197 | * Completly remove node. |
||
1198 | * |
||
1199 | * @return bool |
||
1200 | */ |
||
1201 | abstract protected function _forceDelete(): bool; |
||
1202 | } |
||
1203 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.