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 | * Storage. |
||
| 81 | * |
||
| 82 | * @var Storage |
||
| 83 | */ |
||
| 84 | protected $storage; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Acl. |
||
| 88 | * |
||
| 89 | * @var Acl |
||
| 90 | */ |
||
| 91 | protected $acl; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Node storage cache. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $cache = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Initialize. |
||
| 102 | * |
||
| 103 | * @param Server $server |
||
| 104 | * @param LoggerInterface $logger |
||
| 105 | * @param User $user |
||
| 106 | */ |
||
| 107 | public function __construct(Server $server, Database $db, Hook $hook, LoggerInterface $logger, Storage $storage, Acl $acl, ?User $user = null) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get user. |
||
| 120 | * |
||
| 121 | * @return User |
||
| 122 | */ |
||
| 123 | public function getUser(): ?User |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get server. |
||
| 130 | * |
||
| 131 | * @return Server |
||
| 132 | */ |
||
| 133 | public function getServer(): Server |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get database. |
||
| 140 | * |
||
| 141 | * @return Database |
||
| 142 | */ |
||
| 143 | public function getDatabase(): Database |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get root. |
||
| 150 | * |
||
| 151 | * @return Collection |
||
| 152 | */ |
||
| 153 | public function getRoot(): Collection |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get delta. |
||
| 168 | * |
||
| 169 | * @return Delta |
||
| 170 | */ |
||
| 171 | public function getDelta(): Delta |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Find raw node. |
||
| 182 | * |
||
| 183 | * @param ObjectId $id |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function findRawNode(ObjectId $id): array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Factory loader. |
||
| 206 | * |
||
| 207 | * @param ObjectId|string $id |
||
| 208 | * @param string $class Fore check node type |
||
| 209 | * @param int $deleted |
||
| 210 | * |
||
| 211 | * @return NodeInterface |
||
| 212 | */ |
||
| 213 | public function findNodeById($id, ?string $class = null, int $deleted = NodeInterface::DELETED_INCLUDE): NodeInterface |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Load node with path. |
||
| 268 | * |
||
| 269 | * @param string $path |
||
| 270 | * @param string $class Fore check node type |
||
| 271 | * |
||
| 272 | * @return NodeInterface |
||
| 273 | */ |
||
| 274 | public function findNodeByPath(string $path = '', ?string $class = null): NodeInterface |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Load nodes by id. |
||
| 301 | * |
||
| 302 | * @param array $id |
||
| 303 | * @param string $class Force check node type |
||
| 304 | * @param bool $deleted |
||
| 305 | * |
||
| 306 | * @return Generator |
||
| 307 | */ |
||
| 308 | public function findNodesById(array $id = [], ?string $class = null, int $deleted = NodeInterface::DELETED_INCLUDE): Generator |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Load nodes by id. |
||
| 348 | * |
||
| 349 | * @param array $path |
||
| 350 | * @param string $class Force check node type |
||
| 351 | * |
||
| 352 | * @return Generator |
||
| 353 | */ |
||
| 354 | public function findNodesByPath(array $path = [], ?string $class = null): Generator |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Load nodes by id. |
||
| 384 | * |
||
| 385 | * @param array $id |
||
| 386 | * @param array $path |
||
| 387 | * @param string $class Force set node type |
||
| 388 | * @param int $deleted |
||
| 389 | * |
||
| 390 | * @return Generator |
||
| 391 | */ |
||
| 392 | public function getNodes(?array $id = null, ?array $path = null, $class = null, int $deleted = NodeInterface::DELETED_EXCLUDE): Generator |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Load node. |
||
| 418 | * |
||
| 419 | * @param string $id |
||
| 420 | * @param string $path |
||
| 421 | * @param string $class Force set node type |
||
| 422 | * @param bool $multiple Allow $id to be an array |
||
| 423 | * @param bool $allow_root Allow instance of root collection |
||
| 424 | * @param bool $deleted How to handle deleted node |
||
| 425 | * |
||
| 426 | * @return NodeInterface |
||
| 427 | */ |
||
| 428 | public function getNode($id = null, $path = null, $class = null, bool $multiple = false, bool $allow_root = false, int $deleted = NodeInterface::DELETED_EXCLUDE): NodeInterface |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Find node with custom filter. |
||
| 462 | * |
||
| 463 | * @param array $filter |
||
| 464 | * |
||
| 465 | * @return NodeInterface |
||
| 466 | */ |
||
| 467 | public function findNodeByFilter(array $filter): NodeInterface |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Find nodes with custom filters. |
||
| 482 | * |
||
| 483 | * @param array $filter |
||
| 484 | * |
||
| 485 | * @return Generator |
||
| 486 | */ |
||
| 487 | public function findNodesByFilter(array $filter): Generator |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Get custom filtered children. |
||
| 506 | * |
||
| 507 | * @param int $deleted |
||
| 508 | * @param array $filter |
||
| 509 | * |
||
| 510 | * @return Generator |
||
| 511 | */ |
||
| 512 | public function findNodesByFilterUser(int $deleted, array $filter): Generator |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Init node. |
||
| 546 | * |
||
| 547 | * @param array $node |
||
| 548 | * |
||
| 549 | * @return NodeInterface |
||
| 550 | */ |
||
| 551 | public function initNode(array $node): NodeInterface |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Resolve shared node to reference or share depending who requested. |
||
| 605 | * |
||
| 606 | * @param array $node |
||
| 607 | * |
||
| 608 | * @return array |
||
| 609 | */ |
||
| 610 | protected function findReferenceNode(array $node): array |
||
| 650 | } |
||
| 651 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.