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) |
||
60 | |||
61 | 1 | public function __construct($type, $name, $config = []) |
|
74 | |||
75 | public function getRegistry() { |
||
82 | |||
83 | public function checkType($type) |
||
87 | |||
88 | public function checkName($name) |
||
92 | |||
93 | public function getFullName() |
||
97 | |||
98 | public static function buildFullName($type, $name) |
||
102 | |||
103 | public static function splitFullName($full) |
||
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) |
||
211 | { |
||
212 | $releases = []; |
||
213 | foreach ($repository->getPackages() as $package) { |
||
214 | $version = $package->getVersion(); |
||
215 | $release = [ |
||
216 | 'uid' => $this->prepareUid($version), |
||
217 | 'name' => $this->getNormalName(), |
||
218 | 'version' => $version, |
||
219 | ]; |
||
220 | if ($package->getDistUrl()) { |
||
221 | $release['dist'] = [ |
||
222 | 'type' => $package->getDistType(), |
||
223 | 'url' => $package->getDistUrl(), |
||
224 | 'reference' => $package->getDistReference(), |
||
225 | ]; |
||
226 | } |
||
227 | if ($package->getSourceUrl()) { |
||
228 | $release['source'] = [ |
||
229 | 'type' => $package->getSourceType(), |
||
230 | 'url' => $package->getSourceUrl(), |
||
231 | 'reference' => $package->getSourceReference(), |
||
232 | ]; |
||
233 | } |
||
234 | if ( (isset($release['dist']) && $release['dist']) || (isset($release['source']) && $release['source']) ) { |
||
235 | $releases[$version] = $release; |
||
236 | } |
||
237 | } |
||
238 | |||
239 | return $releases; |
||
240 | } |
||
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..