| Total Complexity | 45 |
| Total Lines | 279 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 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 |
||
| 33 | class AbstractCommand extends Command |
||
| 34 | { |
||
| 35 | public const CONFIG_FILE = ['cecil.yml', 'config.yml']; |
||
| 36 | public const TMP_DIR = '.cecil'; |
||
| 37 | public const THEME_CONFIG_FILE = 'config.yml'; |
||
| 38 | public const EXCLUDED_CMD = ['about', 'new:site', 'self-update']; |
||
| 39 | |||
| 40 | /** @var InputInterface */ |
||
| 41 | protected $input; |
||
| 42 | |||
| 43 | /** @var OutputInterface */ |
||
| 44 | protected $output; |
||
| 45 | |||
| 46 | /** @var SymfonyStyle */ |
||
| 47 | protected $io; |
||
| 48 | |||
| 49 | /** @var null|string */ |
||
| 50 | private $path = null; |
||
| 51 | |||
| 52 | /** @var array */ |
||
| 53 | private $configFiles = []; |
||
| 54 | |||
| 55 | /** @var Config */ |
||
| 56 | private $config; |
||
| 57 | |||
| 58 | /** @var Builder */ |
||
| 59 | private $builder; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 65 | { |
||
| 66 | $this->input = $input; |
||
| 67 | $this->output = $output; |
||
| 68 | $this->io = new SymfonyStyle($input, $output); |
||
| 69 | |||
| 70 | // prepare configuration files list |
||
| 71 | if (!\in_array($this->getName(), self::EXCLUDED_CMD)) { |
||
| 72 | // site config file |
||
| 73 | $this->configFiles[$this->locateConfigFile($this->getPath())['name']] = $this->locateConfigFile($this->getPath())['path']; |
||
| 74 | // additional config file(s) from --config=<file> |
||
| 75 | if ($input->hasOption('config') && $input->getOption('config') !== null) { |
||
| 76 | $this->configFiles += $this->locateAdditionalConfigFiles($this->getPath(), (string) $input->getOption('config')); |
||
| 77 | } |
||
| 78 | // checks file(s) |
||
| 79 | $this->configFiles = array_unique($this->configFiles); |
||
| 80 | foreach ($this->configFiles as $fileName => $filePath) { |
||
| 81 | if ($filePath === false) { |
||
| 82 | unset($this->configFiles[$fileName]); |
||
| 83 | $this->io->warning(\sprintf('Could not find configuration file "%s".', $fileName)); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | parent::initialize($input, $output); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function run(InputInterface $input, OutputInterface $output): int |
||
| 95 | { |
||
| 96 | // disable debug mode if a verbosity level is specified |
||
| 97 | if ($output->getVerbosity() != OutputInterface::VERBOSITY_NORMAL) { |
||
| 98 | putenv('CECIL_DEBUG=false'); |
||
| 99 | } |
||
| 100 | // force verbosity level to "debug" in debug mode |
||
| 101 | if (getenv('CECIL_DEBUG') == 'true') { |
||
| 102 | $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
||
| 103 | } |
||
| 104 | if ($output->isDebug()) { |
||
| 105 | // set env. variable in debug mode |
||
| 106 | putenv('CECIL_DEBUG=true'); |
||
| 107 | |||
| 108 | return parent::run($input, $output); |
||
| 109 | } |
||
| 110 | // run with simplified error message |
||
| 111 | try { |
||
| 112 | return parent::run($input, $output); |
||
| 113 | } catch (\Exception $e) { |
||
| 114 | if ($this->io === null) { |
||
| 115 | $this->io = new SymfonyStyle($input, $output); |
||
| 116 | } |
||
| 117 | $this->io->error($e->getMessage()); |
||
| 118 | |||
| 119 | exit(1); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the working path. |
||
| 125 | */ |
||
| 126 | protected function getPath(bool $exist = true): ?string |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns config file(s) path. |
||
| 155 | */ |
||
| 156 | protected function getConfigFiles(): array |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Creates or returns a Builder instance. |
||
| 163 | * |
||
| 164 | * @throws RuntimeException |
||
| 165 | */ |
||
| 166 | protected function getBuilder(array $config = []): Builder |
||
| 167 | { |
||
| 168 | try { |
||
| 169 | // loads configuration files if not already done |
||
| 170 | if ($this->config === null) { |
||
| 171 | $this->config = new Config(); |
||
| 172 | // loads and merges configuration files |
||
| 173 | foreach ($this->getConfigFiles() as $filePath) { |
||
| 174 | $this->config->import($this->config->loadFile($filePath), Config::MERGE); |
||
| 175 | } |
||
| 176 | // merges configuration from $config parameter |
||
| 177 | $this->config->import($config, Config::MERGE); |
||
| 178 | } |
||
| 179 | // creates builder instance if not already done |
||
| 180 | if ($this->builder === null) { |
||
| 181 | $this->builder = (new Builder($this->config, new ConsoleLogger($this->output))) |
||
| 182 | ->setSourceDir($this->getPath()) |
||
| 183 | ->setDestinationDir($this->getPath()); |
||
| 184 | // import themes config |
||
| 185 | // @todo Move this to Config class |
||
| 186 | $themes = (array) $this->builder->getConfig()->getTheme(); |
||
| 187 | foreach ($themes as $theme) { |
||
| 188 | $themeConfigFile = Util::joinFile($this->builder->getConfig()->getThemesPath(), $theme, self::THEME_CONFIG_FILE); |
||
| 189 | if (Util\File::getFS()->exists($themeConfigFile)) { |
||
| 190 | if (false === $themeFileContent = Util\File::fileGetContents($themeConfigFile)) { |
||
| 191 | throw new ConfigException(\sprintf('Can\'t read file "themes/%s/%s".', $theme, self::THEME_CONFIG_FILE)); |
||
| 192 | } |
||
| 193 | $themeConfig = Yaml::parse($themeFileContent, Yaml::PARSE_DATETIME); |
||
| 194 | $this->builder->getConfig()->import($themeConfig ?? [], Config::PRESERVE); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } catch (\Exception $e) { |
||
| 199 | throw new RuntimeException($e->getMessage()); |
||
| 200 | } |
||
| 201 | |||
| 202 | return $this->builder; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Locates the configuration in the given path, as an array of the file name and path, if file exists, otherwise default name and false. |
||
| 207 | */ |
||
| 208 | protected function locateConfigFile(string $path): array |
||
| 209 | { |
||
| 210 | $config = [ |
||
| 211 | 'name' => self::CONFIG_FILE[0], |
||
| 212 | 'path' => false, |
||
| 213 | ]; |
||
| 214 | foreach (self::CONFIG_FILE as $configFileName) { |
||
| 215 | if (($configFilePath = realpath(Util::joinFile($path, $configFileName))) !== false) { |
||
| 216 | $config = [ |
||
| 217 | 'name' => $configFileName, |
||
| 218 | 'path' => $configFilePath, |
||
| 219 | ]; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | return $config; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Locates additional configuration file(s) from the given list of files, relative to the given path or absolute. |
||
| 228 | */ |
||
| 229 | protected function locateAdditionalConfigFiles(string $path, string $configFilesList): array |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Opens path with editor. |
||
| 245 | * |
||
| 246 | * @throws RuntimeException |
||
| 247 | */ |
||
| 248 | protected function openEditor(string $path, string $editor): void |
||
| 249 | { |
||
| 250 | $command = \sprintf('%s "%s"', $editor, $path); |
||
| 251 | switch (Util\Platform::getOS()) { |
||
| 252 | case Util\Platform::OS_WIN: |
||
| 253 | $command = \sprintf('start /B "" %s "%s"', $editor, $path); |
||
| 254 | break; |
||
| 255 | case Util\Platform::OS_OSX: |
||
| 256 | // Typora on macOS |
||
| 257 | if ($editor == 'typora') { |
||
| 258 | $command = \sprintf('open -a typora "%s"', $path); |
||
| 259 | } |
||
| 260 | break; |
||
| 261 | } |
||
| 262 | $process = Process::fromShellCommandline($command); |
||
| 263 | $process->run(); |
||
| 264 | if (!$process->isSuccessful()) { |
||
| 265 | throw new RuntimeException(\sprintf('Can\'t use "%s" editor.', $editor)); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Validate URL. |
||
| 271 | * |
||
| 272 | * @throws RuntimeException |
||
| 273 | */ |
||
| 274 | public static function validateUrl(string $url): string |
||
| 275 | { |
||
| 276 | $validator = Validation::createValidator(); |
||
| 277 | $violations = $validator->validate($url, new Url()); |
||
| 278 | if (\count($violations) > 0) { |
||
| 279 | foreach ($violations as $violation) { |
||
| 280 | throw new RuntimeException($violation->getMessage()); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | return rtrim($url, '/') . '/'; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the "binary name" in the console context. |
||
| 288 | */ |
||
| 289 | protected function binName(): string |
||
| 290 | { |
||
| 291 | return basename($_SERVER['argv'][0]); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Override default help message. |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function getProcessedHelp(): string |
||
| 312 | } |
||
| 313 | } |
||
| 314 |