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 | public static function normalizeName($name) |
||
| 57 | { |
||
| 58 | return strtolower($name); |
||
| 59 | } |
||
| 60 | |||
| 61 | 1 | public function __construct($type, $name, $config = []) |
|
| 62 | { |
||
| 63 | parent::__construct($config); |
||
| 64 | |||
| 65 | if (!$this->checkType($type)) { |
||
| 66 | throw new Exception('wrong type'); |
||
| 67 | } |
||
| 68 | if (!$this->checkName($name)) { |
||
| 69 | throw new Exception('wrong name'); |
||
| 70 | } |
||
| 71 | 1 | $this->_type = $type; |
|
| 72 | 1 | $this->_name = $name; |
|
| 73 | } |
||
| 74 | |||
| 75 | public function getRegistry() { |
||
| 76 | if ($this->_registry === null) { |
||
| 77 | $this->_registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager()); |
||
|
|
|||
| 78 | } |
||
| 79 | |||
| 80 | return $this->_registry; |
||
| 81 | } |
||
| 82 | |||
| 83 | 1 | public function checkType($type) |
|
| 87 | |||
| 88 | public function checkName($name) |
||
| 89 | { |
||
| 90 | return strlen($name) > 1; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function getFullName() |
||
| 94 | { |
||
| 95 | return static::buildFullName($this->_type, $this->_name); |
||
| 96 | } |
||
| 97 | |||
| 98 | 1 | public static function buildFullName($type, $name) |
|
| 102 | |||
| 103 | public static function splitFullName($full) |
||
| 104 | { |
||
| 105 | list($temp, $name) = explode('/', $full); |
||
| 106 | list($type,) = explode('-', $temp); |
||
| 107 | |||
| 110 | |||
| 111 | public function getType() |
||
| 115 | |||
| 116 | public function getNormalName() |
||
| 120 | |||
| 121 | public function getName() |
||
| 125 | |||
| 126 | public function getHash() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return Composer |
||
| 133 | */ |
||
| 134 | public static function getCommonComposer() |
||
| 147 | |||
| 148 | public function setComposer($value) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return Composer |
||
| 155 | */ |
||
| 156 | public function getComposer() |
||
| 164 | |||
| 165 | public function getIO() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * findOne. |
||
| 176 | * |
||
| 177 | * @param string $type |
||
| 178 | * @param string $name |
||
| 179 | * @return static|null |
||
| 180 | */ |
||
| 181 | public static function findOne($type, $name) |
||
| 188 | |||
| 189 | public function load() |
||
| 198 | |||
| 199 | public function update() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param AssetVcsRepository $repository |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function prepareReleases($repository) |
||
| 241 | |||
| 242 | public function prepareUid($version) |
||
| 248 | |||
| 249 | public function getReleases() |
||
| 253 | |||
| 254 | public function getRelease($version) |
||
| 258 | |||
| 259 | public function getSaved() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return Storage |
||
| 270 | */ |
||
| 271 | public function getStorage() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns the latest update time (UNIX Epoch) |
||
| 278 | * @return int|null |
||
| 279 | */ |
||
| 280 | public function getUpdateTime() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Package can be updated not more often than once in 10 min |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | public function canBeUpdated() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Whether tha package should be auth-updated (if it is older than 1 day) |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function canAutoUpdate() |
||
| 302 | } |
||
| 303 |
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..