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 |
||
| 30 | class Package |
||
| 31 | { |
||
| 32 | private $config; |
||
| 33 | private $filepath; |
||
| 34 | private $filepathNoExt; |
||
| 35 | private $filename; |
||
| 36 | private $filenameNoExt; |
||
| 37 | private $metafile; |
||
| 38 | private $metadata; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param \SSpkS\Config $config Config object |
||
| 42 | * @param string $filename Filename of SPK file |
||
| 43 | */ |
||
| 44 | public function __construct(\SSpkS\Config $config, $filename) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Getter magic method. |
||
| 63 | * |
||
| 64 | * @param string $name Name of requested value. |
||
| 65 | * @return mixed Requested value. |
||
| 66 | */ |
||
| 67 | public function __get($name) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Setter magic method. |
||
| 75 | * |
||
| 76 | * @param string $name Name of variable to set. |
||
| 77 | * @param mixed $value Value to set. |
||
| 78 | */ |
||
| 79 | public function __set($name, $value) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Isset feature magic method. |
||
| 87 | * |
||
| 88 | * @param string $name Name of requested value. |
||
| 89 | * @return bool TRUE if value exists, FALSE otherwise. |
||
| 90 | */ |
||
| 91 | public function __isset($name) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Unset feature magic method. |
||
| 99 | * |
||
| 100 | * @param string $name Name of value to unset. |
||
| 101 | */ |
||
| 102 | public function __unset($name) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Parses boolean value ('yes', '1', 'true') into |
||
| 110 | * boolean type. |
||
| 111 | * |
||
| 112 | * @param mixed $value Input value |
||
| 113 | * @return bool Boolean interpretation of $value. |
||
| 114 | */ |
||
| 115 | public function parseBool($value) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Checks if given property $prop exists and converts it |
||
| 122 | * into a boolean value. |
||
| 123 | * |
||
| 124 | * @param string $prop Property to convert |
||
| 125 | */ |
||
| 126 | private function fixBoolIfExist($prop) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Gathers metadata from package. Extracts INFO file if neccessary. |
||
| 135 | */ |
||
| 136 | private function collectMetadata() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns metadata for this package. |
||
| 172 | * |
||
| 173 | * @return array Metadata. |
||
| 174 | */ |
||
| 175 | public function getMetadata() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Extracts $inPkgName from package to $targetFile, if it doesn't |
||
| 183 | * already exist. Needs the phar.so extension and allow_url_fopen. |
||
| 184 | * |
||
| 185 | * @param string $inPkgName Filename in package |
||
| 186 | * @param string $targetFile Path to destination |
||
| 187 | * @throws \Exception if the file couldn't get extracted. |
||
| 188 | * @return bool TRUE if successful or no action needed. |
||
| 189 | */ |
||
| 190 | public function extractIfMissing($inPkgName, $targetFile) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns a true if the package contains WIZARD_UIFILES/install_uifile. |
||
| 224 | * |
||
| 225 | * @param string $pathPrefix Prefix to put before file path |
||
| 226 | * @return bool Package has a wizard |
||
| 227 | */ |
||
| 228 | public function hasWizardDir($pathPrefix = '') |
||
| 229 | { |
||
| 230 | // Try to find file in package |
||
| 231 | try { |
||
| 232 | $this->extractIfMissing('WIZARD_UIFILES', $this->wizardsdir); |
||
| 233 | return true; |
||
| 234 | } catch (\Exception $e) { |
||
| 235 | return false; |
||
| 236 | } finally { |
||
| 237 | if (file_exists($this->wizardsdir)) { |
||
| 238 | unlink($this->wizardsdir); |
||
| 239 | } |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns a list of thumbnails for the specified package. |
||
| 246 | * |
||
| 247 | * @param string $pathPrefix Prefix to put before file path |
||
| 248 | * @return array List of thumbnail urls |
||
| 249 | */ |
||
| 250 | public function getThumbnails($pathPrefix = '') |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Returns a list of screenshots for the specified package. |
||
| 290 | * |
||
| 291 | * @param string $pathPrefix Prefix to put before file path |
||
| 292 | * @return array List of screenshots |
||
| 293 | */ |
||
| 294 | public function getSnapshots($pathPrefix = '') |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Checks compatibility to the given $arch-itecture. |
||
| 316 | * |
||
| 317 | * @param string $arch Architecture to check against (or "noarch") |
||
| 318 | * @return bool TRUE if compatible, otherwise FALSE. |
||
| 319 | */ |
||
| 320 | public function isCompatibleToArch($arch) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Checks compatibility to the given firmware $version. |
||
| 330 | * |
||
| 331 | * @param string $version Target firmware version. |
||
| 332 | * @return bool TRUE if compatible, otherwise FALSE. |
||
| 333 | */ |
||
| 334 | public function isCompatibleToFirmware($version) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Checks if this package is a beta version or not. |
||
| 342 | * |
||
| 343 | * @return bool TRUE if this is a beta version, FALSE otherwise. |
||
| 344 | */ |
||
| 345 | public function isBeta() |
||
| 350 | } |
||
| 351 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.