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 |
||
| 31 | abstract class AbstractNode implements NodeInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * name max lenght. |
||
| 35 | */ |
||
| 36 | const MAX_NAME_LENGTH = 255; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Unique id. |
||
| 40 | * |
||
| 41 | * @var ObjectId |
||
| 42 | */ |
||
| 43 | protected $_id; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Node name. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $name = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Owner. |
||
| 54 | * |
||
| 55 | * @var ObjectId |
||
| 56 | */ |
||
| 57 | protected $owner; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Mime. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $mime; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Meta attributes. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $meta = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Parent collection. |
||
| 75 | * |
||
| 76 | * @var ObjectId |
||
| 77 | */ |
||
| 78 | protected $parent; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Is file deleted. |
||
| 82 | * |
||
| 83 | * @var bool|UTCDateTime |
||
| 84 | */ |
||
| 85 | protected $deleted = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Is shared? |
||
| 89 | * |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | protected $shared = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Destory at a certain time. |
||
| 96 | * |
||
| 97 | * @var UTCDateTime |
||
| 98 | */ |
||
| 99 | protected $destroy; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Changed timestamp. |
||
| 103 | * |
||
| 104 | * @var UTCDateTime |
||
| 105 | */ |
||
| 106 | protected $changed; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Created timestamp. |
||
| 110 | * |
||
| 111 | * @var UTCDateTime |
||
| 112 | */ |
||
| 113 | protected $created; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Point to antother node (Means this node is reference to $reference). |
||
| 117 | * |
||
| 118 | * @var ObjectId |
||
| 119 | */ |
||
| 120 | protected $reference; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Raw attributes before any processing or modifications. |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected $raw_attributes; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Readonly flag. |
||
| 131 | * |
||
| 132 | * @var bool |
||
| 133 | */ |
||
| 134 | protected $readonly = false; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * App attributes. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | protected $app = []; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Filesystem. |
||
| 145 | * |
||
| 146 | * @var Filesystem |
||
| 147 | */ |
||
| 148 | protected $_fs; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Database. |
||
| 152 | * |
||
| 153 | * @var Database |
||
| 154 | */ |
||
| 155 | protected $_db; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * User. |
||
| 159 | * |
||
| 160 | * @var User |
||
| 161 | */ |
||
| 162 | protected $_user; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Logger. |
||
| 166 | * |
||
| 167 | * @var LoggerInterface |
||
| 168 | */ |
||
| 169 | protected $_logger; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Server. |
||
| 173 | * |
||
| 174 | * @var Server |
||
| 175 | */ |
||
| 176 | protected $_server; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Hook. |
||
| 180 | * |
||
| 181 | * @var Hook |
||
| 182 | */ |
||
| 183 | protected $_hook; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Acl. |
||
| 187 | * |
||
| 188 | * @var Acl |
||
| 189 | */ |
||
| 190 | protected $_acl; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Storage adapter. |
||
| 194 | * |
||
| 195 | * @var string |
||
| 196 | */ |
||
| 197 | protected $storage_adapter; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Acl. |
||
| 201 | * |
||
| 202 | * @var Acl |
||
| 203 | */ |
||
| 204 | protected $acl; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Convert to filename. |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function __toString() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get owner. |
||
| 218 | * |
||
| 219 | * @return ObjectId |
||
| 220 | */ |
||
| 221 | public function getOwner(): ObjectId |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Set filesystem. |
||
| 228 | * |
||
| 229 | * @param Filesystem $fs |
||
| 230 | * |
||
| 231 | * @return NodeInterface |
||
| 232 | */ |
||
| 233 | public function setFilesystem(Filesystem $fs): NodeInterface |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get filesystem. |
||
| 243 | * |
||
| 244 | * @return Filesystem |
||
| 245 | */ |
||
| 246 | public function getFilesystem(): Filesystem |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Check if $node is a sub node of any parent nodes of this node. |
||
| 253 | * |
||
| 254 | * @param NodeInterface $node |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function isSubNode(NodeInterface $node): bool |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Move node. |
||
| 279 | * |
||
| 280 | * @param Collection $parent |
||
| 281 | * @param int $conflict |
||
| 282 | * |
||
| 283 | * @return NodeInterface |
||
| 284 | */ |
||
| 285 | public function setParent(Collection $parent, int $conflict = NodeInterface::CONFLICT_NOACTION): NodeInterface |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get share id. |
||
| 366 | * |
||
| 367 | * @param bool $reference |
||
| 368 | * |
||
| 369 | * @return ObjectId |
||
| 370 | */ |
||
| 371 | public function getShareId(bool $reference = false): ?ObjectId |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get share node. |
||
| 394 | * |
||
| 395 | * @param bool $reference |
||
| 396 | * |
||
| 397 | * @return Collection |
||
| 398 | */ |
||
| 399 | public function getShareNode(): ?Collection |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Is node marked as readonly? |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | public function isReadonly(): bool |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Request is from node owner? |
||
| 420 | * |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | public function isOwnerRequest(): bool |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Check if node is kind of special. |
||
| 430 | * |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | public function isSpecial(): bool |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Check if node is a sub node of a share. |
||
| 450 | * |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | public function isShareMember(): bool |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Is share. |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | public function isShare(): bool |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Is share (Reference or master share). |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | public function isShared(): bool |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set the name. |
||
| 484 | * |
||
| 485 | * @param string $name |
||
| 486 | * |
||
| 487 | * @return bool |
||
| 488 | */ |
||
| 489 | public function setName($name): bool |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Check name. |
||
| 512 | * |
||
| 513 | * @param string $name |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function checkName(string $name): string |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Get the name. |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function getName(): string |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Undelete. |
||
| 545 | * |
||
| 546 | * @param int $conflict |
||
| 547 | * @param string $recursion |
||
| 548 | * @param bool $recursion_first |
||
| 549 | * |
||
| 550 | * @return bool |
||
| 551 | */ |
||
| 552 | public function undelete(int $conflict = NodeInterface::CONFLICT_NOACTION, ?string $recursion = null, bool $recursion_first = true): bool |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Is node deleted? |
||
| 644 | * |
||
| 645 | * @return bool |
||
| 646 | */ |
||
| 647 | public function isDeleted(): bool |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Get last modified timestamp. |
||
| 654 | * |
||
| 655 | * @return int |
||
| 656 | */ |
||
| 657 | public function getLastModified(): int |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get unique id. |
||
| 668 | * |
||
| 669 | * @return ObjectId|string |
||
| 670 | */ |
||
| 671 | public function getId(bool $string = false) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Get parent. |
||
| 682 | * |
||
| 683 | * @return Collection |
||
| 684 | */ |
||
| 685 | public function getParent(): ?Collection |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Get parents. |
||
| 718 | * |
||
| 719 | * @param array $parents |
||
| 720 | * |
||
| 721 | * @return array |
||
| 722 | */ |
||
| 723 | public function getParents(?NodeInterface $node = null, array $parents = []): array |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Get as zip. |
||
| 740 | */ |
||
| 741 | public function getZip(): void |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Create zip. |
||
| 756 | * |
||
| 757 | * @param ZipStream $archive |
||
| 758 | * @param bool $self true means that the zip represents the collection itself instead a child of the zip |
||
| 759 | * @param NodeInterface $parent |
||
| 760 | * @param string $path |
||
| 761 | * @param int $depth |
||
| 762 | * |
||
| 763 | * @return bool |
||
| 764 | */ |
||
| 765 | public function zip(ZipStream $archive, bool $self = true, ?NodeInterface $parent = null, string $path = '', int $depth = 0): bool |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Get mime type. |
||
| 810 | * |
||
| 811 | * @return string |
||
| 812 | */ |
||
| 813 | public function getContentType(): string |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Is reference. |
||
| 820 | * |
||
| 821 | * @return bool |
||
| 822 | */ |
||
| 823 | public function isReference(): bool |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Set app attributes. |
||
| 830 | * |
||
| 831 | * @param AppInterface $app |
||
| 832 | * @param array $attributes |
||
| 833 | * |
||
| 834 | * @return NodeInterface |
||
| 835 | */ |
||
| 836 | public function setAppAttributes(string $namespace, array $attributes): NodeInterface |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Set app attribute. |
||
| 846 | * |
||
| 847 | * @param AppInterface $app |
||
| 848 | * @param string $attribute |
||
| 849 | * @param mixed $value |
||
| 850 | * |
||
| 851 | * @return NodeInterface |
||
| 852 | */ |
||
| 853 | public function setAppAttribute(string $namespace, string $attribute, $value): NodeInterface |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Remove app attribute. |
||
| 867 | * |
||
| 868 | * @param AppInterface $app |
||
| 869 | * |
||
| 870 | * @return NodeInterface |
||
| 871 | */ |
||
| 872 | public function unsetAppAttributes(string $namespace): NodeInterface |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Remove app attribute. |
||
| 884 | * |
||
| 885 | * @param AppInterface $app |
||
| 886 | * @param string $attribute |
||
| 887 | * |
||
| 888 | * @return NodeInterface |
||
| 889 | */ |
||
| 890 | public function unsetAppAttribute(string $namespace, string $attribute): NodeInterface |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Get app attribute. |
||
| 902 | * |
||
| 903 | * @param AppInterface $app |
||
| 904 | * @param string $attribute |
||
| 905 | * |
||
| 906 | * @return mixed |
||
| 907 | */ |
||
| 908 | public function getAppAttribute(string $namespace, string $attribute) |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Get app attributes. |
||
| 919 | * |
||
| 920 | * @param AppInterface $app |
||
| 921 | * |
||
| 922 | * @return array |
||
| 923 | */ |
||
| 924 | public function getAppAttributes(string $namespace): array |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Set meta attribute. |
||
| 935 | * |
||
| 936 | * @param array|string |
||
| 937 | * @param mixed $value |
||
| 938 | * @param mixed $attributes |
||
| 939 | * |
||
| 940 | * @return NodeInterface |
||
| 941 | */ |
||
| 942 | public function setMetaAttribute($attributes, $value = null): NodeInterface |
||
| 949 | |||
| 950 | /** |
||
| 951 | * validate meta attribut. |
||
| 952 | * |
||
| 953 | * @param array|string $attributes |
||
| 954 | * @param mixed $value |
||
| 955 | * @param array $set |
||
| 956 | * |
||
| 957 | * @return array |
||
| 958 | */ |
||
| 959 | public static function validateMetaAttribute($attributes, $value = null, array $set = []): array |
||
| 982 | |||
| 983 | /** |
||
| 984 | * Get meta attributes as array. |
||
| 985 | * |
||
| 986 | * @param array|string $attribute Specify attributes to return |
||
| 987 | * |
||
| 988 | * @return array|string |
||
| 989 | */ |
||
| 990 | public function getMetaAttribute($attribute = []) |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Mark node as readonly. |
||
| 1005 | * |
||
| 1006 | * @param bool $readonly |
||
| 1007 | * |
||
| 1008 | * @return bool |
||
| 1009 | */ |
||
| 1010 | public function setReadonly(bool $readonly = true): bool |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Mark node as self-destroyable. |
||
| 1019 | * |
||
| 1020 | * @param UTCDateTime $ts |
||
| 1021 | * |
||
| 1022 | * @return bool |
||
| 1023 | */ |
||
| 1024 | public function setDestroyable(?UTCDateTime $ts): bool |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Get original raw attributes before any processing. |
||
| 1037 | * |
||
| 1038 | * @return array |
||
| 1039 | */ |
||
| 1040 | public function getRawAttributes(): array |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Check if node is in root. |
||
| 1047 | * |
||
| 1048 | * @return bool |
||
| 1049 | */ |
||
| 1050 | public function isInRoot(): bool |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Check if node is an instance of the actual root collection. |
||
| 1057 | * |
||
| 1058 | * @return bool |
||
| 1059 | */ |
||
| 1060 | public function isRoot(): bool |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Resolve node path. |
||
| 1067 | * |
||
| 1068 | * @return string |
||
| 1069 | */ |
||
| 1070 | public function getPath(): string |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Save node attributes. |
||
| 1084 | * |
||
| 1085 | * @param array|string $attributes |
||
| 1086 | * @param array|string $remove |
||
| 1087 | * @param string $recursion |
||
| 1088 | * @param bool $recursion_first |
||
| 1089 | * |
||
| 1090 | * @return bool |
||
| 1091 | */ |
||
| 1092 | public function save($attributes = [], $remove = [], ?string $recursion = null, bool $recursion_first = true): bool |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Duplicate name with a uniqid within name. |
||
| 1159 | * |
||
| 1160 | * @param string $name |
||
| 1161 | * |
||
| 1162 | * @return string |
||
| 1163 | */ |
||
| 1164 | protected function getDuplicateName(?string $name = null): string |
||
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Completly remove node. |
||
| 1185 | * |
||
| 1186 | * @return bool |
||
| 1187 | */ |
||
| 1188 | abstract protected function _forceDelete(): bool; |
||
| 1189 | } |
||
| 1190 |
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.