Complex classes like AddonUpdater 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 AddonUpdater, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class AddonUpdater |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var PackagistService |
||
19 | */ |
||
20 | private $packagist; |
||
21 | |||
22 | /** |
||
23 | * @var SilverStripe\Elastica\ElasticaService |
||
24 | */ |
||
25 | private $elastica; |
||
26 | |||
27 | /** |
||
28 | * @var SilverStripeVersion[] |
||
29 | */ |
||
30 | private $silverstripes; |
||
31 | |||
32 | public function __construct( |
||
33 | PackagistService $packagist, |
||
34 | ElasticaService $elastica |
||
35 | ) { |
||
36 | $this->packagist = $packagist; |
||
37 | $this->elastica = $elastica; |
||
38 | |||
39 | $this->setSilverStripeVersions(SilverStripeVersion::get()); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Updates all add-ons. |
||
44 | * |
||
45 | * @param Boolean Clear existing addons before updating them. |
||
46 | * Will also clear their search index, and cascade the delete for associated data. |
||
47 | * @param Array Limit to specific addons, using their name incl. vendor prefix. |
||
48 | */ |
||
49 | public function update($clear = false, $limitAddons = null) |
||
50 | { |
||
51 | if ($clear && !$limitAddons) { |
||
52 | Addon::get()->removeAll(); |
||
53 | AddonAuthor::get()->removeAll(); |
||
54 | AddonKeyword::get()->removeAll(); |
||
55 | AddonLink::get()->removeAll(); |
||
56 | AddonVendor::get()->removeAll(); |
||
57 | AddonVersion::get()->removeAll(); |
||
58 | } |
||
59 | |||
60 | // This call to packagist can be expensive. Requests are served from a cache if usePackagistCache() returns true |
||
61 | $cache = SS_Cache::factory('addons'); |
||
62 | |||
63 | if ($this->usePackagistCache() && $packages = $cache->load('packagist')) { |
||
64 | $packages = unserialize($packages); |
||
65 | } else { |
||
66 | $packages = $this->packagist->getPackages(); |
||
67 | $cache->save(serialize($packages), 'packagist'); |
||
68 | } |
||
69 | |||
70 | // TODO: AWS elasticsearch doesn't have this setting enabled |
||
71 | // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/url-access-control.html |
||
72 | // and bulk index operations by elastica currently require it |
||
73 | // Switching to https://github.com/heyday/silverstripe-elastica and SS4 might help |
||
74 | |||
75 | // $this->elastica->startBulkIndex(); |
||
76 | |||
77 | foreach ($packages as $package) { |
||
78 | /** @var Packagist\Api\Result\Package $package */ |
||
79 | |||
80 | $isAbandoned = (method_exists($package, 'isAbandoned') && $package->isAbandoned()); |
||
81 | $name = $package->getName(); |
||
82 | $versions = $package->getVersions(); |
||
83 | |||
84 | if ($limitAddons && !in_array($name, $limitAddons)) { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | $addon = Addon::get()->filter('Name', $name)->first(); |
||
89 | |||
90 | if (!$addon) { |
||
91 | if ($isAbandoned) { |
||
92 | echo ' - Skipping abandoned package: ' . $name, PHP_EOL; |
||
93 | continue; |
||
94 | } |
||
95 | |||
96 | $addon = new Addon(); |
||
97 | $addon->Name = $name; |
||
|
|||
98 | $addon->write(); |
||
99 | } |
||
100 | |||
101 | if ($isAbandoned) { |
||
102 | echo ' - Removing abandoned package: ' . $name, PHP_EOL; |
||
103 | $addon->delete(); |
||
104 | continue; |
||
105 | } |
||
106 | |||
107 | usort($versions, function ($a, $b) { |
||
108 | return version_compare($a->getVersionNormalized(), $b->getVersionNormalized()); |
||
109 | }); |
||
110 | |||
111 | $this->updateAddon($addon, $package, $versions); |
||
112 | } |
||
113 | |||
114 | // $this->elastica->endBulkIndex(); |
||
115 | } |
||
116 | |||
117 | |||
118 | |||
119 | /** |
||
120 | * Check whether or not we should contact packagist or use a cached version. This allows to speed up the task |
||
121 | * during development. |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | protected function usePackagistCache() |
||
129 | |||
130 | private function updateAddon(Addon $addon, Package $package, array $versions) |
||
131 | { |
||
190 | |||
191 | private function updateVersion(Addon $addon, Version $package) |
||
249 | |||
250 | private function updateLinks(AddonVersion $version, Version $package) |
||
304 | |||
305 | private function updateCompatibility(Addon $addon, AddonVersion $version, Version $package) |
||
349 | |||
350 | private function updateAuthors(AddonVersion $version, Version $package) |
||
399 | |||
400 | /** |
||
401 | * Get the list of SilverStripe versions |
||
402 | * |
||
403 | * @return DataList |
||
404 | */ |
||
405 | public function getSilverStripeVersions() |
||
409 | |||
410 | /** |
||
411 | * Set the list of SilverStripeVersions |
||
412 | * |
||
413 | * @param DataList $versions |
||
414 | * @return $this |
||
415 | */ |
||
416 | public function setSilverStripeVersions(DataList $versions) |
||
421 | } |
||
422 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.