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 |
||
| 26 | class AssetPackage extends Object |
||
| 27 | { |
||
| 28 | protected $_type; |
||
| 29 | protected $_name; |
||
| 30 | protected $_hash; |
||
| 31 | protected $_releases = []; |
||
| 32 | protected $_saved; |
||
| 33 | /** |
||
| 34 | * @var AssetVcsRepository|BowerRegistry|NpmRegistry |
||
| 35 | */ |
||
| 36 | protected $_registry; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var integer UNIX Epoch timestamp of the latest package update |
||
| 40 | */ |
||
| 41 | protected $_updateTime; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var NullIO |
||
| 45 | */ |
||
| 46 | protected $_io; |
||
| 47 | /** |
||
| 48 | * @var Composer |
||
| 49 | */ |
||
| 50 | protected $_composer; |
||
| 51 | /** |
||
| 52 | * @var Composer |
||
| 53 | */ |
||
| 54 | protected static $_commonComposer; |
||
| 55 | |||
| 56 | 1 | public function __construct($type, $name, $config = []) |
|
| 69 | |||
| 70 | public function getRegistry() { |
||
| 71 | if ($this->_registry === null) { |
||
| 72 | $this->_registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager()); |
||
|
|
|||
| 73 | } |
||
| 74 | |||
| 75 | return $this->_registry; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function checkType($type) |
||
| 82 | |||
| 83 | public function checkName($name) |
||
| 87 | |||
| 88 | public function getFullName() |
||
| 92 | |||
| 93 | public static function buildFullName($type, $name) |
||
| 97 | |||
| 98 | public static function splitFullName($full) |
||
| 105 | |||
| 106 | public function getType() |
||
| 110 | |||
| 111 | public function getName() |
||
| 115 | |||
| 116 | public function getHash() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return Composer |
||
| 123 | */ |
||
| 124 | public static function getCommonComposer() |
||
| 137 | |||
| 138 | public function setComposer($value) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return Composer |
||
| 145 | */ |
||
| 146 | public function getComposer() |
||
| 154 | |||
| 155 | public function getIO() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * findOne. |
||
| 166 | * |
||
| 167 | * @param string $type |
||
| 168 | * @param string $name |
||
| 169 | * @return static|null |
||
| 170 | */ |
||
| 171 | public static function findOne($type, $name) |
||
| 178 | |||
| 179 | public function load() |
||
| 188 | |||
| 189 | public function update() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param AssetVcsRepository $repository |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function prepareReleases($repository) |
||
| 231 | |||
| 232 | public function prepareUid($version) |
||
| 238 | |||
| 239 | public function getReleases() |
||
| 243 | |||
| 244 | public function getRelease($version) |
||
| 248 | |||
| 249 | public function getSaved() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return Storage |
||
| 260 | */ |
||
| 261 | public function getStorage() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the latest update time (UNIX Epoch) |
||
| 268 | * @return int|null |
||
| 269 | */ |
||
| 270 | public function getUpdateTime() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Package can be updated not more often than once in 10 min |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public function canBeUpdated() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Whether tha package should be auth-updated (if it is older than 1 day) |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | public function canAutoUpdate() |
||
| 292 | } |
||
| 293 |
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..