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 |
||
30 | class Filesystem |
||
31 | { |
||
32 | /** |
||
33 | * Database. |
||
34 | * |
||
35 | * @var Database |
||
36 | */ |
||
37 | protected $db; |
||
38 | |||
39 | /** |
||
40 | * LoggerInterface. |
||
41 | * |
||
42 | * @var LoggerInterface |
||
43 | */ |
||
44 | protected $logger; |
||
45 | |||
46 | /** |
||
47 | * Hook. |
||
48 | * |
||
49 | * @var Hook |
||
50 | */ |
||
51 | protected $hook; |
||
52 | |||
53 | /** |
||
54 | * Server. |
||
55 | * |
||
56 | * @var Server |
||
57 | */ |
||
58 | protected $server; |
||
59 | |||
60 | /** |
||
61 | * Root collection. |
||
62 | * |
||
63 | * @var Collection |
||
64 | */ |
||
65 | protected $root; |
||
66 | |||
67 | /** |
||
68 | * User. |
||
69 | * |
||
70 | * @var Delta |
||
71 | */ |
||
72 | protected $delta; |
||
73 | |||
74 | /** |
||
75 | * Get user. |
||
76 | * |
||
77 | * @var User |
||
78 | */ |
||
79 | protected $user; |
||
80 | |||
81 | /** |
||
82 | * Storage. |
||
83 | * |
||
84 | * @var Storage |
||
85 | */ |
||
86 | protected $storage; |
||
87 | |||
88 | /** |
||
89 | * Acl. |
||
90 | * |
||
91 | * @var Acl |
||
92 | */ |
||
93 | protected $acl; |
||
94 | |||
95 | /** |
||
96 | * Initialize. |
||
97 | * |
||
98 | * @param Server $server |
||
99 | * @param LoggerInterface $logger |
||
100 | * @param User $user |
||
101 | */ |
||
102 | public function __construct(Server $server, Database $db, Hook $hook, LoggerInterface $logger, Storage $storage, Acl $acl, ?User $user = null) |
||
112 | |||
113 | /** |
||
114 | * Get user. |
||
115 | * |
||
116 | * @return User |
||
117 | */ |
||
118 | public function getUser(): ?User |
||
122 | |||
123 | /** |
||
124 | * Get server. |
||
125 | * |
||
126 | * @return Server |
||
127 | */ |
||
128 | public function getServer(): Server |
||
132 | |||
133 | /** |
||
134 | * Get database. |
||
135 | * |
||
136 | * @return Database |
||
137 | */ |
||
138 | public function getDatabase(): Database |
||
142 | |||
143 | /** |
||
144 | * Get root. |
||
145 | * |
||
146 | * @return Collection |
||
147 | */ |
||
148 | public function getRoot(): Collection |
||
160 | |||
161 | /** |
||
162 | * Get delta. |
||
163 | * |
||
164 | * @return Delta |
||
165 | */ |
||
166 | public function getDelta(): Delta |
||
177 | |||
178 | /** |
||
179 | * Find raw node. |
||
180 | * |
||
181 | * @param ObjectId $id |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public function findRawNode(ObjectId $id): array |
||
197 | |||
198 | /** |
||
199 | * Factory loader. |
||
200 | * |
||
201 | * @param ObjectId|string $id |
||
202 | * @param string $class Fore check node type |
||
203 | * @param int $deleted |
||
204 | * |
||
205 | * @return NodeInterface |
||
206 | */ |
||
207 | public function findNodeById($id, ?string $class = null, int $deleted = NodeInterface::DELETED_INCLUDE): NodeInterface |
||
255 | |||
256 | /** |
||
257 | * Load node with path. |
||
258 | * |
||
259 | * @param string $path |
||
260 | * @param string $class Fore check node type |
||
261 | * |
||
262 | * @return NodeInterface |
||
263 | */ |
||
264 | public function findNodeByPath(string $path = '', ?string $class = null): NodeInterface |
||
288 | |||
289 | /** |
||
290 | * Load nodes by id. |
||
291 | * |
||
292 | * @param array $id |
||
293 | * @param string $class Force check node type |
||
294 | * @param bool $deleted |
||
295 | * |
||
296 | * @return Generator |
||
297 | */ |
||
298 | public function findNodesById(array $id = [], ?string $class = null, int $deleted = NodeInterface::DELETED_INCLUDE): Generator |
||
335 | |||
336 | /** |
||
337 | * Load nodes by id. |
||
338 | * |
||
339 | * @param array $path |
||
340 | * @param string $class Force check node type |
||
341 | * |
||
342 | * @return Generator |
||
343 | */ |
||
344 | public function findNodesByPath(array $path = [], ?string $class = null): Generator |
||
371 | |||
372 | /** |
||
373 | * Load nodes by id. |
||
374 | * |
||
375 | * @param array $id |
||
376 | * @param array $path |
||
377 | * @param string $class Force set node type |
||
378 | * @param int $deleted |
||
379 | * |
||
380 | * @return Generator |
||
381 | */ |
||
382 | public function getNodes(?array $id = null, ?array $path = null, $class = null, int $deleted = NodeInterface::DELETED_EXCLUDE): Generator |
||
405 | |||
406 | /** |
||
407 | * Load node. |
||
408 | * |
||
409 | * @param string $id |
||
410 | * @param string $path |
||
411 | * @param string $class Force set node type |
||
412 | * @param bool $multiple Allow $id to be an array |
||
413 | * @param bool $allow_root Allow instance of root collection |
||
414 | * @param bool $deleted How to handle deleted node |
||
415 | * |
||
416 | * @return NodeInterface |
||
417 | */ |
||
418 | public function getNode($id = null, $path = null, $class = null, bool $multiple = false, bool $allow_root = false, int $deleted = NodeInterface::DELETED_EXCLUDE): NodeInterface |
||
449 | |||
450 | /** |
||
451 | * Find node with custom filter. |
||
452 | * |
||
453 | * @param array $filter |
||
454 | * |
||
455 | * @return NodeInterface |
||
456 | */ |
||
457 | public function findNodeByFilter(array $filter): NodeInterface |
||
469 | |||
470 | /** |
||
471 | * Find nodes with custom filters. |
||
472 | * |
||
473 | * @param array $filter |
||
474 | * |
||
475 | * @return Generator |
||
476 | */ |
||
477 | public function findNodesByFilter(array $filter): Generator |
||
493 | |||
494 | /** |
||
495 | * Get custom filtered children. |
||
496 | * |
||
497 | * @param int $deleted |
||
498 | * @param array $filter |
||
499 | * |
||
500 | * @return Generator |
||
501 | */ |
||
502 | public function findNodesByFilterUser(int $deleted, array $filter): Generator |
||
537 | |||
538 | /** |
||
539 | * Init node. |
||
540 | * |
||
541 | * @param array $node |
||
542 | * |
||
543 | * @return NodeInterface |
||
544 | */ |
||
545 | public function initNode(array $node): NodeInterface |
||
590 | |||
591 | /** |
||
592 | * Resolve shared node to reference or share depending who requested. |
||
593 | * |
||
594 | * @param array $node |
||
595 | * |
||
596 | * @return array |
||
597 | */ |
||
598 | protected function findReferenceNode(array $node): array |
||
638 | } |
||
639 |
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: