Complex classes like Plugin 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 Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
30 | { |
||
31 | const YII2_PACKAGE_TYPE = 'yii2-extension'; |
||
32 | const EXTRA_OPTION_NAME = 'config-plugin'; |
||
33 | |||
34 | /** |
||
35 | * @var PackageInterface[] the array of active composer packages |
||
36 | */ |
||
37 | protected $packages; |
||
38 | |||
39 | /** |
||
40 | * @var string absolute path to the package base directory |
||
41 | */ |
||
42 | protected $baseDir; |
||
43 | |||
44 | /** |
||
45 | * @var string absolute path to vendor directory |
||
46 | */ |
||
47 | protected $vendorDir; |
||
48 | |||
49 | /** |
||
50 | * @var Filesystem utility |
||
51 | */ |
||
52 | protected $filesystem; |
||
53 | |||
54 | /** |
||
55 | * @var array config name => list of files |
||
56 | */ |
||
57 | protected $files = [ |
||
58 | 'defines' => [], |
||
59 | 'params' => [], |
||
60 | ]; |
||
61 | |||
62 | protected $aliases = []; |
||
63 | |||
64 | protected $extensions = []; |
||
65 | |||
66 | /** |
||
67 | * @var array array of not yet merged params |
||
68 | */ |
||
69 | protected $rawParams = []; |
||
70 | |||
71 | /** |
||
72 | * @var Composer instance |
||
73 | */ |
||
74 | protected $composer; |
||
75 | |||
76 | /** |
||
77 | * @var IOInterface |
||
78 | */ |
||
79 | public $io; |
||
80 | |||
81 | /** |
||
82 | * Initializes the plugin object with the passed $composer and $io. |
||
83 | * @param Composer $composer |
||
84 | * @param IOInterface $io |
||
85 | */ |
||
86 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
91 | |||
92 | /** |
||
93 | * Returns list of events the plugin is subscribed to. |
||
94 | * @return array list of events |
||
95 | */ |
||
96 | 1 | public static function getSubscribedEvents() |
|
104 | |||
105 | /** |
||
106 | * This is the main function. |
||
107 | * @param Event $event |
||
108 | */ |
||
109 | public function onPostAutoloadDump(Event $event) |
||
122 | |||
123 | protected function scanPackages() |
||
131 | |||
132 | /** |
||
133 | * Scans the given package and collects extensions data. |
||
134 | * @param PackageInterface $package |
||
135 | */ |
||
136 | protected function processPackage(CompletePackageInterface $package) |
||
137 | { |
||
138 | $extra = $package->getExtra(); |
||
139 | $files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; |
||
140 | |||
141 | if ($package->getType() !== self::YII2_PACKAGE_TYPE && is_null($files)) { |
||
142 | return; |
||
143 | } |
||
144 | |||
145 | if (is_array($files)) { |
||
146 | $this->processFiles($package, $files); |
||
147 | } |
||
148 | |||
149 | $aliases = $this->collectAliases($package); |
||
150 | $this->aliases = array_merge($this->aliases, $aliases); |
||
151 | |||
152 | $this->extensions[$package->getPrettyName()] = array_filter([ |
||
153 | 'name' => $package->getPrettyName(), |
||
154 | 'version' => $package->getVersion(), |
||
155 | 'reference' => $package->getSourceReference() ?: $package->getDistReference(), |
||
156 | 'aliases' => $aliases, |
||
157 | ]); |
||
158 | } |
||
159 | |||
160 | protected function processFiles(CompletePackageInterface $package, array $files) |
||
161 | { |
||
162 | foreach ($files as $name => $paths) { |
||
163 | foreach ((array) $paths as $path) { |
||
164 | if (!isset($this->files[$name])) { |
||
165 | $this->files[$name] = []; |
||
166 | } |
||
167 | array_push($this->files[$name], $this->preparePath($package, $path)); |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Collects package aliases. |
||
174 | * @param CompletePackageInterface $package |
||
175 | * @return array collected aliases |
||
176 | */ |
||
177 | protected function collectAliases(CompletePackageInterface $package) |
||
178 | { |
||
179 | $aliases = array_merge( |
||
180 | $this->prepareAliases($package, 'psr-0'), |
||
181 | $this->prepareAliases($package, 'psr-4') |
||
182 | ); |
||
183 | if ($package instanceof RootPackageInterface) { |
||
184 | $aliases = array_merge($aliases, |
||
185 | $this->prepareAliases($package, 'psr-0', true), |
||
186 | $this->prepareAliases($package, 'psr-4', true) |
||
187 | ); |
||
188 | } |
||
189 | |||
190 | return $aliases; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Prepare aliases. |
||
195 | * @param PackageInterface $package |
||
196 | * @param string 'psr-0' or 'psr-4' |
||
197 | * @return array |
||
198 | */ |
||
199 | protected function prepareAliases(PackageInterface $package, $psr, $dev = false) |
||
200 | { |
||
201 | $autoload = $dev ? $package->getDevAutoload() : $package->getAutoload(); |
||
202 | if (empty($autoload[$psr])) { |
||
203 | return []; |
||
204 | } |
||
205 | |||
206 | $aliases = []; |
||
207 | foreach ($autoload[$psr] as $name => $path) { |
||
208 | if (is_array($path)) { |
||
209 | // ignore psr-4 autoload specifications with multiple search paths |
||
210 | // we can not convert them into aliases as they are ambiguous |
||
211 | continue; |
||
212 | } |
||
213 | $name = str_replace('\\', '/', trim($name, '\\')); |
||
214 | $path = $this->preparePath($package, $path); |
||
215 | if ('psr-0' === $psr) { |
||
216 | $path .= '/' . $name; |
||
217 | } |
||
218 | $aliases["@$name"] = $path; |
||
219 | } |
||
220 | |||
221 | return $aliases; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Builds path inside of a package. |
||
226 | * @param PackageInterface $package |
||
227 | * @param mixed $path can be absolute or relative |
||
228 | * @return string absolute paths will stay untouched |
||
229 | */ |
||
230 | public function preparePath(PackageInterface $package, $path) |
||
231 | { |
||
232 | $skippable = strncmp($path, '?', 1) === 0 ? '?' : ''; |
||
233 | if ($skippable) { |
||
234 | $path = substr($path, 1); |
||
235 | } |
||
236 | |||
237 | if (strncmp($path, '$', 1) === 0) { |
||
238 | $path = Builder::path(substr($path, 1)); |
||
239 | } |
||
240 | |||
241 | if (!$this->getFilesystem()->isAbsolutePath($path)) { |
||
242 | $prefix = $package instanceof RootPackageInterface |
||
243 | ? $this->getBaseDir() |
||
244 | : $this->getVendorDir() . '/' . $package->getPrettyName(); |
||
245 | $path = $prefix . '/' . $path; |
||
246 | } |
||
247 | |||
248 | return $skippable . $this->getFilesystem()->normalizePath($path); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Sets [[packages]]. |
||
253 | * @param PackageInterface[] $packages |
||
254 | */ |
||
255 | 2 | public function setPackages(array $packages) |
|
256 | { |
||
257 | 2 | $this->packages = $packages; |
|
258 | 2 | } |
|
259 | |||
260 | /** |
||
261 | * Gets [[packages]]. |
||
262 | * @return \Composer\Package\PackageInterface[] |
||
263 | */ |
||
264 | 1 | public function getPackages() |
|
265 | { |
||
266 | 1 | if ($this->packages === null) { |
|
267 | $this->packages = $this->findPackages(); |
||
268 | } |
||
269 | |||
270 | 1 | return $this->packages; |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
275 | * The list is unordered (chaotic, can be different after every update). |
||
276 | */ |
||
277 | protected $plainList = []; |
||
278 | |||
279 | /** |
||
280 | * Ordered list of package. Order @see findPackages. |
||
281 | */ |
||
282 | protected $orderedList = []; |
||
283 | |||
284 | /** |
||
285 | * Returns ordered list of packages: |
||
286 | * - listed earlier in the composer.json will get earlier in the list |
||
287 | * - childs before parents. |
||
288 | * @return \Composer\Package\PackageInterface[] |
||
289 | */ |
||
290 | public function findPackages() |
||
312 | |||
313 | /** |
||
314 | * Iterates through package dependencies. |
||
315 | * @param PackageInterface $package to iterate |
||
316 | * @param bool $includingDev process development dependencies, defaults to not process |
||
317 | */ |
||
318 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
338 | |||
339 | /** |
||
340 | * Iterates dependencies of the given package. |
||
341 | * @param PackageInterface $package |
||
342 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
343 | */ |
||
344 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
360 | |||
361 | /** |
||
362 | * Get absolute path to package base dir. |
||
363 | * @return string |
||
364 | */ |
||
365 | public function getBaseDir() |
||
366 | { |
||
367 | if ($this->baseDir === null) { |
||
373 | |||
374 | /** |
||
375 | * Get absolute path to composer vendor dir. |
||
376 | * @return string |
||
377 | */ |
||
378 | public function getVendorDir() |
||
387 | |||
388 | /** |
||
389 | * Getter for filesystem utility. |
||
390 | * @return Filesystem |
||
391 | */ |
||
392 | public function getFilesystem() |
||
400 | } |
||
401 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.