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 |
||
| 20 | class AssetPackage extends Object |
||
| 21 | { |
||
| 22 | protected $_type; |
||
| 23 | protected $_name; |
||
| 24 | protected $_hash; |
||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $_releases = []; |
||
| 29 | protected $_saved; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var integer UNIX Epoch timestamp of the latest package update |
||
| 33 | */ |
||
| 34 | protected $_updateTime; |
||
| 35 | |||
| 36 | public static function normalizeName($name) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * AssetPackage constructor. |
||
| 43 | * @param string $type |
||
| 44 | * @param string $name |
||
| 45 | * @param array $config |
||
| 46 | * @throws Exception |
||
| 47 | */ |
||
| 48 | public function __construct($type, $name, $config = []) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return RegistryFactory |
||
| 64 | */ |
||
| 65 | public function getRegistry() |
||
| 69 | |||
| 70 | public function checkType($type) |
||
| 74 | |||
| 75 | 1 | public function checkName($name) |
|
| 79 | |||
| 80 | public function getFullName() |
||
| 84 | |||
| 85 | public static function buildFullName($type, $name) |
||
| 89 | |||
| 90 | public static function splitFullName($full) |
||
| 97 | |||
| 98 | /** |
||
| 99 | 1 | * @param string $full package name |
|
| 100 | * @return static |
||
| 101 | 1 | */ |
|
| 102 | public static function fromFullName($full) |
||
| 107 | |||
| 108 | public function getType() |
||
| 112 | |||
| 113 | public function getNormalName() |
||
| 117 | |||
| 118 | public function getName() |
||
| 122 | |||
| 123 | public function getHash() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * findOne. |
||
| 130 | * |
||
| 131 | * @param string $type |
||
| 132 | * @param string $name |
||
| 133 | * @return static|null |
||
| 134 | */ |
||
| 135 | public static function findOne($type, $name) |
||
| 142 | |||
| 143 | public function load() |
||
| 152 | |||
| 153 | public function update() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param \Composer\DependencyResolver\Pool $pool |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public function prepareReleases($pool) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Prepares array of requires: name => constraint. |
||
| 213 | * @param Link[] array of package requires |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function prepareRequire(array $links) |
||
| 226 | |||
| 227 | public function prepareUid($version) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | public function getReleases() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param $version |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function getRelease($version) |
||
| 250 | |||
| 251 | public function getSaved() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return Storage |
||
| 262 | */ |
||
| 263 | public function getStorage() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns the latest update time (UNIX Epoch). |
||
| 270 | * @return int|null |
||
| 271 | */ |
||
| 272 | public function getUpdateTime() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Package can be updated not more often than once in 10 min. |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | public function canBeUpdated() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Whether tha package should be auth-updated (if it is older than 1 day). |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function canAutoUpdate() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | public function __sleep() |
||
| 302 | } |
||
| 303 |
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.