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 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $_releases = []; |
||
35 | protected $_saved; |
||
36 | /** |
||
37 | * @var AssetVcsRepository|BowerRegistry|NpmRegistry |
||
38 | */ |
||
39 | protected $_registry; |
||
40 | |||
41 | /** |
||
42 | * @var integer UNIX Epoch timestamp of the latest package update |
||
43 | */ |
||
44 | protected $_updateTime; |
||
45 | |||
46 | /** |
||
47 | * @var NullIO |
||
48 | */ |
||
49 | protected $_io; |
||
50 | /** |
||
51 | * @var Composer |
||
52 | */ |
||
53 | protected $_composer; |
||
54 | /** |
||
55 | * @var Composer |
||
56 | */ |
||
57 | protected static $_commonComposer; |
||
58 | |||
59 | public static function normalizeName($name) |
||
63 | |||
64 | 1 | /** |
|
65 | * AssetPackage constructor. |
||
66 | 1 | * @param string $type |
|
67 | * @param string $name |
||
68 | * @param array $config |
||
69 | 1 | * @throws Exception |
|
70 | */ |
||
71 | public function __construct($type, $name, $config = []) |
||
84 | |||
85 | 1 | public function getRegistry() |
|
93 | |||
94 | public function checkType($type) |
||
98 | |||
99 | public function checkName($name) |
||
103 | |||
104 | public function getFullName() |
||
108 | |||
109 | public static function buildFullName($type, $name) |
||
113 | |||
114 | public static function splitFullName($full) |
||
121 | |||
122 | /** |
||
123 | * @param string $full package name |
||
124 | * @return static |
||
125 | */ |
||
126 | public static function fromFullName($full) |
||
131 | |||
132 | public function getType() |
||
136 | |||
137 | public function getNormalName() |
||
141 | |||
142 | public function getName() |
||
146 | |||
147 | public function getHash() |
||
151 | |||
152 | /** |
||
153 | * @return Composer |
||
154 | */ |
||
155 | public static function getCommonComposer() |
||
168 | |||
169 | public function setComposer($value) |
||
173 | |||
174 | /** |
||
175 | * @return Composer |
||
176 | */ |
||
177 | public function getComposer() |
||
185 | |||
186 | public function getIO() |
||
194 | |||
195 | /** |
||
196 | * findOne. |
||
197 | * |
||
198 | * @param string $type |
||
199 | * @param string $name |
||
200 | * @return static|null |
||
201 | */ |
||
202 | public static function findOne($type, $name) |
||
209 | |||
210 | public function load() |
||
219 | |||
220 | public function update() |
||
227 | |||
228 | /** |
||
229 | * @param AssetVcsRepository $repository |
||
230 | * @return array |
||
231 | */ |
||
232 | public function prepareReleases($repository) |
||
275 | |||
276 | /** |
||
277 | * Prepares array of requires: name => constraint. |
||
278 | * @param Link[] array of package requires |
||
279 | * @return array |
||
280 | */ |
||
281 | public function prepareRequire(array $links) |
||
291 | |||
292 | public function prepareUid($version) |
||
298 | |||
299 | /** |
||
300 | * @return array |
||
301 | */ |
||
302 | public function getReleases() |
||
306 | |||
307 | /** |
||
308 | * @param $version |
||
309 | * @return array |
||
310 | */ |
||
311 | public function getRelease($version) |
||
315 | |||
316 | public function getSaved() |
||
324 | |||
325 | /** |
||
326 | * @return Storage |
||
327 | */ |
||
328 | public function getStorage() |
||
332 | |||
333 | /** |
||
334 | * Returns the latest update time (UNIX Epoch). |
||
335 | * @return int|null |
||
336 | */ |
||
337 | public function getUpdateTime() |
||
341 | |||
342 | /** |
||
343 | * Package can be updated not more often than once in 10 min. |
||
344 | * @return bool |
||
345 | */ |
||
346 | public function canBeUpdated() |
||
350 | |||
351 | /** |
||
352 | * Whether tha package should be auth-updated (if it is older than 1 day). |
||
353 | * @return bool |
||
354 | */ |
||
355 | public function canAutoUpdate() |
||
359 | |||
360 | /** |
||
361 | * @return array |
||
362 | */ |
||
363 | public function __sleep() |
||
367 | } |
||
368 |
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..