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 |
||
| 10 | abstract class ResourceDOAbstract implements ResourceDOInterface, \Iterator, ResourceInterface |
||
| 11 | { |
||
| 12 | const TYPE = ''; |
||
| 13 | const TOKEN_BASEDIRECTORY = 'basedirectory'; |
||
| 14 | const TOKEN_NAMESPACE = 'namespace'; |
||
| 15 | const TOKEN_TYPE = 'type'; |
||
| 16 | const TOKEN_SHARD_VARIANT = 'shard_variant'; |
||
| 17 | const TOKEN_VARIANT = 'variant'; |
||
| 18 | const TOKEN_VERSION = 'version'; |
||
| 19 | const TOKEN_SHARD_FILENAME = 'shard_filename'; |
||
| 20 | const SHARD_SLICE_LENGTH = 3; |
||
| 21 | |||
| 22 | protected $uuid; |
||
| 23 | protected $namespace = ''; |
||
| 24 | protected $name = ''; |
||
| 25 | protected $nameAlternative = ''; |
||
| 26 | protected $type = self::TYPE; |
||
| 27 | protected $variant = self::DEFAULT_VARIANT; |
||
| 28 | protected $version = self::DEFAULT_VERSION; |
||
| 29 | protected $author = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * true if resource file is just created (or should be) |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $new = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * true if exists resource needs to be recreated |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | protected $recreate = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Path to base directory (without dynamic path part) |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $baseDirectory = self::DEFAULT_BASE_DIRECTORY; |
||
| 48 | protected $filePath = ''; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * List of object properties that should not be iterable (denied for the usage in response) |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $notIterable = [ |
||
| 55 | 'itemPosition', |
||
| 56 | 'notIterable', |
||
| 57 | 'baseDirectory', |
||
| 58 | 'filePath', |
||
| 59 | 'author', |
||
| 60 | ]; |
||
| 61 | |||
| 62 | protected $itemPosition = 0; |
||
| 63 | |||
| 64 | 75 | public function reset() |
|
| 82 | |||
| 83 | 75 | public function __construct() |
|
| 91 | |||
| 92 | 47 | protected function setFilePath() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * /type/variant/version/[other-type-specified/]uuid.type |
||
| 99 | * /mp3/default/1/22af64.mp3 |
||
| 100 | * /mp3/ivona/0/22af64.mp3 |
||
| 101 | */ |
||
| 102 | 57 | public function generateFilePath() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Map of the resource directory elements. |
||
| 115 | * For example, you can use keys with the strtok() method. Or for routes buildings. |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | * @see strtok() |
||
| 119 | * @example strtok($relative_path, '/'); |
||
| 120 | */ |
||
| 121 | 58 | public function getDirectoryTokens() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Note: Uuid is not really unique, if you want full unique identifier, use hash sum from the full path, for example |
||
| 136 | * @return mixed |
||
| 137 | */ |
||
| 138 | 63 | public function getUuid() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 42 | public function getName() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $name |
||
| 156 | * @return ResourceDOInterface |
||
| 157 | */ |
||
| 158 | 43 | public function setName($name) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | 59 | public function getNamespace() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $namespace |
||
| 177 | * @return ResourceDOInterface |
||
| 178 | */ |
||
| 179 | 16 | public function setNamespace($namespace = '') |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | 1 | public function getNameAlternative() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $nameAlternative |
||
| 198 | * @return ResourceDOInterface |
||
| 199 | */ |
||
| 200 | 18 | public function setNameAlternative($nameAlternative = '') |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 64 | public function getType() |
|
| 216 | /** |
||
| 217 | * @param string $type |
||
| 218 | * @return ResourceDOInterface |
||
| 219 | */ |
||
| 220 | 40 | public function setType($type) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | 59 | public function getVariant() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $variant |
||
| 242 | * @return ResourceDOInterface |
||
| 243 | */ |
||
| 244 | 15 | public function setVariant($variant = self::DEFAULT_VARIANT) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @return int |
||
| 254 | */ |
||
| 255 | 60 | public function getVersion() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param int $version |
||
| 265 | * @return ResourceDOInterface |
||
| 266 | */ |
||
| 267 | 31 | public function setVersion($version = self::DEFAULT_VERSION) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 1 | public function getAuthor() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $author |
||
| 285 | * @return ResourceDOInterface |
||
| 286 | */ |
||
| 287 | 9 | public function setAuthor($author) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | 45 | public function getFilePath() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | 59 | public function getBaseDirectory() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $dir |
||
| 312 | * @return ResourceDOInterface |
||
| 313 | */ |
||
| 314 | 42 | public function setBaseDirectory($dir = self::DEFAULT_BASE_DIRECTORY) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @return boolean |
||
| 329 | */ |
||
| 330 | 1 | public function isNew() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param boolean $new |
||
| 337 | * @return ResourceDOAbstract |
||
| 338 | */ |
||
| 339 | 5 | public function setNew($new = false) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @return boolean |
||
| 348 | */ |
||
| 349 | 1 | public function isRecreate() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param boolean $recreate |
||
| 356 | * @return ResourceDOAbstract |
||
| 357 | */ |
||
| 358 | 5 | public function setRecreate($recreate = false) |
|
| 364 | |||
| 365 | 4 | public function rewind() |
|
| 369 | |||
| 370 | 4 | public function current() |
|
| 384 | |||
| 385 | 4 | public function key() |
|
| 389 | |||
| 390 | 4 | public function next() |
|
| 394 | |||
| 395 | 4 | public function valid() |
|
| 403 | |||
| 404 | 4 | public function toArray() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Unique resource identifier for ACL |
||
| 418 | * @return mixed |
||
| 419 | * @see \Zend\Permissions\Acl\Resource\ResourceInterface::getResourceId |
||
| 420 | * @see getFilePath |
||
| 421 | */ |
||
| 422 | public function getResourceId() |
||
| 426 | } |