| Conditions | 15 |
| Paths | 461 |
| Total Lines | 77 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 55 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 56 | { |
||
| 57 | $name = (string) $input->getOption('name'); |
||
| 58 | $prefix = $input->getOption('prefix'); |
||
| 59 | $force = $input->getOption('force'); |
||
| 60 | $open = $input->getOption('open'); |
||
| 61 | $editor = $input->getOption('editor'); |
||
| 62 | |||
| 63 | try { |
||
| 64 | // ask |
||
| 65 | if (empty($name)) { |
||
| 66 | $name = $this->io->ask('What is the name of the page file?', 'new-page.md'); |
||
| 67 | $prefix = $this->io->confirm('Add date prefix to the filename?', false); |
||
| 68 | $open = $this->io->confirm('Do you want open the created file with your editor?', false); |
||
| 69 | if ($open && !$this->getBuilder()->getConfig()->has('editor')) { |
||
| 70 | $editor = $this->io->ask('Which editor?'); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | // parse given path name |
||
| 74 | $nameParts = pathinfo($name); |
||
| 75 | $dirname = trim($nameParts['dirname'], '.'); |
||
| 76 | $basename = $nameParts['basename']; |
||
| 77 | $extension = $nameParts['extension']; |
||
| 78 | $title = substr($basename, 0, -\strlen(".$extension")); |
||
| 79 | $filename = $basename; |
||
| 80 | if (!\in_array($extension, (array) $this->getBuilder()->getConfig()->get('pages.ext'))) { |
||
| 81 | $title = $filename; |
||
| 82 | $filename = "$basename.md"; // force a valid extension |
||
| 83 | } |
||
| 84 | $title = ucfirst(str_replace('-', ' ', $title)); |
||
| 85 | $date = date('Y-m-d'); |
||
| 86 | // date prefix? |
||
| 87 | $datePrefix = $prefix ? sprintf('%s-', $date) : ''; |
||
| 88 | // define target path |
||
| 89 | $fileRelativePath = sprintf( |
||
| 90 | '%s%s%s%s%s', |
||
| 91 | (string) $this->getBuilder()->getConfig()->get('pages.dir'), |
||
| 92 | DIRECTORY_SEPARATOR, |
||
| 93 | empty($dirname) ? '' : $dirname . DIRECTORY_SEPARATOR, |
||
| 94 | $datePrefix, |
||
| 95 | $filename |
||
| 96 | ); |
||
| 97 | $filePath = Util::joinFile($this->getPath(), $fileRelativePath); |
||
| 98 | // ask to override existing file? |
||
| 99 | if (Util\File::getFS()->exists($filePath) && !$force) { |
||
| 100 | $output->writeln(sprintf('<comment>The file "%s" already exists.</comment>', $fileRelativePath)); |
||
| 101 | if (!$this->io->confirm('Do you want to override it?', false)) { |
||
| 102 | return 0; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | // creates a new file |
||
| 106 | $model = $this->findModel(sprintf('%s%s', empty($dirname) ? '' : $dirname . DIRECTORY_SEPARATOR, $filename)); |
||
| 107 | $fileContent = str_replace( |
||
| 108 | ['%title%', '%date%'], |
||
| 109 | [$title, $date], |
||
| 110 | $model['content'] |
||
| 111 | ); |
||
| 112 | Util\File::getFS()->dumpFile($filePath, $fileContent); |
||
| 113 | $output->writeln(sprintf('<info>File "%s" created (with model "%s").</info>', $fileRelativePath, $model['name'])); |
||
| 114 | // open editor? |
||
| 115 | if ($open) { |
||
| 116 | if ($editor === null) { |
||
| 117 | if (!$this->getBuilder()->getConfig()->has('editor')) { |
||
| 118 | $output->writeln('<comment>No editor configured.</comment>'); |
||
| 119 | |||
| 120 | return 0; |
||
| 121 | } |
||
| 122 | $editor = (string) $this->getBuilder()->getConfig()->get('editor'); |
||
| 123 | } |
||
| 124 | $output->writeln(sprintf('<info>Opening file with %s...</info>', ucfirst($editor))); |
||
| 125 | $this->openEditor($filePath, $editor); |
||
| 126 | } |
||
| 127 | } catch (\Exception $e) { |
||
| 128 | throw new RuntimeException(sprintf($e->getMessage())); |
||
| 129 | } |
||
| 130 | |||
| 131 | return 0; |
||
| 132 | } |
||
| 163 |