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 |
||
| 177 | { |
||
| 178 | if (isset($this->raw_cache[(string) $id])) { |
||
| 179 | return $this->raw_cache[(string) $id]; |
||
| 180 | } |
||
| 181 | |||
| 182 | $node = $this->db->storage->findOne(['_id' => $id]); |
||
| 183 | if (null === $node) { |
||
| 184 | throw new Exception\NotFound('node '.$id.' not found', Exception\NotFound::NODE_NOT_FOUND); |
||
| 185 | } |
||
| 186 | |||
| 187 | $this->raw_cache[(string) $id] = $node; |
||
| 188 | |||
| 189 | return $node; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Factory loader. |
||
| 194 | */ |
||
| 195 | public function findNodeById($id, ?string $class = null, int $deleted = NodeInterface::DELETED_INCLUDE): NodeInterface |
||
| 196 | { |
||
| 197 | if (isset($this->cache[(string) $id])) { |
||
| 198 | return $this->cache[(string) $id]; |
||
| 199 | } |
||
| 200 | |||
| 201 | if (!is_string($id) && !($id instanceof ObjectId)) { |
||
| 202 | throw new Exception\InvalidArgument($id.' node id has to be a string or instance of \MongoDB\BSON\ObjectId'); |
||
| 203 | } |
||
| 204 | |||
| 205 | try { |
||
| 206 | if (is_string($id)) { |
||
| 207 | $id = new ObjectId($id); |
||
| 208 | } |
||
| 209 | } catch (\Exception $e) { |
||
| 210 | throw new Exception\InvalidArgument('invalid node id specified'); |
||
| 211 | } |
||
| 212 | |||
| 213 | $filter = [ |
||
| 214 | '_id' => $id, |
||
| 215 | ]; |
||
| 216 | |||
| 217 | if (NodeInterface::DELETED_EXCLUDE === $deleted) { |
||
| 218 | $filter['deleted'] = false; |
||
| 219 | } elseif (NodeInterface::DELETED_ONLY === $deleted) { |
||
| 220 | $filter['deleted'] = ['$type' => 9]; |
||
| 221 | } |
||
| 222 | |||
| 223 | $result = iterator_to_array($this->findNodesByFilterRecursiveChildren($filter, $deleted, 0, 1)); |
||
| 224 | |||
| 225 | if (count($result) === 0) { |
||
| 226 | throw new Exception\NotFound('node ['.$id.'] not found', Exception\NotFound::NODE_NOT_FOUND); |
||
| 227 | } |
||
| 228 | |||
| 229 | $node = array_shift($result); |
||
| 230 | if (null !== $class && !($node instanceof $class)) { |
||
| 231 | throw new Exception('node '.get_class($node).' is not instance of '.$class); |
||
| 232 | } |
||
| 233 | |||
| 234 | return $node; |
||
| 235 | } |
||
| 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('requested node not found', Exception\NotFound::NODE_NOT_FOUND); |
||
| 246 | } |
||
| 247 | |||
| 248 | return array_shift($result); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Load nodes by id. |
||
| 253 | * |
||
| 254 | * @deprecated |
||
| 255 | */ |
||
| 256 | public function findNodesById(array $id = [], ?string $class = null, int $deleted = NodeInterface::DELETED_INCLUDE): Generator |
||
| 257 | { |
||
| 258 | $find = []; |
||
| 259 | foreach ($id as $i) { |
||
| 260 | $find[] = new ObjectId($i); |
||
| 261 | } |
||
| 262 | |||
| 263 | $filter = [ |
||
| 264 | '_id' => ['$in' => $find], |
||
| 265 | ]; |
||
| 266 | |||
| 267 | switch ($deleted) { |
||
| 268 | case NodeInterface::DELETED_INCLUDE: |
||
| 269 | break; |
||
| 270 | case NodeInterface::DELETED_EXCLUDE: |
||
| 271 | $filter['deleted'] = false; |
||
| 272 | |||
| 273 | break; |
||
| 274 | case NodeInterface::DELETED_ONLY: |
||
| 275 | $filter['deleted'] = ['$type' => 9]; |
||
| 276 | |||
| 277 | break; |
||
| 278 | } |
||
| 279 | |||
| 280 | $result = $this->db->storage->find($filter); |
||
| 281 | |||
| 282 | $nodes = []; |
||
| 283 | foreach ($result as $node) { |
||
| 284 | try { |
||
| 285 | $return = $this->initNode($node); |
||
| 286 | |||
| 287 | if (in_array($return->getId(), $nodes)) { |
||
| 288 | continue; |
||
| 289 | } |
||
| 290 | |||
| 291 | $nodes[] = $return->getId(); |
||
| 292 | } catch (\Exception $e) { |
||
| 293 | $this->logger->error('remove node from result list, failed load node', [ |
||
| 294 | 'category' => get_class($this), |
||
| 295 | 'exception' => $e, |
||
| 296 | ]); |
||
| 297 | |||
| 298 | continue; |
||
| 299 | } |
||
| 300 | |||
| 301 | if (null !== $class && !($return instanceof $class)) { |
||
| 302 | throw new Exception('node is not an instance of '.$class); |
||
| 303 | } |
||
| 304 | |||
| 305 | yield $return; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Load nodes by id. |
||
| 311 | * |
||
| 312 | * @param null|mixed $class |
||
| 313 | * |
||
| 314 | * @deprecated |
||
| 315 | */ |
||
| 316 | public function getNodes(?array $id = null, $class = null, int $deleted = NodeInterface::DELETED_EXCLUDE): Generator |
||
| 317 | { |
||
| 318 | return $this->findNodesById($id, $class, $deleted); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Load node. |
||
| 323 | * |
||
| 324 | * @param null|mixed $id |
||
| 325 | * @param null|mixed $class |
||
| 326 | * |
||
| 327 | * @deprecated |
||
| 328 | */ |
||
| 329 | public function getNode($id = null, $class = null, bool $multiple = false, bool $allow_root = false, ?int $deleted = null): NodeInterface |
||
| 330 | { |
||
| 331 | if (empty($id)) { |
||
| 332 | if (true === $allow_root) { |
||
| 333 | return $this->getRoot(); |
||
| 334 | } |
||
| 335 | |||
| 336 | throw new Exception\InvalidArgument('invalid id given'); |
||
| 337 | } |
||
| 338 | |||
| 339 | if (null === $deleted) { |
||
| 340 | $deleted = NodeInterface::DELETED_INCLUDE; |
||
| 341 | } |
||
| 342 | |||
| 343 | if (true === $multiple && is_array($id)) { |
||
| 344 | return $this->findNodesById($id, $class, $deleted); |
||
| 345 | } |
||
| 346 | |||
| 347 | return $this->findNodeById($id, $class, $deleted); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Find node with custom filter. |
||
| 352 | */ |
||
| 353 | public function findNodeByFilter(array $filter): NodeInterface |
||
| 354 | { |
||
| 355 | $result = $this->db->storage->findOne($filter); |
||
| 356 | if (null === $result) { |
||
| 357 | throw new Exception\NotFound('node with custom filter was not found', Exception\NotFound::NODE_NOT_FOUND); |
||
| 358 | } |
||
| 359 | |||
| 360 | return $this->initNode($result); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Count. |
||
| 365 | */ |
||
| 366 | public function countNodes(array $filter = []): int |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Find nodes with custom filters. |
||
| 373 | */ |
||
| 374 | public function findNodesByFilter(array $filter, ?int $offset = null, ?int $limit = null): Generator |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Find nodes with custom filter recursive. |
||
| 399 | */ |
||
| 400 | public function findNodesByFilterRecursiveToArray(Collection $collection, array $filter = []): array |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get deleted nodes. |
||
| 428 | * |
||
| 429 | * Note this query excludes deleted nodes which have a deleted parent |
||
| 430 | */ |
||
| 431 | public function getTrash(array $query = [], ?int $offset = null, ?int $limit = null): Generator |
||
| 432 | { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Find nodes with custom filter recursive. |
||
| 508 | */ |
||
| 509 | public function findNodesByFilterRecursiveChildren(array $parent_filter, int $deleted, ?int $offset = null, ?int $limit = null, ?Collection $parent = null): Generator |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Find nodes with custom filter recursive. |
||
| 540 | */ |
||
| 541 | public function findNodesByFilterRecursive(Collection $collection, array $filter = [], ?int $offset = null, ?int $limit = null): Generator |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Get custom filtered children. |
||
| 567 | * |
||
| 568 | * @deprecated |
||
| 569 | */ |
||
| 570 | public function findNodesByFilterUser(int $deleted, array $filter, ?int $offset = null, ?int $limit = null): Generator |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Init node. |
||
| 594 | */ |
||
| 595 | public function initNode(array $node, ?Collection $parent = null): NodeInterface |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Find node with path. |
||
| 649 | */ |
||
| 650 | public function findNodeByPath(string $path = '', ?string $class = null): NodeInterface |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Prepare children filter. |
||
| 692 | */ |
||
| 693 | protected function prepareChildrenFilter(int $deleted) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Execute complex aggregation. |
||
| 734 | */ |
||
| 735 | protected function executeAggregation(array $query, ?int $offset = null, ?int $limit = null, ?Collection $parent = null): Generator |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Resolve shared node to reference or share depending who requested. |
||
| 767 | */ |
||
| 768 | protected function findReferenceNode(array $node): array |
||
| 802 | } |
||
| 803 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: