Complex classes like AssetPackage 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 AssetPackage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class AssetPackage extends Object |
||
| 22 | { |
||
| 23 | protected $_type; |
||
| 24 | protected $_name; |
||
| 25 | protected $_hash; |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $_releases = []; |
||
| 30 | protected $_saved; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var integer UNIX Epoch timestamp of the latest package update |
||
| 34 | */ |
||
| 35 | protected $_updateTime; |
||
| 36 | |||
| 37 | public static function normalizeName($name) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * AssetPackage constructor. |
||
| 44 | * @param string $type |
||
| 45 | * @param string $name |
||
| 46 | * @param array $config |
||
| 47 | * @throws Exception |
||
| 48 | */ |
||
| 49 | 1 | public function __construct($type, $name, $config = []) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @return RegistryFactory |
||
| 65 | */ |
||
| 66 | public function getRegistry() |
||
| 70 | |||
| 71 | 1 | public function checkType($type) |
|
| 75 | |||
| 76 | 1 | public function checkName($name) |
|
| 80 | |||
| 81 | 1 | public function getFullName() |
|
| 85 | |||
| 86 | public static function buildNormalName($type, $name) |
||
| 90 | |||
| 91 | 1 | public static function buildFullName($type, $name) |
|
| 95 | |||
| 96 | public static function splitFullName($full) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $full package name |
||
| 106 | * @return static |
||
| 107 | */ |
||
| 108 | public static function fromFullName($full) |
||
| 113 | |||
| 114 | public function getType() |
||
| 118 | |||
| 119 | public function getNormalName() |
||
| 123 | |||
| 124 | public function getName() |
||
| 128 | |||
| 129 | public function getHash() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * findOne. |
||
| 136 | * |
||
| 137 | * @param string $type |
||
| 138 | * @param string $name |
||
| 139 | * @return static|null |
||
| 140 | */ |
||
| 141 | public static function findOne($type, $name) |
||
| 148 | |||
| 149 | public function load() |
||
| 158 | |||
| 159 | public function update() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param \Composer\DependencyResolver\Pool $pool |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | public function prepareReleases($pool) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Prepares array of requires: name => constraint. |
||
| 222 | * @param Link[] array of package requires |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | public function prepareRequire(array $links) |
||
| 235 | |||
| 236 | public function prepareUid($version) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function getReleases() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param $version |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function getRelease($version) |
||
| 259 | |||
| 260 | public function getSaved() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return Storage |
||
| 271 | */ |
||
| 272 | public function getStorage() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the latest update time (UNIX Epoch). |
||
| 279 | * @return int|null |
||
| 280 | */ |
||
| 281 | public function getUpdateTime() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Package can be updated not more often than once in 10 min. |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function canBeUpdated() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Whether tha package should be auth-updated (if it is older than 1 day). |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | public function canAutoUpdate() |
||
| 303 | |||
| 304 | public function isAvailable() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | public function __sleep() |
||
| 318 | } |
||
| 319 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.