| Conditions | 20 |
| Paths | 1084 |
| Total Lines | 79 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 78 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 79 | { |
||
| 80 | $name = (string) $input->getOption('name'); |
||
| 81 | $slugify = $input->getOption('slugify'); |
||
| 82 | $prefix = (bool) $input->getOption('prefix'); |
||
| 83 | $force = (bool) $input->getOption('force'); |
||
| 84 | $open = (bool) $input->getOption('open'); |
||
| 85 | $editor = $input->getOption('editor'); |
||
| 86 | |||
| 87 | try { |
||
| 88 | // ask |
||
| 89 | if (empty($name)) { |
||
| 90 | $name = $this->io->ask('What is the name of the page file?', 'new-page.md'); |
||
| 91 | $slugify = ($slugify !== null) ? $slugify : $this->io->confirm('Slugify the file name?', true); |
||
| 92 | $prefix = ($prefix !== false) ?: $this->io->confirm('Add date prefix to the filename?', false); |
||
| 93 | $open = ($open !== false) ?: $this->io->confirm('Do you want open the created file with your editor?', false); |
||
| 94 | if ($open && !$this->getBuilder()->getConfig()->has('editor')) { |
||
| 95 | $editor = ($editor !== null) ? $editor : $this->io->ask('Which editor?'); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | // parse given path name |
||
| 99 | $nameParts = pathinfo($name); |
||
| 100 | $dirname = trim($nameParts['dirname'], '.'); |
||
| 101 | $basename = $nameParts['basename']; |
||
| 102 | $extension = $nameParts['extension']; |
||
| 103 | $title = substr($basename, 0, -\strlen(".$extension")); |
||
| 104 | $filename = $basename; |
||
| 105 | if (!\in_array($extension, (array) $this->getBuilder()->getConfig()->get('pages.ext'))) { |
||
| 106 | $title = $filename; |
||
| 107 | $filename = trim("$basename.md"); // force a valid extension |
||
| 108 | } |
||
| 109 | $title = trim(ucfirst(str_replace('-', ' ', $title))); |
||
| 110 | $date = date('Y-m-d'); |
||
| 111 | // date prefix? |
||
| 112 | $datePrefix = $prefix ? \sprintf('%s-', $date) : ''; |
||
| 113 | // define target path |
||
| 114 | $fileRelativePath = \sprintf( |
||
| 115 | '%s%s%s%s%s', |
||
| 116 | (string) $this->getBuilder()->getConfig()->get('pages.dir'), |
||
| 117 | DIRECTORY_SEPARATOR, |
||
| 118 | empty($dirname) ? '' : $dirname . DIRECTORY_SEPARATOR, |
||
| 119 | $datePrefix, |
||
| 120 | $slugify ? \Cecil\Collection\Page\Page::slugify($filename) : $filename |
||
| 121 | ); |
||
| 122 | $filePath = Util::joinFile($this->getPath(), $fileRelativePath); |
||
| 123 | // ask to override existing file? |
||
| 124 | if (Util\File::getFS()->exists($filePath) && !$force) { |
||
| 125 | $output->writeln(\sprintf('<comment>The file "%s" already exists.</comment>', $fileRelativePath)); |
||
| 126 | if (!$this->io->confirm('Do you want to override it?', false)) { |
||
| 127 | return 0; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | // creates a new file |
||
| 131 | $model = $this->findModel(\sprintf('%s%s', empty($dirname) ? '' : $dirname . DIRECTORY_SEPARATOR, $filename)); |
||
| 132 | $fileContent = str_replace( |
||
| 133 | ['%title%', '%date%'], |
||
| 134 | [$title, $date], |
||
| 135 | $model['content'] |
||
| 136 | ); |
||
| 137 | Util\File::getFS()->dumpFile($filePath, $fileContent); |
||
| 138 | $output->writeln(\sprintf('<info>File %s created (with "%s" model).</info>', $filePath, $model['name'])); |
||
| 139 | // open editor? |
||
| 140 | if ($open) { |
||
| 141 | if ($editor === null) { |
||
| 142 | if (!$this->getBuilder()->getConfig()->has('editor')) { |
||
| 143 | $output->writeln('<comment>No editor configured.</comment>'); |
||
| 144 | |||
| 145 | return 0; |
||
| 146 | } |
||
| 147 | $editor = (string) $this->getBuilder()->getConfig()->get('editor'); |
||
| 148 | } |
||
| 149 | $output->writeln(\sprintf('<info>Opening file with %s...</info>', ucfirst($editor))); |
||
| 150 | $this->openEditor($filePath, $editor); |
||
| 151 | } |
||
| 152 | } catch (\Exception $e) { |
||
| 153 | throw new RuntimeException(\sprintf($e->getMessage())); |
||
| 154 | } |
||
| 155 | |||
| 156 | return 0; |
||
| 157 | } |
||
| 188 |