Complex classes like Collection 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 Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Collection extends AbstractNode implements IQuota |
||
28 | { |
||
29 | /** |
||
30 | * Root folder. |
||
31 | */ |
||
32 | const ROOT_FOLDER = '/'; |
||
33 | |||
34 | /** |
||
35 | * Share acl. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $acl = []; |
||
40 | |||
41 | /** |
||
42 | * Share name. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $share_name; |
||
47 | |||
48 | /** |
||
49 | * filter. |
||
50 | * |
||
51 | * @param string |
||
52 | */ |
||
53 | protected $filter; |
||
54 | |||
55 | /** |
||
56 | * Initialize. |
||
57 | * |
||
58 | * @param array $attributes |
||
59 | * @param Filesystem $fs |
||
60 | */ |
||
61 | public function __construct(array $attributes, Filesystem $fs, LoggerInterface $logger, Hook $hook, Acl $acl) |
||
78 | |||
79 | /** |
||
80 | * Copy node with children. |
||
81 | * |
||
82 | * @param Collection $parent |
||
83 | * @param int $conflict |
||
84 | * @param string $recursion |
||
85 | * @param bool $recursion_first |
||
86 | * |
||
87 | * @return NodeInterface |
||
88 | */ |
||
89 | public function copyTo(self $parent, int $conflict = NodeInterface::CONFLICT_NOACTION, ?string $recursion = null, bool $recursion_first = true): NodeInterface |
||
139 | |||
140 | /** |
||
141 | * Get share. |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getAcl(): array |
||
157 | |||
158 | /** |
||
159 | * Get Share name. |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getShareName(): string |
||
171 | |||
172 | /** |
||
173 | * Get Attributes. |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | public function getAttributes(): array |
||
200 | |||
201 | /** |
||
202 | * Set collection filter. |
||
203 | * |
||
204 | * @param string $filter |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function setFilter(?array $filter = null): bool |
||
214 | |||
215 | /** |
||
216 | * Get collection. |
||
217 | */ |
||
218 | public function get(): void |
||
222 | |||
223 | /** |
||
224 | * Fetch children items of this collection. |
||
225 | * |
||
226 | * Deleted: |
||
227 | * 0 - Exclude deleted |
||
228 | * 1 - Only deleted |
||
229 | * 2 - Include deleted |
||
230 | * |
||
231 | * @param int $deleted |
||
232 | * @param array $filter |
||
233 | * @param int $offset |
||
234 | * @param int $limit |
||
235 | * |
||
236 | * @return Generator |
||
237 | */ |
||
238 | public function getChildNodes(int $deleted = NodeInterface::DELETED_EXCLUDE, array $filter = [], ?int $offset = null, ?int $limit = null): Generator |
||
244 | |||
245 | /** |
||
246 | * Fetch children items of this collection (as array). |
||
247 | * |
||
248 | * Deleted: |
||
249 | * 0 - Exclude deleted |
||
250 | * 1 - Only deleted |
||
251 | * 2 - Include deleted |
||
252 | * |
||
253 | * @param int $deleted |
||
254 | * @param array $filter |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | public function getChildren(int $deleted = NodeInterface::DELETED_EXCLUDE, array $filter = []): array |
||
262 | |||
263 | /** |
||
264 | * Is custom filter node. |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function isCustomFilter(): bool |
||
272 | |||
273 | /** |
||
274 | * Get number of children. |
||
275 | * |
||
276 | * @return int |
||
277 | */ |
||
278 | public function getSize(): int |
||
282 | |||
283 | /** |
||
284 | * Get real id (reference). |
||
285 | * |
||
286 | * @return ObjectId |
||
287 | */ |
||
288 | public function getRealId(): ?ObjectId |
||
296 | |||
297 | /** |
||
298 | * Get user quota information. |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | public function getQuotaInfo(): array |
||
311 | |||
312 | /** |
||
313 | * Fetch children items of this collection. |
||
314 | * |
||
315 | * @param string $node |
||
316 | * @param int $deleted |
||
317 | * @param array $filter |
||
318 | * @param mixed $name |
||
319 | * |
||
320 | * @return NodeInterface |
||
321 | */ |
||
322 | public function getChild($name, int $deleted = NodeInterface::DELETED_EXCLUDE, array $filter = []): NodeInterface |
||
341 | |||
342 | /** |
||
343 | * Delete node. |
||
344 | * |
||
345 | * Actually the node will not be deleted (Just set a delete flag), set $force=true to |
||
346 | * delete finally |
||
347 | * |
||
348 | * @param bool $force |
||
349 | * @param string $recursion Identifier to identify a recursive action |
||
350 | * @param bool $recursion_first |
||
351 | * |
||
352 | * @return bool |
||
353 | */ |
||
354 | public function delete(bool $force = false, ?string $recursion = null, bool $recursion_first = true): bool |
||
409 | |||
410 | /** |
||
411 | * Check if this collection has child named $name. |
||
412 | * |
||
413 | * deleted: |
||
414 | * |
||
415 | * 0 - Exclude deleted |
||
416 | * 1 - Only deleted |
||
417 | * 2 - Include deleted |
||
418 | * |
||
419 | * @param string $name |
||
420 | * @param int $deleted |
||
421 | * @param array $filter |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | public function childExists($name, $deleted = NodeInterface::DELETED_EXCLUDE, array $filter = []): bool |
||
457 | |||
458 | /** |
||
459 | * Share collection. |
||
460 | * |
||
461 | * @param array $acl |
||
462 | * @param string $name |
||
463 | * |
||
464 | * @return bool |
||
465 | */ |
||
466 | public function share(array $acl, string $name): bool |
||
523 | |||
524 | /** |
||
525 | * Unshare collection. |
||
526 | * |
||
527 | * @return bool |
||
528 | */ |
||
529 | public function unshare(): bool |
||
569 | |||
570 | /** |
||
571 | * Get children. |
||
572 | * |
||
573 | * @param ObjectId $id |
||
574 | * @param array $shares |
||
575 | * |
||
576 | * @return array |
||
577 | */ |
||
578 | public function getChildrenRecursive(?ObjectId $id = null, ?array &$shares = []): array |
||
606 | |||
607 | /** |
||
608 | * Create new directory. |
||
609 | * |
||
610 | * @param string $name |
||
611 | * @param arracy $attributes |
||
612 | * @param int $conflict |
||
613 | * @param bool $clone |
||
614 | * |
||
615 | * @return Collection |
||
616 | */ |
||
617 | public function addDirectory($name, array $attributes = [], int $conflict = NodeInterface::CONFLICT_NOACTION, bool $clone = false): self |
||
699 | |||
700 | /** |
||
701 | * Create new file as a child from this collection. |
||
702 | * |
||
703 | * @param string $name |
||
704 | * @param mixed $data |
||
705 | * @param array $attributes |
||
706 | * @param int $conflict |
||
707 | * @param bool $clone |
||
708 | * |
||
709 | * @return File |
||
710 | */ |
||
711 | public function addFile($name, $data = null, array $attributes = [], int $conflict = NodeInterface::CONFLICT_NOACTION, bool $clone = false): File |
||
812 | |||
813 | /** |
||
814 | * Create new file wrapper |
||
815 | * (Sabe\DAV compatible method, elsewhere use addFile(). |
||
816 | * |
||
817 | * Sabre\DAV requires that createFile() returns the ETag instead the newly created file instance |
||
818 | * |
||
819 | * @param string $name |
||
820 | * @param string $data |
||
821 | * |
||
822 | * @return File |
||
823 | */ |
||
824 | public function createFile($name, $data = null): String |
||
828 | |||
829 | /** |
||
830 | * Create new directory wrapper |
||
831 | * (Sabe\DAV compatible method, elsewhere use addDirectory(). |
||
832 | * |
||
833 | * Sabre\DAV requires that createDirectory() returns void |
||
834 | * |
||
835 | * @param string $name |
||
836 | */ |
||
837 | public function createDirectory($name): void |
||
841 | |||
842 | /** |
||
843 | * Do recursive Action. |
||
844 | * |
||
845 | * @param callable $callable |
||
846 | * @param int $deleted |
||
847 | * @param bool $ignore_exception |
||
848 | * |
||
849 | * @return bool |
||
850 | */ |
||
851 | public function doRecursiveAction(callable $callable, int $deleted = NodeInterface::DELETED_EXCLUDE): bool |
||
861 | |||
862 | /** |
||
863 | * Get children query filter. |
||
864 | * |
||
865 | * Deleted: |
||
866 | * 0 - Exclude deleted |
||
867 | * 1 - Only deleted |
||
868 | * 2 - Include deleted |
||
869 | * |
||
870 | * @param int $deleted |
||
871 | * @param array $filter |
||
872 | * |
||
873 | * @return array |
||
874 | */ |
||
875 | protected function getChildrenFilter(int $deleted = NodeInterface::DELETED_EXCLUDE, array $filter = []): array |
||
927 | |||
928 | /** |
||
929 | * Completely remove node. |
||
930 | * |
||
931 | * @param string $recursion Identifier to identify a recursive action |
||
932 | * @param bool $recursion_first |
||
933 | * |
||
934 | * @return bool |
||
935 | */ |
||
936 | protected function _forceDelete(?string $recursion = null, bool $recursion_first = true): bool |
||
970 | } |
||
971 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: