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 = 1073741824; |
||
93 | |||
94 | /** |
||
95 | * Initialize. |
||
96 | * |
||
97 | * @param Database $db |
||
98 | * @param Storage $storage |
||
99 | * @param LoggerInterface $logger |
||
100 | * @param Hook $hook |
||
101 | * @param Acl $acl |
||
102 | * @param iterable $config |
||
103 | */ |
||
104 | public function __construct(Database $db, Storage $storage, LoggerInterface $logger, Hook $hook, Acl $acl, ?Iterable $config = null) |
||
114 | |||
115 | /** |
||
116 | * Set options. |
||
117 | * |
||
118 | * @param iterable $config |
||
119 | * |
||
120 | * @return Server |
||
121 | */ |
||
122 | public function setOptions(?Iterable $config = null): self |
||
146 | |||
147 | /** |
||
148 | * Get temporary directory. |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getTempDir(): string |
||
156 | |||
157 | /** |
||
158 | * Get max file version. |
||
159 | * |
||
160 | * @return int |
||
161 | */ |
||
162 | public function getMaxFileVersion(): int |
||
166 | |||
167 | /** |
||
168 | * Get max file size. |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | public function getMaxFileSize(): int |
||
176 | |||
177 | /** |
||
178 | * Filesystem factory. |
||
179 | * |
||
180 | * @return Filesystem |
||
181 | */ |
||
182 | public function getFilesystem(?User $user = null): Filesystem |
||
193 | |||
194 | /** |
||
195 | * Add user. |
||
196 | * |
||
197 | * @param string $username |
||
198 | * @param string $password |
||
199 | * @param array $attributes |
||
200 | * |
||
201 | * @return ObjectId |
||
202 | */ |
||
203 | public function addUser(string $username, ?string $password = null, array $attributes = []): ObjectId |
||
225 | |||
226 | /** |
||
227 | * Check if user exists. |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function userExists(string $username): bool |
||
235 | |||
236 | /** |
||
237 | * Check if group exists. |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function groupExists(string $name): bool |
||
245 | |||
246 | /** |
||
247 | * Get user by id. |
||
248 | * |
||
249 | * @param ObjectId $id |
||
250 | * |
||
251 | * @return User |
||
252 | */ |
||
253 | public function getUserById(ObjectId $id): User |
||
265 | |||
266 | /** |
||
267 | * Get users by id. |
||
268 | * |
||
269 | * @param array $id |
||
270 | * |
||
271 | * @return Generator |
||
272 | */ |
||
273 | public function getUsersById(array $id): Generator |
||
290 | |||
291 | /** |
||
292 | * Set Identity. |
||
293 | * |
||
294 | * @param Identity $identity |
||
295 | * |
||
296 | * @return bool |
||
297 | */ |
||
298 | public function setIdentity(Identity $identity): bool |
||
321 | |||
322 | /** |
||
323 | * Get authenticated user. |
||
324 | * |
||
325 | * @return User |
||
326 | */ |
||
327 | public function getIdentity(): ?User |
||
331 | |||
332 | /** |
||
333 | * Get user by name. |
||
334 | * |
||
335 | * @param string $name |
||
336 | * |
||
337 | * @return User |
||
338 | */ |
||
339 | public function getUserByName(string $name): User |
||
351 | |||
352 | /** |
||
353 | * Get users. |
||
354 | * |
||
355 | * @param array $filter |
||
356 | * |
||
357 | * @return Generator |
||
358 | */ |
||
359 | public function getUsers(array $filter): Generator |
||
367 | |||
368 | /** |
||
369 | * Get groups. |
||
370 | * |
||
371 | * @param array $filter |
||
372 | * |
||
373 | * @return Generator |
||
374 | */ |
||
375 | public function getGroups(array $filter): Generator |
||
383 | |||
384 | /** |
||
385 | * Get group by name. |
||
386 | * |
||
387 | * @param string $name |
||
388 | * |
||
389 | * @return Group |
||
390 | */ |
||
391 | public function getGroupByName(string $name): Group |
||
403 | |||
404 | /** |
||
405 | * Get group by id. |
||
406 | * |
||
407 | * @param string $id |
||
408 | * |
||
409 | * @return User |
||
410 | */ |
||
411 | public function getGroupById(ObjectId $id): Group |
||
423 | |||
424 | /** |
||
425 | * Add group. |
||
426 | * |
||
427 | * @param string $name |
||
428 | * @param array $member |
||
429 | * @param array $attributes |
||
430 | * |
||
431 | * @return ObjectId |
||
432 | */ |
||
433 | public function addGroup(string $name, array $member, array $attributes = []): ObjectId |
||
465 | } |
||
466 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: