Total Complexity | 41 |
Total Lines | 244 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
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.
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 |
||
12 | final class Plugin |
||
13 | { |
||
14 | /** |
||
15 | * @var Package[] the array of active composer packages |
||
16 | */ |
||
17 | private array $packages = []; |
||
18 | |||
19 | private array $alternatives = []; |
||
20 | |||
21 | private ?string $outputDir = null; |
||
22 | |||
23 | private ?Package $rootPackage = null; |
||
24 | |||
25 | /** |
||
26 | * @var array config name => list of files |
||
27 | */ |
||
28 | private array $files = [ |
||
29 | 'dotenv' => [], |
||
30 | 'defines' => [], |
||
31 | 'params' => [], |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * @var array package name => configs as listed in `composer.json` |
||
36 | */ |
||
37 | private array $originalFiles = []; |
||
38 | |||
39 | /** |
||
40 | * @var Builder |
||
41 | */ |
||
42 | private Builder $builder; |
||
43 | |||
44 | /** |
||
45 | * @var Composer instance |
||
46 | */ |
||
47 | private Composer $composer; |
||
48 | |||
49 | /** |
||
50 | * @var IOInterface |
||
51 | */ |
||
52 | private IOInterface $io; |
||
53 | |||
54 | private PackageFinder $packageFinder; |
||
55 | |||
56 | /** |
||
57 | * Initializes the plugin object with the passed $composer and $io. |
||
58 | * |
||
59 | * @param Composer $composer |
||
60 | * @param IOInterface $io |
||
61 | */ |
||
62 | public function __construct(Composer $composer, IOInterface $io) |
||
63 | { |
||
64 | $this->builder = new Builder(); |
||
65 | $this->packageFinder = new PackageFinder($composer); |
||
66 | $this->composer = $composer; |
||
67 | $this->io = $io; |
||
68 | } |
||
69 | |||
70 | public function build(): void |
||
71 | { |
||
72 | $this->io->overwriteError('<info>Assembling config files</info>'); |
||
73 | |||
74 | $this->scanPackages(); |
||
75 | $this->reorderFiles(); |
||
76 | |||
77 | $this->builder->setOutputDir($this->outputDir); |
||
78 | $this->builder->buildAllConfigs($this->files); |
||
79 | |||
80 | $saveFiles = $this->files; |
||
81 | $saveEnv = $_ENV; |
||
82 | foreach ($this->alternatives as $name => $files) { |
||
83 | $this->files = $saveFiles; |
||
84 | $_ENV = $saveEnv; |
||
85 | $builder = $this->builder->createAlternative($name); |
||
86 | $this->addFiles($this->rootPackage, $files); |
||
|
|||
87 | $builder->buildAllConfigs($this->files); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | private function scanPackages(): void |
||
92 | { |
||
93 | foreach ($this->getPackages() as $package) { |
||
94 | if ($package->isComplete()) { |
||
95 | $this->processPackage($package); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | private function reorderFiles(): void |
||
101 | { |
||
102 | foreach (array_keys($this->files) as $name) { |
||
103 | $this->files[$name] = $this->getAllFiles($name); |
||
104 | } |
||
105 | foreach ($this->files as $name => $files) { |
||
106 | $this->files[$name] = $this->orderFiles($files); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | private function getAllFiles(string $name, array $stack = []): array |
||
111 | { |
||
112 | if (empty($this->files[$name])) { |
||
113 | return[]; |
||
114 | } |
||
115 | $res = []; |
||
116 | foreach ($this->files[$name] as $file) { |
||
117 | if (strncmp($file, '$', 1) === 0) { |
||
118 | if (!in_array($name, $stack, true)) { |
||
119 | $res = array_merge($res, $this->getAllFiles(substr($file, 1), array_merge($stack, [$name]))); |
||
120 | } |
||
121 | } else { |
||
122 | $res[] = $file; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | return $res; |
||
127 | } |
||
128 | |||
129 | private function orderFiles(array $files): array |
||
130 | { |
||
131 | if (empty($files)) { |
||
132 | return []; |
||
133 | } |
||
134 | $keys = array_combine($files, $files); |
||
135 | $res = []; |
||
136 | foreach ($this->orderedFiles as $file) { |
||
137 | if (isset($keys[$file])) { |
||
138 | $res[$file] = $file; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | return array_values($res); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Scans the given package and collects packages data. |
||
147 | * @param Package $package |
||
148 | */ |
||
149 | private function processPackage(Package $package) |
||
150 | { |
||
151 | $files = $package->getFiles(); |
||
152 | $this->originalFiles[$package->getPrettyName()] = $files; |
||
153 | |||
154 | if (!empty($files)) { |
||
155 | $this->addFiles($package, $files); |
||
156 | } |
||
157 | if ($package->isRoot()) { |
||
158 | $this->rootPackage = $package; |
||
159 | $this->loadDotEnv($package); |
||
160 | $devFiles = $package->getDevFiles(); |
||
161 | if (!empty($devFiles)) { |
||
162 | $this->addFiles($package, $devFiles); |
||
163 | } |
||
164 | $this->outputDir = $package->getOutputDir(); |
||
165 | $alternatives = $package->getAlternatives(); |
||
166 | if (is_string($alternatives)) { |
||
167 | $this->alternatives = $this->readConfig($package, $alternatives); |
||
168 | } elseif (is_array($alternatives)) { |
||
169 | $this->alternatives = $alternatives; |
||
170 | } elseif (!empty($alternatives)) { |
||
171 | throw new BadConfigurationException('Alternatives must be array or path to configuration file.'); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | $aliases = $package->collectAliases(); |
||
176 | |||
177 | $this->builder->mergeAliases($aliases); |
||
178 | $this->builder->setPackage($package->getPrettyName(), array_filter([ |
||
179 | 'name' => $package->getPrettyName(), |
||
180 | 'version' => $package->getVersion(), |
||
181 | 'reference' => $package->getSourceReference() ?: $package->getDistReference(), |
||
182 | 'aliases' => $aliases, |
||
183 | ])); |
||
184 | } |
||
185 | |||
186 | private function readConfig($package, $file): array |
||
187 | { |
||
188 | $path = $package->preparePath($file); |
||
189 | if (!file_exists($path)) { |
||
190 | throw new FailedReadException("failed read file: $file"); |
||
191 | } |
||
192 | $reader = ReaderFactory::get($this->builder, $path); |
||
193 | |||
194 | return $reader->read($path); |
||
195 | } |
||
196 | |||
197 | private function loadDotEnv(Package $package): void |
||
198 | { |
||
199 | $path = $package->preparePath('.env'); |
||
200 | if (file_exists($path) && class_exists('Dotenv\Dotenv')) { |
||
201 | $this->addFile($package, 'dotenv', $path); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Adds given files to the list of files to be processed. |
||
207 | * Prepares `defines` in reversed order (outer package first) because |
||
208 | * constants cannot be redefined. |
||
209 | * @param Package $package |
||
210 | * @param array $files |
||
211 | */ |
||
212 | private function addFiles(Package $package, array $files): void |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | private array $orderedFiles = []; |
||
226 | |||
227 | private function addFile(Package $package, string $name, string $path): void |
||
228 | { |
||
242 | } |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Gets [[packages]]. |
||
247 | * @return Package[] |
||
248 | */ |
||
249 | private function getPackages(): array |
||
258 |