Complex classes like Package 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 Package, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Package |
||
| 34 | { |
||
| 35 | private $config; |
||
| 36 | private $filepath; |
||
| 37 | private $filepathNoExt; |
||
| 38 | private $filename; |
||
| 39 | private $filenameNoExt; |
||
| 40 | private $metafile; |
||
| 41 | private $wizfile; |
||
| 42 | private $nowizfile; |
||
| 43 | private $metadata; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param \SSpkS\Config $config Config object |
||
| 47 | * @param string $filename Filename of SPK file |
||
| 48 | */ |
||
| 49 | 17 | public function __construct(\SSpkS\Config $config, $filename) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Getter magic method. |
||
| 69 | * |
||
| 70 | * @param string $name Name of requested value. |
||
| 71 | * @return mixed Requested value. |
||
| 72 | */ |
||
| 73 | 10 | public function __get($name) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Setter magic method. |
||
| 81 | * |
||
| 82 | * @param string $name Name of variable to set. |
||
| 83 | * @param mixed $value Value to set. |
||
| 84 | */ |
||
| 85 | 5 | public function __set($name, $value) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Isset feature magic method. |
||
| 93 | * |
||
| 94 | * @param string $name Name of requested value. |
||
| 95 | * @return bool TRUE if value exists, FALSE otherwise. |
||
| 96 | */ |
||
| 97 | 6 | public function __isset($name) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Unset feature magic method. |
||
| 105 | * |
||
| 106 | * @param string $name Name of value to unset. |
||
| 107 | */ |
||
| 108 | 1 | public function __unset($name) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Parses boolean value ('yes', '1', 'true') into |
||
| 116 | * boolean type. |
||
| 117 | * |
||
| 118 | * @param mixed $value Input value |
||
| 119 | * @return bool Boolean interpretation of $value. |
||
| 120 | */ |
||
| 121 | 8 | public function parseBool($value) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Checks if given property $prop exists and converts it |
||
| 128 | * into a boolean value. |
||
| 129 | * |
||
| 130 | * @param string $prop Property to convert |
||
| 131 | */ |
||
| 132 | 12 | private function fixBoolIfExist($prop) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Gathers metadata from package. Extracts INFO file if neccessary. |
||
| 141 | */ |
||
| 142 | 12 | private function collectMetadata() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Returns metadata for this package. |
||
| 178 | * |
||
| 179 | * @return array Metadata. |
||
| 180 | */ |
||
| 181 | 3 | public function getMetadata() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Extracts $inPkgName from package to $targetFile, if it doesn't |
||
| 189 | * already exist. Needs the phar.so extension and allow_url_fopen. |
||
| 190 | * |
||
| 191 | * @param string $inPkgName Filename in package |
||
| 192 | * @param string $targetFile Path to destination |
||
| 193 | * @throws \Exception if the file couldn't get extracted. |
||
| 194 | * @return bool TRUE if successful or no action needed. |
||
| 195 | */ |
||
| 196 | 13 | public function extractIfMissing($inPkgName, $targetFile) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Returns a true if the package contains WIZARD_UIFILES. |
||
| 230 | * |
||
| 231 | * @return bool Package has a wizard |
||
| 232 | */ |
||
| 233 | 12 | public function hasWizardDir() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Returns a list of thumbnails for the specified package. |
||
| 261 | * |
||
| 262 | * @param string $pathPrefix Prefix to put before file path |
||
| 263 | * @return array List of thumbnail urls |
||
| 264 | */ |
||
| 265 | 12 | public function getThumbnails($pathPrefix = '') |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Returns a list of screenshots for the specified package. |
||
| 305 | * |
||
| 306 | * @param string $pathPrefix Prefix to put before file path |
||
| 307 | * @return array List of screenshots |
||
| 308 | */ |
||
| 309 | 12 | public function getSnapshots($pathPrefix = '') |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Checks compatibility to the given $arch-itecture. |
||
| 331 | * |
||
| 332 | * @param string $arch Architecture to check against (or "noarch") |
||
| 333 | * @return bool TRUE if compatible, otherwise FALSE. |
||
| 334 | */ |
||
| 335 | 1 | public function isCompatibleToArch($arch) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Checks compatibility to the given firmware $version. |
||
| 345 | * |
||
| 346 | * @param string $version Target firmware version. |
||
| 347 | * @return bool TRUE if compatible, otherwise FALSE. |
||
| 348 | */ |
||
| 349 | 1 | public function isCompatibleToFirmware($version) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Checks if this package is a beta version or not. |
||
| 357 | * |
||
| 358 | * @return bool TRUE if this is a beta version, FALSE otherwise. |
||
| 359 | */ |
||
| 360 | 1 | public function isBeta() |
|
| 365 | } |
||
| 366 |