Complex classes like Server 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 Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Server |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Database. |
||
| 33 | * |
||
| 34 | * @var Database |
||
| 35 | */ |
||
| 36 | protected $db; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Storage. |
||
| 40 | * |
||
| 41 | * @var Storage |
||
| 42 | */ |
||
| 43 | protected $storage; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * LoggerInterface. |
||
| 47 | * |
||
| 48 | * @var LoggerInterface |
||
| 49 | */ |
||
| 50 | protected $logger; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Hook. |
||
| 54 | * |
||
| 55 | * @var Hook |
||
| 56 | */ |
||
| 57 | protected $hook; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Authenticated identity. |
||
| 61 | * |
||
| 62 | * @var User |
||
| 63 | */ |
||
| 64 | protected $identity; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Acl. |
||
| 68 | * |
||
| 69 | * @var Acl |
||
| 70 | */ |
||
| 71 | protected $acl; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Temporary store. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $temp_dir = '/tmp/balloon'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Max file version. |
||
| 82 | * |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | protected $max_file_version = 8; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Max file size. |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | protected $max_file_size = 17179869184; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Password policy. |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $password_policy = '/.*/'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Password hash. |
||
| 103 | * |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | protected $password_hash = PASSWORD_DEFAULT; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Server url. |
||
| 110 | * |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | protected $server_url = 'https://localhost'; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Initialize. |
||
| 117 | * |
||
| 118 | * @param Database $db |
||
| 119 | * @param Storage $storage |
||
| 120 | * @param LoggerInterface $logger |
||
| 121 | * @param Hook $hook |
||
| 122 | * @param Acl $acl |
||
| 123 | * @param iterable $config |
||
| 124 | */ |
||
| 125 | public function __construct(Database $db, Storage $storage, LoggerInterface $logger, Hook $hook, Acl $acl, ?Iterable $config = null) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set options. |
||
| 138 | * |
||
| 139 | * @param iterable $config |
||
| 140 | * |
||
| 141 | * @return Server |
||
| 142 | */ |
||
| 143 | public function setOptions(?Iterable $config = null): self |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get server url. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function getServerUrl(): string |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get temporary directory. |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getTempDir(): string |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get max file version. |
||
| 193 | * |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | public function getMaxFileVersion(): int |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get max file size. |
||
| 203 | * |
||
| 204 | * @return int |
||
| 205 | */ |
||
| 206 | public function getMaxFileSize(): int |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Filesystem factory. |
||
| 213 | * |
||
| 214 | * @return Filesystem |
||
| 215 | */ |
||
| 216 | public function getFilesystem(?User $user = null): Filesystem |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Verify group attributes. |
||
| 230 | * |
||
| 231 | * @param array $attributes |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function validateGroupAttributes(array $attributes): array |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Verify user attributes. |
||
| 298 | * |
||
| 299 | * @param array $attributes |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function validateUserAttributes(array $attributes): array |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Add user. |
||
| 370 | * |
||
| 371 | * @param string $username |
||
| 372 | * @param array $attributes |
||
| 373 | * |
||
| 374 | * @return ObjectId |
||
| 375 | */ |
||
| 376 | public function addUser(string $username, array $attributes = []): ObjectId |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Check if user exists. |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | public function userExists(string $username): bool |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Check if group exists. |
||
| 404 | * |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | public function groupExists(string $name): bool |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get user by id. |
||
| 414 | * |
||
| 415 | * @param ObjectId $id |
||
| 416 | * |
||
| 417 | * @return User |
||
| 418 | */ |
||
| 419 | public function getUserById(ObjectId $id): User |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get users by id. |
||
| 438 | * |
||
| 439 | * @param array $id |
||
| 440 | * |
||
| 441 | * @return Generator |
||
| 442 | */ |
||
| 443 | public function getUsersById(array $id): Generator |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Set Identity. |
||
| 467 | * |
||
| 468 | * @param Identity $identity |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | public function setIdentity(Identity $identity): bool |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get authenticated user. |
||
| 507 | * |
||
| 508 | * @return User |
||
| 509 | */ |
||
| 510 | public function getIdentity(): ?User |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Get user by name. |
||
| 517 | * |
||
| 518 | * @param string $name |
||
| 519 | * |
||
| 520 | * @return User |
||
| 521 | */ |
||
| 522 | public function getUserByName(string $name): User |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Get users. |
||
| 541 | * |
||
| 542 | * @param array $filter |
||
| 543 | * |
||
| 544 | * @return Generator |
||
| 545 | */ |
||
| 546 | public function getUsers(array $filter): Generator |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get groups. |
||
| 563 | * |
||
| 564 | * @param array $filter |
||
| 565 | * |
||
| 566 | * @return Generator |
||
| 567 | */ |
||
| 568 | public function getGroups(array $filter): Generator |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Get group by name. |
||
| 579 | * |
||
| 580 | * @param string $name |
||
| 581 | * |
||
| 582 | * @return Group |
||
| 583 | */ |
||
| 584 | public function getGroupByName(string $name): Group |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Get group by id. |
||
| 599 | * |
||
| 600 | * @param string $id |
||
| 601 | * |
||
| 602 | * @return User |
||
| 603 | */ |
||
| 604 | public function getGroupById(ObjectId $id): Group |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Add group. |
||
| 619 | * |
||
| 620 | * @param string $name |
||
| 621 | * @param array $member |
||
| 622 | * @param array $attributes |
||
| 623 | * |
||
| 624 | * @return ObjectId |
||
| 625 | */ |
||
| 626 | public function addGroup(string $name, array $member = [], array $attributes = []): ObjectId |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get user aggregation pipe. |
||
| 645 | * |
||
| 646 | * @return array |
||
| 647 | */ |
||
| 648 | protected function getUserAggregationPipes(): array |
||
| 668 | } |
||
| 669 |
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: