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 | 'dotenv' => [], |
||
59 | 'defines' => [], |
||
60 | 'params' => [], |
||
61 | ]; |
||
62 | |||
63 | protected $aliases = []; |
||
64 | |||
65 | protected $extensions = []; |
||
66 | |||
67 | /** |
||
68 | * @var array array of not yet merged params |
||
69 | */ |
||
70 | protected $rawParams = []; |
||
71 | |||
72 | /** |
||
73 | * @var Composer instance |
||
74 | */ |
||
75 | protected $composer; |
||
76 | |||
77 | /** |
||
78 | * @var IOInterface |
||
79 | */ |
||
80 | public $io; |
||
81 | |||
82 | /** |
||
83 | * Initializes the plugin object with the passed $composer and $io. |
||
84 | * @param Composer $composer |
||
85 | * @param IOInterface $io |
||
86 | */ |
||
87 | 2 | public function activate(Composer $composer, IOInterface $io) |
|
92 | |||
93 | /** |
||
94 | * Returns list of events the plugin is subscribed to. |
||
95 | * @return array list of events |
||
96 | */ |
||
97 | 1 | public static function getSubscribedEvents() |
|
98 | { |
||
99 | return [ |
||
100 | 1 | ScriptEvents::POST_AUTOLOAD_DUMP => [ |
|
101 | 1 | ['onPostAutoloadDump', 0], |
|
102 | 1 | ], |
|
103 | 1 | ]; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * This is the main function. |
||
108 | * @param Event $event |
||
109 | */ |
||
110 | public function onPostAutoloadDump(Event $event) |
||
124 | |||
125 | protected function initAutoload() |
||
129 | |||
130 | protected function scanPackages() |
||
131 | { |
||
132 | foreach ($this->getPackages() as $package) { |
||
133 | if ($package instanceof CompletePackageInterface) { |
||
134 | $this->processPackage($package); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Scans the given package and collects extensions data. |
||
141 | * @param PackageInterface $package |
||
142 | */ |
||
143 | protected function processPackage(CompletePackageInterface $package) |
||
144 | { |
||
145 | $extra = $package->getExtra(); |
||
146 | $files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; |
||
147 | |||
148 | if ($package->getType() !== self::YII2_PACKAGE_TYPE && is_null($files)) { |
||
149 | return; |
||
150 | } |
||
151 | |||
152 | if (is_array($files)) { |
||
153 | $this->addFiles($package, $files); |
||
154 | } |
||
155 | if ($package instanceof RootPackageInterface) { |
||
156 | $this->loadDotEnv($package); |
||
157 | } |
||
158 | |||
159 | $aliases = $this->collectAliases($package); |
||
160 | $this->aliases = array_merge($this->aliases, $aliases); |
||
161 | |||
162 | $this->extensions[$package->getPrettyName()] = array_filter([ |
||
163 | 'name' => $package->getPrettyName(), |
||
164 | 'version' => $package->getVersion(), |
||
165 | 'reference' => $package->getSourceReference() ?: $package->getDistReference(), |
||
166 | 'aliases' => $aliases, |
||
167 | ]); |
||
168 | } |
||
169 | |||
170 | protected function loadDotEnv(RootPackageInterface $package) |
||
171 | { |
||
172 | $path = $this->preparePath($package, '.env'); |
||
173 | if (file_exists($path) && class_exists('Dotenv\Dotenv')) { |
||
174 | array_push($this->files['dotenv'], $path); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Adds given files to the list of files to be processed. |
||
180 | * Prepares `defines` in reversed order (outer package first) because |
||
181 | * constants cannot be redefined. |
||
182 | * @param CompletePackageInterface $package |
||
183 | * @param array $files |
||
184 | */ |
||
185 | protected function addFiles(CompletePackageInterface $package, array $files) |
||
186 | { |
||
187 | foreach ($files as $name => $paths) { |
||
188 | $paths = (array) $paths; |
||
189 | if ($name === 'defines') { |
||
190 | $paths = array_reverse($paths); |
||
191 | } |
||
192 | foreach ($paths as $path) { |
||
193 | if (!isset($this->files[$name])) { |
||
194 | $this->files[$name] = []; |
||
195 | } |
||
196 | $path = $this->preparePath($package, $path); |
||
197 | if ($name === 'defines') { |
||
198 | array_unshift($this->files[$name], $path); |
||
199 | } else { |
||
200 | array_push($this->files[$name], $path); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Collects package aliases. |
||
208 | * @param CompletePackageInterface $package |
||
209 | * @return array collected aliases |
||
210 | */ |
||
211 | protected function collectAliases(CompletePackageInterface $package) |
||
212 | { |
||
213 | $aliases = array_merge( |
||
214 | $this->prepareAliases($package, 'psr-0'), |
||
215 | $this->prepareAliases($package, 'psr-4') |
||
216 | ); |
||
217 | if ($package instanceof RootPackageInterface) { |
||
218 | $aliases = array_merge($aliases, |
||
219 | $this->prepareAliases($package, 'psr-0', true), |
||
220 | $this->prepareAliases($package, 'psr-4', true) |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | return $aliases; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Prepare aliases. |
||
229 | * @param PackageInterface $package |
||
230 | * @param string 'psr-0' or 'psr-4' |
||
231 | * @return array |
||
232 | */ |
||
233 | protected function prepareAliases(PackageInterface $package, $psr, $dev = false) |
||
234 | { |
||
235 | $autoload = $dev ? $package->getDevAutoload() : $package->getAutoload(); |
||
236 | if (empty($autoload[$psr])) { |
||
237 | return []; |
||
238 | } |
||
239 | |||
240 | $aliases = []; |
||
241 | foreach ($autoload[$psr] as $name => $path) { |
||
242 | if (is_array($path)) { |
||
243 | // ignore psr-4 autoload specifications with multiple search paths |
||
244 | // we can not convert them into aliases as they are ambiguous |
||
245 | continue; |
||
246 | } |
||
247 | $name = str_replace('\\', '/', trim($name, '\\')); |
||
248 | $path = $this->preparePath($package, $path); |
||
249 | if ('psr-0' === $psr) { |
||
250 | $path .= '/' . $name; |
||
251 | } |
||
252 | $aliases["@$name"] = $path; |
||
253 | } |
||
254 | |||
255 | return $aliases; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Builds path inside of a package. |
||
260 | * @param PackageInterface $package |
||
261 | * @param mixed $path can be absolute or relative |
||
262 | * @return string absolute paths will stay untouched |
||
263 | */ |
||
264 | public function preparePath(PackageInterface $package, $path) |
||
265 | { |
||
266 | if (strncmp($path, '$', 1) === 0) { |
||
267 | return $path; |
||
268 | } |
||
269 | |||
270 | $skippable = strncmp($path, '?', 1) === 0 ? '?' : ''; |
||
271 | if ($skippable) { |
||
272 | $path = substr($path, 1); |
||
273 | } |
||
274 | |||
275 | if (!$this->getFilesystem()->isAbsolutePath($path)) { |
||
276 | $prefix = $package instanceof RootPackageInterface |
||
277 | ? $this->getBaseDir() |
||
278 | : $this->getVendorDir() . '/' . $package->getPrettyName(); |
||
279 | $path = $prefix . '/' . $path; |
||
280 | } |
||
281 | |||
282 | return $skippable . $this->getFilesystem()->normalizePath($path); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Sets [[packages]]. |
||
287 | * @param PackageInterface[] $packages |
||
288 | */ |
||
289 | 2 | public function setPackages(array $packages) |
|
293 | |||
294 | /** |
||
295 | * Gets [[packages]]. |
||
296 | * @return \Composer\Package\PackageInterface[] |
||
297 | */ |
||
298 | 1 | public function getPackages() |
|
299 | { |
||
300 | 1 | if ($this->packages === null) { |
|
301 | $this->packages = $this->findPackages(); |
||
306 | |||
307 | /** |
||
308 | * Plain list of all project dependencies (including nested) as provided by composer. |
||
309 | * The list is unordered (chaotic, can be different after every update). |
||
310 | */ |
||
311 | protected $plainList = []; |
||
312 | |||
313 | /** |
||
314 | * Ordered list of package. Order @see findPackages. |
||
315 | */ |
||
316 | protected $orderedList = []; |
||
317 | |||
318 | /** |
||
319 | * Returns ordered list of packages: |
||
320 | * - listed earlier in the composer.json will get earlier in the list |
||
321 | * - childs before parents. |
||
322 | * @return \Composer\Package\PackageInterface[] |
||
323 | */ |
||
324 | public function findPackages() |
||
346 | |||
347 | /** |
||
348 | * Iterates through package dependencies. |
||
349 | * @param PackageInterface $package to iterate |
||
350 | * @param bool $includingDev process development dependencies, defaults to not process |
||
351 | */ |
||
352 | public function iteratePackage(PackageInterface $package, $includingDev = false) |
||
372 | |||
373 | /** |
||
374 | * Iterates dependencies of the given package. |
||
375 | * @param PackageInterface $package |
||
376 | * @param bool $dev which dependencies to iterate: true - dev, default - general |
||
377 | */ |
||
378 | public function iterateDependencies(PackageInterface $package, $dev = false) |
||
394 | |||
395 | /** |
||
396 | * Get absolute path to package base dir. |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getBaseDir() |
||
407 | |||
408 | /** |
||
409 | * Get absolute path to composer vendor dir. |
||
410 | * @return string |
||
411 | */ |
||
412 | public function getVendorDir() |
||
421 | |||
422 | /** |
||
423 | * Getter for filesystem utility. |
||
424 | * @return Filesystem |
||
425 | */ |
||
426 | public function getFilesystem() |
||
434 | } |
||
435 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.