Complex classes like Filesystem 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 Filesystem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Filesystem |
||
29 | { |
||
30 | /** |
||
31 | * Database. |
||
32 | * |
||
33 | * @var Database |
||
34 | */ |
||
35 | protected $db; |
||
36 | |||
37 | /** |
||
38 | * LoggerInterface. |
||
39 | * |
||
40 | * @var LoggerInterface |
||
41 | */ |
||
42 | protected $logger; |
||
43 | |||
44 | /** |
||
45 | * Hook. |
||
46 | * |
||
47 | * @var Hook |
||
48 | */ |
||
49 | protected $hook; |
||
50 | |||
51 | /** |
||
52 | * Server. |
||
53 | * |
||
54 | * @var Server |
||
55 | */ |
||
56 | protected $server; |
||
57 | |||
58 | /** |
||
59 | * Root collection. |
||
60 | * |
||
61 | * @var Collection |
||
62 | */ |
||
63 | protected $root; |
||
64 | |||
65 | /** |
||
66 | * User. |
||
67 | * |
||
68 | * @var Delta |
||
69 | */ |
||
70 | protected $delta; |
||
71 | |||
72 | /** |
||
73 | * Get user. |
||
74 | * |
||
75 | * @var User |
||
76 | */ |
||
77 | protected $user; |
||
78 | |||
79 | /** |
||
80 | * Node factory. |
||
81 | * |
||
82 | * @var NodeFactory |
||
83 | */ |
||
84 | protected $node_factory; |
||
85 | |||
86 | /** |
||
87 | * Acl. |
||
88 | * |
||
89 | * @var Acl |
||
90 | */ |
||
91 | protected $acl; |
||
92 | |||
93 | /** |
||
94 | * Cache. |
||
95 | * |
||
96 | * @var array |
||
97 | */ |
||
98 | protected $cache = []; |
||
99 | |||
100 | /** |
||
101 | * RAW Cache. |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $raw_cache = []; |
||
106 | |||
107 | /** |
||
108 | * Initialize. |
||
109 | */ |
||
110 | public function __construct(Server $server, Database $db, Hook $hook, LoggerInterface $logger, NodeFactory $node_factory, Acl $acl, ?User $user = null) |
||
120 | |||
121 | /** |
||
122 | * Get user. |
||
123 | */ |
||
124 | public function getUser(): ?User |
||
128 | |||
129 | /** |
||
130 | * Get server. |
||
131 | */ |
||
132 | public function getServer(): Server |
||
136 | |||
137 | /** |
||
138 | * Get database. |
||
139 | */ |
||
140 | public function getDatabase(): Database |
||
144 | |||
145 | /** |
||
146 | * Get root. |
||
147 | */ |
||
148 | public function getRoot(): Collection |
||
160 | |||
161 | /** |
||
162 | * Get delta. |
||
163 | */ |
||
164 | public function getDelta(): Delta |
||
172 | |||
173 | /** |
||
174 | * Find raw node. |
||
175 | */ |
||
176 | public function findRawNode(ObjectId $id): array |
||
194 | |||
195 | /** |
||
196 | * Factory loader. |
||
197 | */ |
||
198 | public function findNodeById($id, ?string $class = null, int $deleted = NodeInterface::DELETED_INCLUDE): NodeInterface |
||
236 | |||
237 | /** |
||
238 | * Find one. |
||
239 | */ |
||
240 | public function findOne(array $filter, int $deleted = NodeInterface::DELETED_INCLUDE, ?Collection $parent = null): NodeInterface |
||
241 | { |
||
242 | $result = iterator_to_array($this->findNodesByFilterRecursiveChildren($filter, $deleted, 0, 1, $parent)); |
||
243 | |||
244 | if (count($result) === 0) { |
||
245 | throw new Exception\NotFound( |
||
246 | 'requested node not found', |
||
247 | Exception\NotFound::NODE_NOT_FOUND |
||
248 | ); |
||
249 | } |
||
250 | |||
251 | return array_shift($result); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Load nodes by id. |
||
256 | * |
||
257 | * @deprecated |
||
258 | */ |
||
259 | public function findNodesById(array $id = [], ?string $class = null, int $deleted = NodeInterface::DELETED_INCLUDE): Generator |
||
311 | |||
312 | /** |
||
313 | * Load nodes by id. |
||
314 | * |
||
315 | * @param null|mixed $class |
||
316 | * |
||
317 | * @deprecated |
||
318 | */ |
||
319 | public function getNodes(?array $id = null, $class = null, int $deleted = NodeInterface::DELETED_EXCLUDE): Generator |
||
323 | |||
324 | /** |
||
325 | * Load node. |
||
326 | * |
||
327 | * @param null|mixed $id |
||
328 | * @param null|mixed $class |
||
329 | * |
||
330 | * @deprecated |
||
331 | */ |
||
332 | public function getNode($id = null, $class = null, bool $multiple = false, bool $allow_root = false, ?int $deleted = null): NodeInterface |
||
352 | |||
353 | /** |
||
354 | * Find node with custom filter. |
||
355 | */ |
||
356 | public function findNodeByFilter(array $filter): NodeInterface |
||
368 | |||
369 | /** |
||
370 | * Count. |
||
371 | */ |
||
372 | public function countNodes(array $filter = []): int |
||
376 | |||
377 | /** |
||
378 | * Find nodes with custom filters. |
||
379 | */ |
||
380 | public function findNodesByFilter(array $filter, ?int $offset = null, ?int $limit = null): Generator |
||
402 | |||
403 | /** |
||
404 | * Find nodes with custom filter recursive. |
||
405 | */ |
||
406 | public function findNodesByFilterRecursiveToArray(Collection $collection, array $filter = []): array |
||
431 | |||
432 | /** |
||
433 | * Get deleted nodes. |
||
434 | * |
||
435 | * Note this query excludes deleted nodes which have a deleted parent |
||
436 | */ |
||
437 | public function getTrash(array $query = [], ?int $offset = null, ?int $limit = null): Generator |
||
511 | |||
512 | /** |
||
513 | * Find nodes with custom filter recursive. |
||
514 | */ |
||
515 | public function findNodesByFilterRecursiveChildren(array $parent_filter, int $deleted, ?int $offset = null, ?int $limit = null, ?Collection $parent = null): Generator |
||
516 | { |
||
517 | $query = [ |
||
518 | ['$match' => $parent_filter], |
||
519 | ['$graphLookup' => [ |
||
520 | 'from' => 'storage', |
||
521 | 'startWith' => '$pointer', |
||
522 | 'connectFromField' => 'pointer', |
||
523 | 'connectToField' => 'parent', |
||
524 | 'as' => 'children', |
||
525 | 'maxDepth' => 0, |
||
526 | 'restrictSearchWithMatch' => $this->prepareChildrenFilter($deleted), |
||
527 | ]], |
||
528 | ['$addFields' => [ |
||
529 | 'size' => [ |
||
530 | '$cond' => [ |
||
531 | 'if' => ['$eq' => ['$directory', true]], |
||
532 | 'then' => ['$size' => '$children'], |
||
533 | 'else' => '$size', |
||
534 | ], |
||
535 | ], |
||
536 | ]], |
||
537 | ['$project' => ['children' => 0]], |
||
538 | ['$group' => ['_id' => null, 'total' => ['$sum' => 1]]], |
||
539 | ]; |
||
540 | |||
541 | return $this->executeAggregation($query, $offset, $limit, $parent); |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Find nodes with custom filter recursive. |
||
546 | */ |
||
547 | public function findNodesByFilterRecursive(Collection $collection, array $filter = [], ?int $offset = null, ?int $limit = null): Generator |
||
570 | |||
571 | /** |
||
572 | * Get custom filtered children. |
||
573 | * |
||
574 | * @deprecated |
||
575 | */ |
||
576 | public function findNodesByFilterUser(int $deleted, array $filter, ?int $offset = null, ?int $limit = null): Generator |
||
597 | |||
598 | /** |
||
599 | * Init node. |
||
600 | */ |
||
601 | public function initNode(array $node, ?Collection $parent = null): NodeInterface |
||
602 | { |
||
603 | $id = $node['_id']; |
||
604 | |||
605 | if (isset($node['shared']) && true === $node['shared'] && null !== $this->user && $node['owner'] != $this->user->getId()) { |
||
606 | $node = $this->findReferenceNode($node); |
||
607 | } |
||
608 | |||
609 | if (isset($node['parent'])) { |
||
610 | if ($parent === null || $parent->getId() != $node['parent']) { |
||
611 | $parent = $this->findNodeById($node['parent']); |
||
612 | } |
||
613 | } elseif ($node['_id'] !== null) { |
||
614 | $parent = $this->getRoot(); |
||
615 | } |
||
616 | |||
617 | if (!array_key_exists('directory', $node)) { |
||
618 | throw new Exception('invalid node ['.$node['_id'].'] found, directory attribute does not exists'); |
||
619 | } |
||
620 | |||
621 | $instance = $this->node_factory->build($this, $node, $parent); |
||
622 | |||
623 | if (!$this->acl->isAllowed($instance, 'r')) { |
||
624 | if ($instance->isReference()) { |
||
625 | $instance->delete(true); |
||
626 | } |
||
627 | |||
628 | throw new ForbiddenException( |
||
629 | 'not allowed to access node', |
||
630 | ForbiddenException::NOT_ALLOWED_TO_ACCESS |
||
631 | ); |
||
632 | } |
||
633 | |||
634 | $loaded = isset($this->cache[(string) $node['_id']]); |
||
635 | |||
636 | if ($loaded === false) { |
||
637 | $this->cache[(string) $node['_id']] = $instance; |
||
638 | } |
||
639 | |||
640 | if ($loaded === false && isset($node['destroy']) && $node['destroy'] instanceof UTCDateTime && $node['destroy']->toDateTime()->format('U') <= time()) { |
||
641 | $this->logger->info('node ['.$node['_id'].'] is not accessible anmyore, destroy node cause of expired destroy flag', [ |
||
642 | 'category' => get_class($this), |
||
643 | ]); |
||
644 | |||
645 | $instance->delete(true); |
||
646 | |||
647 | throw new Exception\Conflict('node is not available anymore'); |
||
648 | } |
||
649 | |||
650 | if (PHP_SAPI === 'cli') { |
||
651 | unset($this->cache[(string) $node['_id']]); |
||
652 | } |
||
653 | |||
654 | return $instance; |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * Find node with path. |
||
659 | */ |
||
660 | public function findNodeByPath(string $path = '', ?string $class = null): NodeInterface |
||
699 | |||
700 | /** |
||
701 | * Prepare children filter. |
||
702 | */ |
||
703 | protected function prepareChildrenFilter(int $deleted) |
||
741 | |||
742 | /** |
||
743 | * Execute complex aggregation. |
||
744 | */ |
||
745 | protected function executeAggregation(array $query, ?int $offset = null, ?int $limit = null, ?Collection $parent = null): Generator |
||
774 | |||
775 | /** |
||
776 | * Resolve shared node to reference or share depending who requested. |
||
777 | */ |
||
778 | protected function findReferenceNode(array $node): array |
||
818 | } |
||
819 |
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: