| Total Complexity | 55 |
| Total Lines | 328 |
| 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 |
||
| 38 | class AbstractCommand extends Command |
||
| 39 | { |
||
| 40 | public const CONFIG_FILE = ['cecil.yml', 'config.yml']; |
||
| 41 | public const TMP_DIR = '.cecil'; |
||
| 42 | public const EXCLUDED_CMD = ['about', 'new:site', 'self-update']; |
||
| 43 | public const SERVE_OUTPUT = '.cecil/preview'; |
||
| 44 | |||
| 45 | /** @var InputInterface */ |
||
| 46 | protected $input; |
||
| 47 | |||
| 48 | /** @var OutputInterface */ |
||
| 49 | protected $output; |
||
| 50 | |||
| 51 | /** @var SymfonyStyle */ |
||
| 52 | protected $io; |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | protected $rootPath; |
||
| 56 | |||
| 57 | /** @var null|string */ |
||
| 58 | private $path = null; |
||
| 59 | |||
| 60 | /** @var array */ |
||
| 61 | private $configFiles = []; |
||
| 62 | |||
| 63 | /** @var Config */ |
||
| 64 | private $config; |
||
| 65 | |||
| 66 | /** @var Builder */ |
||
| 67 | private $builder; |
||
| 68 | |||
| 69 | /** @var ContainerInterface|null */ |
||
| 70 | private $container; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 76 | { |
||
| 77 | $this->input = $input; |
||
| 78 | $this->output = $output; |
||
| 79 | $this->io = new SymfonyStyle($input, $output); |
||
| 80 | $this->rootPath = (Util\Platform::isPhar() ? Util\Platform::getPharPath() : realpath(Util::joinFile(__DIR__, '/../../'))) . '/'; |
||
| 81 | |||
| 82 | // prepare configuration files list |
||
| 83 | if (!\in_array($this->getName(), self::EXCLUDED_CMD)) { |
||
| 84 | // site config file |
||
| 85 | $this->configFiles[$this->locateConfigFile($this->getPath())['name']] = $this->locateConfigFile($this->getPath())['path']; |
||
| 86 | // additional config file(s) from --config=<file> |
||
| 87 | if ($input->hasOption('config') && $input->getOption('config') !== null) { |
||
| 88 | $this->configFiles += $this->locateAdditionalConfigFiles($this->getPath(), (string) $input->getOption('config')); |
||
| 89 | } |
||
| 90 | // checks file(s) |
||
| 91 | $this->configFiles = array_unique($this->configFiles); |
||
| 92 | foreach ($this->configFiles as $fileName => $filePath) { |
||
| 93 | if ($filePath === false) { |
||
| 94 | unset($this->configFiles[$fileName]); |
||
| 95 | $this->io->warning(\sprintf('Could not find configuration file "%s".', $fileName)); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | parent::initialize($input, $output); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | public function run(InputInterface $input, OutputInterface $output): int |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Send desktop notification. |
||
| 153 | */ |
||
| 154 | public function notification(string $body, ?string $url = null): void |
||
| 155 | { |
||
| 156 | $notifier = new DefaultNotifier(); |
||
| 157 | $notification = (new Notification()) |
||
| 158 | ->setTitle('Cecil') |
||
| 159 | ->setIcon($this->rootPath . 'resources/icon.png') |
||
| 160 | ->setBody($body) |
||
| 161 | ; |
||
| 162 | if ($url !== null) { |
||
| 163 | $notification->addOption('url', $url); |
||
| 164 | } |
||
| 165 | if (false === $notifier->send($notification)) { |
||
| 166 | $this->output->writeln('<comment>Desktop notification could not be sent</comment>'); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the working path. |
||
| 172 | */ |
||
| 173 | protected function getPath(bool $exist = true): ?string |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns config file(s) path. |
||
| 202 | */ |
||
| 203 | protected function getConfigFiles(): array |
||
| 204 | { |
||
| 205 | return $this->configFiles ?? []; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Sets the DI container. |
||
| 210 | */ |
||
| 211 | public function setContainer(?ContainerInterface $container): void |
||
| 212 | { |
||
| 213 | $this->container = $container; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Creates or returns a Builder instance. |
||
| 218 | * |
||
| 219 | * @throws RuntimeException |
||
| 220 | */ |
||
| 221 | protected function getBuilder(array $config = []): Builder |
||
| 222 | { |
||
| 223 | try { |
||
| 224 | // loads configuration files if not already done |
||
| 225 | if ($this->config === null) { |
||
| 226 | $this->config = new Config(); |
||
| 227 | // loads and merges configuration files |
||
| 228 | foreach ($this->getConfigFiles() as $filePath) { |
||
| 229 | $this->config->import($this->config::loadFile($filePath), Config::IMPORT_MERGE); |
||
| 230 | } |
||
| 231 | // merges configuration from $config parameter |
||
| 232 | $this->config->import($config, Config::IMPORT_MERGE); |
||
| 233 | } |
||
| 234 | // creates builder instance if not already done |
||
| 235 | if ($this->builder === null) { |
||
| 236 | // Use container if available |
||
| 237 | if ($this->container !== null && $this->container->has('Cecil\\Builder')) { |
||
| 238 | $this->builder = $this->container->get('Cecil\\Builder'); |
||
| 239 | } else { |
||
| 240 | // Direct instantiation with fallback container |
||
| 241 | $fallbackContainer = $this->container ?? new \Symfony\Component\DependencyInjection\ContainerBuilder(); |
||
| 242 | $this->builder = new Builder($this->config, new ConsoleLogger($this->output), $fallbackContainer); |
||
| 243 | } |
||
| 244 | $this->builder |
||
| 245 | ->setSourceDir($this->getPath()) |
||
|
|
|||
| 246 | ->setDestinationDir($this->getPath()); |
||
| 247 | } |
||
| 248 | } catch (\Exception $e) { |
||
| 249 | throw new RuntimeException($e->getMessage()); |
||
| 250 | } |
||
| 251 | |||
| 252 | return $this->builder; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Locates the configuration in the given path, as an array of the file name and path, if file exists, otherwise default name and false. |
||
| 257 | */ |
||
| 258 | protected function locateConfigFile(string $path): array |
||
| 259 | { |
||
| 260 | $config = [ |
||
| 261 | 'name' => self::CONFIG_FILE[0], |
||
| 262 | 'path' => false, |
||
| 263 | ]; |
||
| 264 | foreach (self::CONFIG_FILE as $configFileName) { |
||
| 265 | if (($configFilePath = realpath(Util::joinFile($path, $configFileName))) !== false) { |
||
| 266 | $config = [ |
||
| 267 | 'name' => $configFileName, |
||
| 268 | 'path' => $configFilePath, |
||
| 269 | ]; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | return $config; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Locates additional configuration file(s) from the given list of files, relative to the given path or absolute. |
||
| 278 | */ |
||
| 279 | protected function locateAdditionalConfigFiles(string $path, string $configFilesList): array |
||
| 280 | { |
||
| 281 | $config = []; |
||
| 282 | foreach (explode(',', $configFilesList) as $filename) { |
||
| 283 | // absolute path |
||
| 284 | $config[$filename] = realpath($filename); |
||
| 285 | // relative path |
||
| 286 | if (!Util\File::getFS()->isAbsolutePath($filename)) { |
||
| 287 | $config[$filename] = realpath(Util::joinFile($path, $filename)); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | return $config; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Opens path with editor. |
||
| 296 | * |
||
| 297 | * @throws RuntimeException |
||
| 298 | */ |
||
| 299 | protected function openEditor(string $path, string $editor): void |
||
| 300 | { |
||
| 301 | $command = \sprintf('%s "%s"', $editor, $path); |
||
| 302 | switch (Util\Platform::getOS()) { |
||
| 303 | case Util\Platform::OS_WIN: |
||
| 304 | $command = \sprintf('start /B "" %s "%s"', $editor, $path); |
||
| 305 | break; |
||
| 306 | case Util\Platform::OS_OSX: |
||
| 307 | // Typora on macOS |
||
| 308 | if ($editor == 'typora') { |
||
| 309 | $command = \sprintf('open -a typora "%s"', $path); |
||
| 310 | } |
||
| 311 | break; |
||
| 312 | } |
||
| 313 | $process = Process::fromShellCommandline($command); |
||
| 314 | $process->run(); |
||
| 315 | if (!$process->isSuccessful()) { |
||
| 316 | throw new RuntimeException(\sprintf('Unable to use "%s" editor.', $editor)); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Validate URL. |
||
| 322 | * |
||
| 323 | * @throws RuntimeException |
||
| 324 | */ |
||
| 325 | public static function validateUrl(string $url): string |
||
| 326 | { |
||
| 327 | if ($url == '/') { // tolerate root URL |
||
| 328 | return $url; |
||
| 329 | } |
||
| 330 | $validator = Validation::createValidator(); |
||
| 331 | $violations = $validator->validate($url, new Url()); |
||
| 332 | if (\count($violations) > 0) { |
||
| 333 | foreach ($violations as $violation) { |
||
| 334 | throw new RuntimeException($violation->getMessage()); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | return rtrim($url, '/') . '/'; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns the "binary name" in the console context. |
||
| 342 | */ |
||
| 343 | protected function binName(): string |
||
| 344 | { |
||
| 345 | return basename($_SERVER['argv'][0]); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Override default help message. |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getProcessedHelp(): string |
||
| 366 | } |
||
| 367 | } |
||
| 368 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.