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 $wizfile; |
||
| 39 | private $metadata; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param \SSpkS\Config $config Config object |
||
| 43 | * @param string $filename Filename of SPK file |
||
| 44 | */ |
||
| 45 | public function __construct(\SSpkS\Config $config, $filename) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Getter magic method. |
||
| 64 | * |
||
| 65 | * @param string $name Name of requested value. |
||
| 66 | * @return mixed Requested value. |
||
| 67 | */ |
||
| 68 | public function __get($name) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Setter magic method. |
||
| 76 | * |
||
| 77 | * @param string $name Name of variable to set. |
||
| 78 | * @param mixed $value Value to set. |
||
| 79 | */ |
||
| 80 | public function __set($name, $value) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Isset feature magic method. |
||
| 88 | * |
||
| 89 | * @param string $name Name of requested value. |
||
| 90 | * @return bool TRUE if value exists, FALSE otherwise. |
||
| 91 | */ |
||
| 92 | public function __isset($name) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Unset feature magic method. |
||
| 100 | * |
||
| 101 | * @param string $name Name of value to unset. |
||
| 102 | */ |
||
| 103 | public function __unset($name) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Parses boolean value ('yes', '1', 'true') into |
||
| 111 | * boolean type. |
||
| 112 | * |
||
| 113 | * @param mixed $value Input value |
||
| 114 | * @return bool Boolean interpretation of $value. |
||
| 115 | */ |
||
| 116 | public function parseBool($value) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Checks if given property $prop exists and converts it |
||
| 123 | * into a boolean value. |
||
| 124 | * |
||
| 125 | * @param string $prop Property to convert |
||
| 126 | */ |
||
| 127 | private function fixBoolIfExist($prop) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Gathers metadata from package. Extracts INFO file if neccessary. |
||
| 136 | */ |
||
| 137 | private function collectMetadata() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns metadata for this package. |
||
| 173 | * |
||
| 174 | * @return array Metadata. |
||
| 175 | */ |
||
| 176 | public function getMetadata() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Extracts $inPkgName from package to $targetFile, if it doesn't |
||
| 184 | * already exist. Needs the phar.so extension and allow_url_fopen. |
||
| 185 | * |
||
| 186 | * @param string $inPkgName Filename in package |
||
| 187 | * @param string $targetFile Path to destination |
||
| 188 | * @throws \Exception if the file couldn't get extracted. |
||
| 189 | * @return bool TRUE if successful or no action needed. |
||
| 190 | */ |
||
| 191 | 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 | public function hasWizardDir() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns a list of thumbnails for the specified package. |
||
| 253 | * |
||
| 254 | * @param string $pathPrefix Prefix to put before file path |
||
| 255 | * @return array List of thumbnail urls |
||
| 256 | */ |
||
| 257 | public function getThumbnails($pathPrefix = '') |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Returns a list of screenshots for the specified package. |
||
| 297 | * |
||
| 298 | * @param string $pathPrefix Prefix to put before file path |
||
| 299 | * @return array List of screenshots |
||
| 300 | */ |
||
| 301 | public function getSnapshots($pathPrefix = '') |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Checks compatibility to the given $arch-itecture. |
||
| 323 | * |
||
| 324 | * @param string $arch Architecture to check against (or "noarch") |
||
| 325 | * @return bool TRUE if compatible, otherwise FALSE. |
||
| 326 | */ |
||
| 327 | public function isCompatibleToArch($arch) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Checks compatibility to the given firmware $version. |
||
| 337 | * |
||
| 338 | * @param string $version Target firmware version. |
||
| 339 | * @return bool TRUE if compatible, otherwise FALSE. |
||
| 340 | */ |
||
| 341 | public function isCompatibleToFirmware($version) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Checks if this package is a beta version or not. |
||
| 349 | * |
||
| 350 | * @return bool TRUE if this is a beta version, FALSE otherwise. |
||
| 351 | */ |
||
| 352 | public function isBeta() |
||
| 357 | } |
||
| 358 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'or with double quotes"literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\') and the backslash (\\). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is ValueIf your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.