Complex classes like StorageConfig 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 StorageConfig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class StorageConfig implements IStorageConfig { |
||
37 | /** |
||
38 | * Storage config id |
||
39 | * |
||
40 | * @var int |
||
41 | */ |
||
42 | private $id; |
||
43 | |||
44 | /** |
||
45 | * Backend |
||
46 | * |
||
47 | * @var Backend |
||
48 | */ |
||
49 | private $backend; |
||
50 | |||
51 | /** |
||
52 | * Authentication mechanism |
||
53 | * |
||
54 | * @var AuthMechanism |
||
55 | */ |
||
56 | private $authMechanism; |
||
57 | |||
58 | /** |
||
59 | * Backend options |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | private $backendOptions = []; |
||
64 | |||
65 | /** |
||
66 | * Mount point path, relative to the user's "files" folder |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $mountPoint; |
||
71 | |||
72 | /** |
||
73 | * Storage status |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | private $status; |
||
78 | |||
79 | /** |
||
80 | * Status message |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | private $statusMessage; |
||
85 | |||
86 | /** |
||
87 | * Priority |
||
88 | * |
||
89 | * @var int |
||
90 | */ |
||
91 | private $priority; |
||
92 | |||
93 | /** |
||
94 | * List of users who have access to this storage |
||
95 | * |
||
96 | * @var array |
||
97 | */ |
||
98 | private $applicableUsers = []; |
||
99 | |||
100 | /** |
||
101 | * List of groups that have access to this storage |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | private $applicableGroups = []; |
||
106 | |||
107 | /** |
||
108 | * Mount-specific options |
||
109 | * |
||
110 | * @var array |
||
111 | */ |
||
112 | private $mountOptions = []; |
||
113 | |||
114 | /** |
||
115 | * Whether it's a personal or admin mount |
||
116 | * |
||
117 | * @var int |
||
118 | */ |
||
119 | private $type; |
||
120 | |||
121 | /** |
||
122 | * Creates a storage config |
||
123 | * |
||
124 | * @param int|null $id config id or null for a new config |
||
125 | */ |
||
126 | public function __construct($id = null) { |
||
130 | |||
131 | /** |
||
132 | * Returns the configuration id |
||
133 | * |
||
134 | * @return int |
||
135 | */ |
||
136 | public function getId() { |
||
139 | |||
140 | /** |
||
141 | * Sets the configuration id |
||
142 | * |
||
143 | * @param int $id configuration id |
||
144 | */ |
||
145 | public function setId($id) { |
||
148 | |||
149 | /** |
||
150 | * Returns mount point path relative to the user's |
||
151 | * "files" folder. |
||
152 | * |
||
153 | * @return string path |
||
154 | */ |
||
155 | public function getMountPoint() { |
||
158 | |||
159 | /** |
||
160 | * Sets mount point path relative to the user's |
||
161 | * "files" folder. |
||
162 | * The path will be normalized. |
||
163 | * |
||
164 | * @param string $mountPoint path |
||
165 | */ |
||
166 | public function setMountPoint($mountPoint) { |
||
169 | |||
170 | /** |
||
171 | * @return Backend |
||
172 | */ |
||
173 | public function getBackend() { |
||
176 | |||
177 | /** |
||
178 | * @param Backend $backend |
||
179 | */ |
||
180 | public function setBackend(Backend $backend) { |
||
183 | |||
184 | /** |
||
185 | * @return AuthMechanism |
||
186 | */ |
||
187 | public function getAuthMechanism() { |
||
190 | |||
191 | /** |
||
192 | * @param AuthMechanism $authMechanism |
||
193 | */ |
||
194 | public function setAuthMechanism(AuthMechanism $authMechanism) { |
||
197 | |||
198 | /** |
||
199 | * Returns the external storage backend-specific options |
||
200 | * |
||
201 | * @return array backend options |
||
202 | */ |
||
203 | public function getBackendOptions() { |
||
206 | |||
207 | /** |
||
208 | * Sets the external storage backend-specific options |
||
209 | * |
||
210 | * @param array $backendOptions backend options |
||
211 | */ |
||
212 | public function setBackendOptions($backendOptions) { |
||
238 | |||
239 | /** |
||
240 | * @param string $key |
||
241 | * @return mixed |
||
242 | */ |
||
243 | public function getBackendOption($key) { |
||
256 | |||
257 | /** |
||
258 | * @param string $key |
||
259 | * @param mixed $value |
||
260 | */ |
||
261 | public function setBackendOption($key, $value) { |
||
264 | |||
265 | /** |
||
266 | * Returns the mount priority |
||
267 | * |
||
268 | * @return int priority |
||
269 | */ |
||
270 | public function getPriority() { |
||
273 | |||
274 | /** |
||
275 | * Sets the mount priotity |
||
276 | * |
||
277 | * @param int $priority priority |
||
278 | */ |
||
279 | public function setPriority($priority) { |
||
282 | |||
283 | /** |
||
284 | * Returns the users for which to mount this storage |
||
285 | * |
||
286 | * @return array applicable users |
||
287 | */ |
||
288 | public function getApplicableUsers() { |
||
291 | |||
292 | /** |
||
293 | * Sets the users for which to mount this storage |
||
294 | * |
||
295 | * @param array|null $applicableUsers applicable users |
||
296 | */ |
||
297 | public function setApplicableUsers($applicableUsers) { |
||
303 | |||
304 | /** |
||
305 | * Returns the groups for which to mount this storage |
||
306 | * |
||
307 | * @return array applicable groups |
||
308 | */ |
||
309 | public function getApplicableGroups() { |
||
312 | |||
313 | /** |
||
314 | * Sets the groups for which to mount this storage |
||
315 | * |
||
316 | * @param array|null $applicableGroups applicable groups |
||
317 | */ |
||
318 | public function setApplicableGroups($applicableGroups) { |
||
324 | |||
325 | /** |
||
326 | * Returns the mount-specific options |
||
327 | * |
||
328 | * @return array mount specific options |
||
329 | */ |
||
330 | public function getMountOptions() { |
||
333 | |||
334 | /** |
||
335 | * Sets the mount-specific options |
||
336 | * |
||
337 | * @param array $mountOptions applicable groups |
||
338 | */ |
||
339 | public function setMountOptions($mountOptions) { |
||
345 | |||
346 | /** |
||
347 | * @param string $key |
||
348 | * @return mixed |
||
349 | */ |
||
350 | public function getMountOption($key) { |
||
356 | |||
357 | /** |
||
358 | * @param string $key |
||
359 | * @param mixed $value |
||
360 | */ |
||
361 | public function setMountOption($key, $value) { |
||
364 | |||
365 | /** |
||
366 | * Gets the storage status, whether the config worked last time |
||
367 | * |
||
368 | * @return int $status status |
||
369 | */ |
||
370 | public function getStatus() { |
||
373 | |||
374 | /** |
||
375 | * Gets the message describing the storage status |
||
376 | * |
||
377 | * @return string|null |
||
378 | */ |
||
379 | public function getStatusMessage() { |
||
382 | |||
383 | /** |
||
384 | * Sets the storage status, whether the config worked last time |
||
385 | * |
||
386 | * @param int $status status |
||
387 | * @param string|null $message optional message |
||
388 | */ |
||
389 | public function setStatus($status, $message = null) { |
||
393 | |||
394 | /** |
||
395 | * @return int self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl |
||
396 | */ |
||
397 | public function getType() { |
||
400 | |||
401 | /** |
||
402 | * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl |
||
403 | */ |
||
404 | public function setType($type) { |
||
407 | |||
408 | /** |
||
409 | * Serialize config to JSON |
||
410 | * |
||
411 | * @return array |
||
412 | */ |
||
413 | public function jsonSerialize() { |
||
444 | } |
||
445 |