Complex classes like ResourceDOAbstract 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 ResourceDOAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class ResourceDOAbstract implements ResourceDOInterface |
||
9 | { |
||
10 | const TYPE = ''; |
||
11 | const TOKEN_BASEDIRECTORY = 'basedirectory'; |
||
12 | const TOKEN_NAMESPACE = 'namespace'; |
||
13 | const TOKEN_TYPE = 'type'; |
||
14 | const TOKEN_SHARD_VARIANT = 'shard_variant'; |
||
15 | const TOKEN_VARIANT = 'variant'; |
||
16 | const TOKEN_VERSION = 'version'; |
||
17 | const TOKEN_SHARD_FILENAME = 'shard_filename'; |
||
18 | const SHARD_SLICE_LENGTH = 3; |
||
19 | |||
20 | protected $uuid; |
||
21 | protected $namespace = ''; |
||
22 | protected $name = ''; |
||
23 | protected $nameAlternative = self::DEFAULT_NAME_ALTERNATIVE; |
||
24 | |||
25 | /** |
||
26 | * The body of resource that can be passed through HTTP POST Body while creation for some resources types |
||
27 | * For example, the body can be used as a full text than need to use for resource creation |
||
28 | * when it can't be sent in URI and does need to change URI at all (instead of 'alt' argument) |
||
29 | * The body MUST NOT change the UUID, but body changing can initialise uuid |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $body = ''; |
||
33 | protected $type = self::TYPE; |
||
34 | protected $variant = self::DEFAULT_VARIANT; |
||
35 | protected $version = self::DEFAULT_VERSION; |
||
36 | protected $author = ''; |
||
37 | |||
38 | /** |
||
39 | * true if resource file is just created (or should be) |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $new = false; |
||
43 | |||
44 | /** |
||
45 | * true if exists resource needs to be recreated |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $recreate = false; |
||
49 | |||
50 | /** |
||
51 | * Path to base directory (without dynamic path part) |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $baseDirectory = self::DEFAULT_BASE_DIRECTORY; |
||
55 | protected $filePath = ''; |
||
56 | |||
57 | /** |
||
58 | * List of object properties that should not be iterable (denied for the usage in response) |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $notIterable = [ |
||
62 | 'itemPosition', |
||
63 | 'notIterable', |
||
64 | 'baseDirectory', |
||
65 | 'filePath', |
||
66 | 'author', |
||
67 | ]; |
||
68 | |||
69 | protected $itemPosition = 0; |
||
70 | |||
71 | 75 | public function reset() |
|
89 | |||
90 | 75 | public function __construct() |
|
98 | |||
99 | 47 | protected function setFilePath() |
|
103 | |||
104 | /** |
||
105 | * /type/variant/version/[other-type-specified/]uuid.type |
||
106 | * /mp3/default/1/22af64.mp3 |
||
107 | * /mp3/ivona/0/22af64.mp3 |
||
108 | */ |
||
109 | 57 | public function generateFilePath() |
|
119 | |||
120 | /** |
||
121 | * Map of the resource directory elements. |
||
122 | * For example, you can use keys with the strtok() method. Or for routes buildings. |
||
123 | * |
||
124 | * @return array |
||
125 | * @see strtok() |
||
126 | * @example strtok($relative_path, '/'); |
||
127 | */ |
||
128 | 58 | public function getDirectoryTokens() |
|
142 | |||
143 | /** |
||
144 | * Note: Uuid is not really unique, if you want full unique identifier, use hash sum from the full path, for example |
||
145 | * @return mixed |
||
146 | */ |
||
147 | 63 | public function getUuid() |
|
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | 42 | public function getName() |
|
162 | |||
163 | /** |
||
164 | * @param string $name |
||
165 | * @return ResourceDOInterface |
||
166 | */ |
||
167 | 43 | public function setName($name) |
|
175 | |||
176 | /** |
||
177 | * @return string |
||
178 | */ |
||
179 | 59 | public function getNamespace() |
|
183 | |||
184 | /** |
||
185 | * @param string $namespace |
||
186 | * @return ResourceDOInterface |
||
187 | */ |
||
188 | 16 | public function setNamespace($namespace = '') |
|
196 | |||
197 | /** |
||
198 | * @return string |
||
199 | */ |
||
200 | 1 | public function getNameAlternative() |
|
204 | |||
205 | /** |
||
206 | * @param string $nameAlternative |
||
207 | * @return ResourceDOInterface |
||
208 | */ |
||
209 | 18 | public function setNameAlternative($nameAlternative = '') |
|
217 | |||
218 | /** |
||
219 | * @return string |
||
220 | */ |
||
221 | public function getBody() |
||
225 | |||
226 | /** |
||
227 | * @param string $body |
||
228 | * @return ResourceDOInterface |
||
229 | */ |
||
230 | public function setBody($body = '') |
||
238 | |||
239 | /** |
||
240 | * @return string |
||
241 | */ |
||
242 | 64 | public function getType() |
|
246 | /** |
||
247 | * @param string $type |
||
248 | * @return ResourceDOInterface |
||
249 | */ |
||
250 | 40 | public function setType($type) |
|
257 | |||
258 | /** |
||
259 | * @return string |
||
260 | */ |
||
261 | 59 | public function getVariant() |
|
269 | |||
270 | /** |
||
271 | * @param string $variant |
||
272 | * @return ResourceDOInterface |
||
273 | */ |
||
274 | 15 | public function setVariant($variant = self::DEFAULT_VARIANT) |
|
281 | |||
282 | /** |
||
283 | * @return int |
||
284 | */ |
||
285 | 60 | public function getVersion() |
|
292 | |||
293 | /** |
||
294 | * @param int $version |
||
295 | * @return ResourceDOInterface |
||
296 | */ |
||
297 | 31 | public function setVersion($version = self::DEFAULT_VERSION) |
|
304 | |||
305 | /** |
||
306 | * @return string |
||
307 | */ |
||
308 | 1 | public function getAuthor() |
|
312 | |||
313 | /** |
||
314 | * @param string $author |
||
315 | * @return ResourceDOInterface |
||
316 | */ |
||
317 | 9 | public function setAuthor($author) |
|
323 | |||
324 | /** |
||
325 | * @return string |
||
326 | */ |
||
327 | 45 | public function getFilePath() |
|
331 | |||
332 | /** |
||
333 | * @return mixed |
||
334 | */ |
||
335 | 59 | public function getBaseDirectory() |
|
339 | |||
340 | /** |
||
341 | * @param string $dir |
||
342 | * @return ResourceDOInterface |
||
343 | */ |
||
344 | 42 | public function setBaseDirectory($dir = self::DEFAULT_BASE_DIRECTORY) |
|
356 | |||
357 | /** |
||
358 | * @return boolean |
||
359 | */ |
||
360 | 1 | public function isNew() |
|
364 | |||
365 | /** |
||
366 | * @param boolean $new |
||
367 | * @return ResourceDOAbstract |
||
368 | */ |
||
369 | 5 | public function setNew($new = false) |
|
375 | |||
376 | /** |
||
377 | * @return boolean |
||
378 | */ |
||
379 | 1 | public function isRecreate() |
|
383 | |||
384 | /** |
||
385 | * @param boolean $recreate |
||
386 | * @return ResourceDOAbstract |
||
387 | */ |
||
388 | 5 | public function setRecreate($recreate = false) |
|
394 | |||
395 | 4 | public function rewind() |
|
399 | |||
400 | 4 | public function current() |
|
414 | |||
415 | 4 | public function key() |
|
419 | |||
420 | 4 | public function next() |
|
424 | |||
425 | 4 | public function valid() |
|
433 | |||
434 | /** |
||
435 | * @return array |
||
436 | */ |
||
437 | 4 | public function toArray() |
|
448 | |||
449 | /** |
||
450 | * Unique resource identifier for ACL |
||
451 | * @return mixed |
||
452 | * @see \Zend\Permissions\Acl\Resource\ResourceInterface::getResourceId |
||
453 | * @see getFilePath |
||
454 | */ |
||
455 | public function getResourceId() |
||
459 | } |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.