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 |
||
| 27 | class AssetPackage extends Object |
||
| 28 | { |
||
| 29 | protected $_type; |
||
| 30 | protected $_name; |
||
| 31 | protected $_hash; |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $_releases = []; |
||
| 36 | protected $_saved; |
||
| 37 | /** |
||
| 38 | * @var AssetVcsRepository|BowerRegistry|NpmRegistry |
||
| 39 | */ |
||
| 40 | protected $_registry; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var integer UNIX Epoch timestamp of the latest package update |
||
| 44 | */ |
||
| 45 | protected $_updateTime; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var NullIO |
||
| 49 | */ |
||
| 50 | protected $_io; |
||
| 51 | /** |
||
| 52 | * @var Composer |
||
| 53 | */ |
||
| 54 | protected $_composer; |
||
| 55 | /** |
||
| 56 | * @var Composer |
||
| 57 | */ |
||
| 58 | protected static $_commonComposer; |
||
| 59 | |||
| 60 | public static function normalizeName($name) |
||
| 64 | 1 | ||
| 65 | public function __construct($type, $name, $config = []) |
||
| 78 | |||
| 79 | public function getRegistry() |
||
| 87 | 1 | ||
| 88 | public function checkType($type) |
||
| 92 | 1 | ||
| 93 | public function checkName($name) |
||
| 97 | 1 | ||
| 98 | public function getFullName() |
||
| 102 | 1 | ||
| 103 | public static function buildFullName($type, $name) |
||
| 107 | |||
| 108 | public static function splitFullName($full) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $full package name |
||
| 118 | * @return static |
||
| 119 | */ |
||
| 120 | public static function fromFullName($full) |
||
| 125 | |||
| 126 | public function getType() |
||
| 130 | |||
| 131 | public function getNormalName() |
||
| 135 | |||
| 136 | public function getName() |
||
| 140 | |||
| 141 | public function getHash() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return Composer |
||
| 148 | */ |
||
| 149 | public static function getCommonComposer() |
||
| 162 | |||
| 163 | public function setComposer($value) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return Composer |
||
| 170 | */ |
||
| 171 | public function getComposer() |
||
| 179 | |||
| 180 | public function getIO() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * findOne. |
||
| 191 | * |
||
| 192 | * @param string $type |
||
| 193 | * @param string $name |
||
| 194 | * @return static|null |
||
| 195 | */ |
||
| 196 | public static function findOne($type, $name) |
||
| 203 | |||
| 204 | public function load() |
||
| 213 | |||
| 214 | public function update() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param AssetVcsRepository $repository |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function prepareReleases($repository) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Prepares array of requires: name => constraint. |
||
| 266 | * @param Link[] array of package requires. |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function prepareRequire(array $links) |
||
| 279 | |||
| 280 | public function prepareUid($version) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function getReleases() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param $version |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | public function getRelease($version) |
||
| 303 | |||
| 304 | public function getSaved() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return Storage |
||
| 315 | */ |
||
| 316 | public function getStorage() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns the latest update time (UNIX Epoch). |
||
| 323 | * @return int|null |
||
| 324 | */ |
||
| 325 | public function getUpdateTime() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Package can be updated not more often than once in 10 min. |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function canBeUpdated() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Whether tha package should be auth-updated (if it is older than 1 day). |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | public function canAutoUpdate() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | public function __sleep() |
||
| 355 | } |
||
| 356 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..