Total Complexity | 43 |
Total Lines | 230 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AbstractCommand 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 AbstractCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class AbstractCommand extends Command |
||
32 | { |
||
33 | public const CONFIG_FILE = ['cecil.yml', 'config.yml']; |
||
34 | public const TMP_DIR = '.cecil'; |
||
35 | |||
36 | /** @var InputInterface */ |
||
37 | protected $input; |
||
38 | |||
39 | /** @var OutputInterface */ |
||
40 | protected $output; |
||
41 | |||
42 | /** @var SymfonyStyle */ |
||
43 | protected $io; |
||
44 | |||
45 | /** @var null|string */ |
||
46 | private $path = null; |
||
47 | |||
48 | /** @var array */ |
||
49 | private $configFiles; |
||
50 | |||
51 | /** @var array */ |
||
52 | private $config; |
||
53 | |||
54 | /** @var Builder */ |
||
55 | private $builder; |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
61 | { |
||
62 | $this->input = $input; |
||
63 | $this->output = $output; |
||
64 | $this->io = new SymfonyStyle($input, $output); |
||
65 | |||
66 | // set up configuration |
||
67 | if (!\in_array($this->getName(), ['new:site', 'self-update'])) { |
||
68 | // default configuration file |
||
69 | $this->configFiles[$this->findConfigFile('name')] = $this->findConfigFile('path'); |
||
70 | // from --config=<file> |
||
71 | if ($input->hasOption('config') && $input->getOption('config') !== null) { |
||
72 | foreach (explode(',', (string) $input->getOption('config')) as $configFile) { |
||
73 | $this->configFiles[$configFile] = realpath($configFile); |
||
74 | if (!Util\File::getFS()->isAbsolutePath($configFile)) { |
||
75 | $this->configFiles[$configFile] = realpath(Util::joinFile($this->getPath(), $configFile)); |
||
76 | } |
||
77 | } |
||
78 | $this->configFiles = array_unique($this->configFiles); |
||
79 | } |
||
80 | // checks file(s) |
||
81 | foreach ($this->configFiles as $fileName => $filePath) { |
||
82 | if ($filePath === false || !file_exists($filePath)) { |
||
83 | unset($this->configFiles[$fileName]); |
||
84 | $this->getBuilder()->getLogger()->error(sprintf('Could not find configuration file "%s".', $fileName)); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | if ($this->getName() == 'new:site') { |
||
89 | Util\File::getFS()->mkdir($this->getPath(true)); |
||
90 | } |
||
91 | |||
92 | parent::initialize($input, $output); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function run(InputInterface $input, OutputInterface $output): int |
||
99 | { |
||
100 | // disable debug mode if a verbosity level is specified |
||
101 | if ($output->getVerbosity() != OutputInterface::VERBOSITY_NORMAL) { |
||
102 | putenv('CECIL_DEBUG=false'); |
||
103 | } |
||
104 | // force verbosity level to "debug" in debug mode |
||
105 | if (getenv('CECIL_DEBUG') == 'true') { |
||
106 | $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
||
107 | } |
||
108 | if ($output->isDebug()) { |
||
109 | // set env. variable in debug mode |
||
110 | putenv('CECIL_DEBUG=true'); |
||
111 | |||
112 | return parent::run($input, $output); |
||
113 | } |
||
114 | // simplified error message |
||
115 | try { |
||
116 | return parent::run($input, $output); |
||
117 | } catch (\Exception $e) { |
||
118 | if ($this->io === null) { |
||
119 | $this->io = new SymfonyStyle($input, $output); |
||
120 | } |
||
121 | $this->io->error($e->getMessage()); |
||
122 | |||
123 | exit(1); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Returns the working path. |
||
129 | */ |
||
130 | protected function getPath(bool $create = false): ?string |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Returns the configuration file name or path, if file exists, otherwise default name or false. |
||
159 | */ |
||
160 | protected function findConfigFile(string $nameOrPath): string|false |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Returns config file(s) path. |
||
180 | */ |
||
181 | protected function getConfigFiles(): ?array |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Creates or returns a Builder instance. |
||
188 | * |
||
189 | * @throws RuntimeException |
||
190 | */ |
||
191 | protected function getBuilder(array $config = []): Builder |
||
192 | { |
||
193 | try { |
||
194 | // config |
||
195 | if ($this->config === null) { |
||
196 | $siteConfig = []; |
||
197 | foreach ($this->getConfigFiles() as $fileName => $filePath) { |
||
198 | if ($filePath === false || false === $configContent = Util\File::fileGetContents($filePath)) { |
||
199 | throw new RuntimeException(sprintf('Can\'t read configuration file "%s".', $fileName)); |
||
200 | } |
||
201 | $siteConfig = array_replace_recursive($siteConfig, (array) Yaml::parse($configContent)); |
||
202 | } |
||
203 | $this->config = array_replace_recursive($siteConfig, $config); |
||
204 | } |
||
205 | // builder |
||
206 | if ($this->builder === null) { |
||
207 | $this->builder = (new Builder($this->config, new ConsoleLogger($this->output))) |
||
208 | ->setSourceDir($this->getPath()) |
||
209 | ->setDestinationDir($this->getPath()); |
||
210 | } |
||
211 | } catch (ParseException $e) { |
||
212 | throw new RuntimeException(sprintf('Configuration parsing error: %s', $e->getMessage())); |
||
213 | } catch (\Exception $e) { |
||
214 | throw new RuntimeException($e->getMessage()); |
||
215 | } |
||
216 | |||
217 | return $this->builder; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Opens path with editor. |
||
222 | * |
||
223 | * @throws RuntimeException |
||
224 | */ |
||
225 | protected function openEditor(string $path, string $editor): void |
||
226 | { |
||
227 | $command = sprintf('%s "%s"', $editor, $path); |
||
228 | switch (Util\Plateform::getOS()) { |
||
229 | case Util\Plateform::OS_WIN: |
||
230 | $command = sprintf('start /B "" %s "%s"', $editor, $path); |
||
231 | break; |
||
232 | case Util\Plateform::OS_OSX: |
||
233 | // Typora on macOS |
||
234 | if ($editor == 'typora') { |
||
235 | $command = sprintf('open -a typora "%s"', $path); |
||
236 | } |
||
237 | break; |
||
238 | } |
||
239 | $process = Process::fromShellCommandline($command); |
||
240 | $process->run(); |
||
241 | if (!$process->isSuccessful()) { |
||
242 | throw new RuntimeException(sprintf('Can\'t use "%s" editor.', $editor)); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Validate URL. |
||
248 | * |
||
249 | * @throws RuntimeException |
||
250 | */ |
||
251 | public static function validateUrl(string $url): string |
||
261 | } |
||
262 | } |
||
263 |