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 = 8; |
||
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 Database $db |
||
117 | * @param Storage $storage |
||
118 | * @param LoggerInterface $logger |
||
119 | * @param Hook $hook |
||
120 | * @param Acl $acl |
||
121 | * @param iterable $config |
||
122 | */ |
||
123 | public function __construct(Database $db, Storage $storage, LoggerInterface $logger, Hook $hook, Acl $acl, ?Iterable $config = null) |
||
133 | |||
134 | /** |
||
135 | * Set options. |
||
136 | * |
||
137 | * @param iterable $config |
||
138 | * |
||
139 | * @return Server |
||
140 | */ |
||
141 | public function setOptions(?Iterable $config = null): self |
||
168 | |||
169 | /** |
||
170 | * Get server url. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getServerUrl(): string |
||
178 | |||
179 | /** |
||
180 | * Get temporary directory. |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getTempDir(): string |
||
188 | |||
189 | /** |
||
190 | * Get max file version. |
||
191 | * |
||
192 | * @return int |
||
193 | */ |
||
194 | public function getMaxFileVersion(): int |
||
198 | |||
199 | /** |
||
200 | * Get max file size. |
||
201 | * |
||
202 | * @return int |
||
203 | */ |
||
204 | public function getMaxFileSize(): int |
||
208 | |||
209 | /** |
||
210 | * Filesystem factory. |
||
211 | * |
||
212 | * @return Filesystem |
||
213 | */ |
||
214 | public function getFilesystem(?User $user = null): Filesystem |
||
225 | |||
226 | /** |
||
227 | * Verify group attributes. |
||
228 | * |
||
229 | * @param array $attributes |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public function validateGroupAttributes(array $attributes): array |
||
301 | |||
302 | /** |
||
303 | * Verify user attributes. |
||
304 | * |
||
305 | * @param array $attributes |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | public function validateUserAttributes(array $attributes): array |
||
406 | |||
407 | /** |
||
408 | * Add user. |
||
409 | * |
||
410 | * @param string $username |
||
411 | * @param array $attributes |
||
412 | * |
||
413 | * @return ObjectId |
||
414 | */ |
||
415 | public function addUser(string $username, array $attributes = []): ObjectId |
||
431 | |||
432 | /** |
||
433 | * Check if user exists. |
||
434 | * |
||
435 | * @return bool |
||
436 | */ |
||
437 | public function usernameExists(string $username): bool |
||
441 | |||
442 | /** |
||
443 | * Check if user exists. |
||
444 | * |
||
445 | * @return bool |
||
446 | */ |
||
447 | public function userExists(ObjectId $id): bool |
||
451 | |||
452 | /** |
||
453 | * Check if user exists. |
||
454 | * |
||
455 | * @return bool |
||
456 | */ |
||
457 | public function groupExists(ObjectId $id): bool |
||
461 | |||
462 | /** |
||
463 | * Get user by id. |
||
464 | * |
||
465 | * @param ObjectId $id |
||
466 | * |
||
467 | * @return User |
||
468 | */ |
||
469 | public function getUserById(ObjectId $id): User |
||
485 | |||
486 | /** |
||
487 | * Get users by id. |
||
488 | * |
||
489 | * @param array $id |
||
490 | * |
||
491 | * @return Generator |
||
492 | */ |
||
493 | public function getUsersById(array $id): Generator |
||
514 | |||
515 | /** |
||
516 | * Set Identity. |
||
517 | * |
||
518 | * @param Identity $identity |
||
519 | * |
||
520 | * @return bool |
||
521 | */ |
||
522 | public function setIdentity(Identity $identity): bool |
||
554 | |||
555 | /** |
||
556 | * Get authenticated user. |
||
557 | * |
||
558 | * @return User |
||
559 | */ |
||
560 | public function getIdentity(): ?User |
||
564 | |||
565 | /** |
||
566 | * Get user by name. |
||
567 | * |
||
568 | * @param string $name |
||
569 | * |
||
570 | * @return User |
||
571 | */ |
||
572 | public function getUserByName(string $name): User |
||
588 | |||
589 | /** |
||
590 | * Count users. |
||
591 | * |
||
592 | * @param array $filter |
||
593 | * |
||
594 | * @return int |
||
595 | */ |
||
596 | public function countUsers(array $filter): int |
||
600 | |||
601 | /** |
||
602 | * Count groups. |
||
603 | * |
||
604 | * @param array $filter |
||
605 | * |
||
606 | * @return int |
||
607 | */ |
||
608 | public function countGroups(array $filter): int |
||
612 | |||
613 | /** |
||
614 | * Get users. |
||
615 | * |
||
616 | * @param array $filter |
||
617 | * @param int $offset |
||
618 | * @param int $limit |
||
619 | * |
||
620 | * @return Generator |
||
621 | */ |
||
622 | public function getUsers(array $filter, ?int $offset = null, ?int $limit = null): Generator |
||
646 | |||
647 | /** |
||
648 | * Get groups. |
||
649 | * |
||
650 | * @param array $filter |
||
651 | * @param int $offset |
||
652 | * @param int $limit |
||
653 | * |
||
654 | * @return Generator |
||
655 | */ |
||
656 | public function getGroups(array $filter, ?int $offset = null, ?int $limit = null): Generator |
||
669 | |||
670 | /** |
||
671 | * Get group by name. |
||
672 | * |
||
673 | * @param string $name |
||
674 | * |
||
675 | * @return Group |
||
676 | */ |
||
677 | public function getGroupByName(string $name): Group |
||
689 | |||
690 | /** |
||
691 | * Get group by id. |
||
692 | * |
||
693 | * @param string $id |
||
694 | * |
||
695 | * @return Group |
||
696 | */ |
||
697 | public function getGroupById(ObjectId $id): Group |
||
709 | |||
710 | /** |
||
711 | * Add group. |
||
712 | * |
||
713 | * @param string $name |
||
714 | * @param array $member |
||
715 | * @param array $attributes |
||
716 | * |
||
717 | * @return ObjectId |
||
718 | */ |
||
719 | public function addGroup(string $name, array $member = [], array $attributes = []): ObjectId |
||
736 | |||
737 | /** |
||
738 | * Get user aggregation pipe. |
||
739 | * |
||
740 | * @return array |
||
741 | */ |
||
742 | protected function getUserAggregationPipes(): array |
||
762 | } |
||
763 |
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.