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