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) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Getter magic method. |
||
| 71 | * |
||
| 72 | * @param string $name Name of requested value. |
||
| 73 | * @return mixed Requested value. |
||
| 74 | */ |
||
| 75 | 9 | public function __get($name) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Setter magic method. |
||
| 82 | * |
||
| 83 | * @param string $name Name of variable to set. |
||
| 84 | * @param mixed $value Value to set. |
||
| 85 | */ |
||
| 86 | 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 | 5 | public function __isset($name) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Unset feature magic method. |
||
| 104 | * |
||
| 105 | * @param string $name Name of value to unset. |
||
| 106 | */ |
||
| 107 | 1 | public function __unset($name) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Parses boolean value ('yes', '1', 'true') into |
||
| 114 | * boolean type. |
||
| 115 | * |
||
| 116 | * @param mixed $value Input value |
||
| 117 | * @return bool Boolean interpretation of $value. |
||
| 118 | */ |
||
| 119 | 15 | public function parseBool($value) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Checks if given property $prop exists and converts it |
||
| 126 | * into a boolean value. |
||
| 127 | * |
||
| 128 | * @param string $prop Property to convert |
||
| 129 | */ |
||
| 130 | 15 | private function fixBoolIfExist($prop) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Gathers metadata from package. Extracts INFO file if neccessary. |
||
| 139 | */ |
||
| 140 | 15 | private function collectMetadata() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Returns metadata for this package. |
||
| 176 | * |
||
| 177 | * @return array Metadata. |
||
| 178 | */ |
||
| 179 | 3 | public function getMetadata() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Extracts $inPkgName from package to $targetFile, if it doesn't |
||
| 186 | * already exist. Needs the phar.so extension and allow_url_fopen. |
||
| 187 | * |
||
| 188 | * @param string $inPkgName Filename in package |
||
| 189 | * @param string $targetFile Path to destination |
||
| 190 | * @throws \Exception if the file couldn't get extracted. |
||
| 191 | * @return bool TRUE if successful or no action needed. |
||
| 192 | */ |
||
| 193 | 15 | public function extractIfMissing($inPkgName, $targetFile) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Returns a true if the package contains WIZARD_UIFILES. |
||
| 227 | * |
||
| 228 | * @return bool Package has a wizard |
||
| 229 | */ |
||
| 230 | 15 | public function hasWizardDir() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Returns a list of thumbnails for the specified package. |
||
| 258 | * |
||
| 259 | * @param string $pathPrefix Prefix to put before file path |
||
| 260 | * @return array List of thumbnail urls |
||
| 261 | */ |
||
| 262 | 15 | public function getThumbnails($pathPrefix = '') |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Returns a list of screenshots for the specified package. |
||
| 301 | * |
||
| 302 | * @param string $pathPrefix Prefix to put before file path |
||
| 303 | * @return array List of screenshots |
||
| 304 | */ |
||
| 305 | 15 | public function getSnapshots($pathPrefix = '') |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Checks compatibility to the given $arch-itecture. |
||
| 327 | * |
||
| 328 | * @param string $arch Architecture to check against (or "noarch") |
||
| 329 | * @return bool TRUE if compatible, otherwise FALSE. |
||
| 330 | */ |
||
| 331 | 1 | public function isCompatibleToArch($arch) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Checks compatibility to the given firmware $version. |
||
| 339 | * |
||
| 340 | * @param string $version Target firmware version. |
||
| 341 | * @return bool TRUE if compatible, otherwise FALSE. |
||
| 342 | */ |
||
| 343 | 1 | public function isCompatibleToFirmware($version) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Checks if this package is a beta version or not. |
||
| 350 | * |
||
| 351 | * @return bool TRUE if this is a beta version, FALSE otherwise. |
||
| 352 | */ |
||
| 353 | 15 | public function isBeta() |
|
| 357 | } |
||
| 358 |