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 |
||
| 27 | class Server |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Database. |
||
| 31 | * |
||
| 32 | * @var Database |
||
| 33 | */ |
||
| 34 | protected $db; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Storage. |
||
| 38 | * |
||
| 39 | * @var Storage |
||
| 40 | */ |
||
| 41 | protected $storage; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * LoggerInterface. |
||
| 45 | * |
||
| 46 | * @var LoggerInterface |
||
| 47 | */ |
||
| 48 | protected $logger; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Hook. |
||
| 52 | * |
||
| 53 | * @var Hook |
||
| 54 | */ |
||
| 55 | protected $hook; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Authenticated identity. |
||
| 59 | * |
||
| 60 | * @var User |
||
| 61 | */ |
||
| 62 | protected $identity; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Acl. |
||
| 66 | * |
||
| 67 | * @var Acl |
||
| 68 | */ |
||
| 69 | protected $acl; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Temporary store. |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $temp_dir = '/tmp/balloon'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Max file version. |
||
| 80 | * |
||
| 81 | * @var int |
||
| 82 | */ |
||
| 83 | protected $max_file_version = 16; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Max file size. |
||
| 87 | * |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | protected $max_file_size = 17179869184; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Password policy. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $password_policy = '/.*/'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Password hash. |
||
| 101 | * |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | protected $password_hash = PASSWORD_DEFAULT; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Server url. |
||
| 108 | * |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | protected $server_url = 'https://localhost'; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Initialize. |
||
| 115 | * |
||
| 116 | * @param iterable $config |
||
| 117 | */ |
||
| 118 | public function __construct(Database $db, Storage $storage, LoggerInterface $logger, Hook $hook, Acl $acl, ?Iterable $config = null) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set options. |
||
| 131 | * |
||
| 132 | * @param iterable $config |
||
| 133 | * |
||
| 134 | * @return Server |
||
| 135 | */ |
||
| 136 | public function setOptions(?Iterable $config = null): self |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get server url. |
||
| 166 | */ |
||
| 167 | public function getServerUrl(): string |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get temporary directory. |
||
| 174 | */ |
||
| 175 | public function getTempDir(): string |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get max file version. |
||
| 182 | */ |
||
| 183 | public function getMaxFileVersion(): int |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get max file size. |
||
| 190 | */ |
||
| 191 | public function getMaxFileSize(): int |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Filesystem factory. |
||
| 198 | */ |
||
| 199 | public function getFilesystem(?User $user = null): Filesystem |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Verify group attributes. |
||
| 213 | */ |
||
| 214 | public function validateGroupAttributes(array $attributes): array |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Verify user attributes. |
||
| 285 | */ |
||
| 286 | public function validateUserAttributes(array $attributes): array |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Add user. |
||
| 386 | */ |
||
| 387 | public function addUser(string $username, array $attributes = []): ObjectId |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Check if user exists. |
||
| 406 | */ |
||
| 407 | public function usernameExists(string $username): bool |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Check if user exists. |
||
| 414 | */ |
||
| 415 | public function userExists(ObjectId $id): bool |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Check if user exists. |
||
| 422 | */ |
||
| 423 | public function groupExists(ObjectId $id): bool |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Get user by id. |
||
| 430 | */ |
||
| 431 | public function getUserById(ObjectId $id): User |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get users by id. |
||
| 450 | */ |
||
| 451 | public function getUsersById(array $id): Generator |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Set Identity. |
||
| 475 | */ |
||
| 476 | public function setIdentity(Identity $identity): bool |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Get authenticated user. |
||
| 511 | * |
||
| 512 | * @return User |
||
| 513 | */ |
||
| 514 | public function getIdentity(): ?User |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get user by name. |
||
| 521 | */ |
||
| 522 | public function getUserByName(string $name): User |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Count users. |
||
| 541 | */ |
||
| 542 | public function countUsers(array $filter): int |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Count groups. |
||
| 549 | */ |
||
| 550 | public function countGroups(array $filter): int |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Get users. |
||
| 557 | * |
||
| 558 | * @param int $offset |
||
| 559 | * @param int $limit |
||
| 560 | */ |
||
| 561 | public function getUsers(array $filter = [], ?int $offset = null, ?int $limit = null): Generator |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Get groups. |
||
| 588 | * |
||
| 589 | * @param int $offset |
||
| 590 | * @param int $limit |
||
| 591 | */ |
||
| 592 | public function getGroups(array $filter = [], ?int $offset = null, ?int $limit = null): Generator |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Get group by name. |
||
| 608 | */ |
||
| 609 | public function getGroupByName(string $name): Group |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Get group by id. |
||
| 624 | * |
||
| 625 | * @param string $id |
||
| 626 | */ |
||
| 627 | public function getGroupById(ObjectId $id): Group |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Add group. |
||
| 642 | */ |
||
| 643 | public function addGroup(string $name, array $member = [], array $attributes = []): ObjectId |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Get user aggregation pipe. |
||
| 663 | */ |
||
| 664 | protected function getUserAggregationPipes(): array |
||
| 684 | } |
||
| 685 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.