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 | protected $_releases = []; |
||
| 33 | protected $_saved; |
||
| 34 | /** |
||
| 35 | * @var AssetVcsRepository|BowerRegistry|NpmRegistry |
||
| 36 | */ |
||
| 37 | protected $_registry; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var integer UNIX Epoch timestamp of the latest package update |
||
| 41 | */ |
||
| 42 | protected $_updateTime; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var NullIO |
||
| 46 | */ |
||
| 47 | protected $_io; |
||
| 48 | /** |
||
| 49 | * @var Composer |
||
| 50 | */ |
||
| 51 | protected $_composer; |
||
| 52 | /** |
||
| 53 | * @var Composer |
||
| 54 | */ |
||
| 55 | protected static $_commonComposer; |
||
| 56 | |||
| 57 | public static function normalizeName($name) |
||
| 61 | |||
| 62 | 1 | public function __construct($type, $name, $config = []) |
|
| 75 | |||
| 76 | public function getRegistry() |
||
| 84 | |||
| 85 | 1 | public function checkType($type) |
|
| 89 | |||
| 90 | 1 | public function checkName($name) |
|
| 94 | |||
| 95 | 1 | public function getFullName() |
|
| 99 | |||
| 100 | 1 | public static function buildFullName($type, $name) |
|
| 104 | |||
| 105 | public static function splitFullName($full) |
||
| 112 | |||
| 113 | public function getType() |
||
| 117 | |||
| 118 | public function getNormalName() |
||
| 122 | |||
| 123 | public function getName() |
||
| 127 | |||
| 128 | public function getHash() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return Composer |
||
| 135 | */ |
||
| 136 | public static function getCommonComposer() |
||
| 149 | |||
| 150 | public function setComposer($value) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return Composer |
||
| 157 | */ |
||
| 158 | public function getComposer() |
||
| 166 | |||
| 167 | public function getIO() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * findOne. |
||
| 178 | * |
||
| 179 | * @param string $type |
||
| 180 | * @param string $name |
||
| 181 | * @return static|null |
||
| 182 | */ |
||
| 183 | public static function findOne($type, $name) |
||
| 190 | |||
| 191 | public function load() |
||
| 200 | |||
| 201 | public function update() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param AssetVcsRepository $repository |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | public function prepareReleases($repository) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Prepares array of requires: name => constraint. |
||
| 250 | * @param Link[] array of package requires. |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | public function prepareRequire(array $links) |
||
| 262 | |||
| 263 | public function prepareUid($version) |
||
| 269 | |||
| 270 | public function getReleases() |
||
| 274 | |||
| 275 | public function getRelease($version) |
||
| 279 | |||
| 280 | public function getSaved() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return Storage |
||
| 291 | */ |
||
| 292 | public function getStorage() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the latest update time (UNIX Epoch). |
||
| 299 | * @return int|null |
||
| 300 | */ |
||
| 301 | public function getUpdateTime() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Package can be updated not more often than once in 10 min. |
||
| 308 | * @return bool |
||
| 309 | */ |
||
| 310 | public function canBeUpdated() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Whether tha package should be auth-updated (if it is older than 1 day). |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function canAutoUpdate() |
||
| 323 | } |
||
| 324 |
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..