| Conditions | 22 |
| Paths | > 20000 |
| Total Lines | 108 |
| Code Lines | 70 |
| 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 |
||
| 95 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 96 | { |
||
| 97 | $config = []; |
||
| 98 | $options = []; |
||
| 99 | $messageOpt = ''; |
||
| 100 | |||
| 101 | if ($input->getOption('baseurl')) { |
||
| 102 | $config['baseurl'] = $input->getOption('baseurl'); |
||
| 103 | } |
||
| 104 | if ($input->getOption('output')) { |
||
| 105 | $config['output']['dir'] = $input->getOption('output'); |
||
| 106 | if ($input->getOption('output') != self::SERVE_OUTPUT) { |
||
| 107 | Util\File::getFS()->dumpFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'output'), (string) $input->getOption('output')); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | if ($input->getOption('optimize') === true) { |
||
| 111 | $config['optimize']['enabled'] = true; |
||
| 112 | } |
||
| 113 | if ($input->getOption('optimize') === false) { |
||
| 114 | $config['optimize']['enabled'] = false; |
||
| 115 | } |
||
| 116 | if ($input->getOption('clear-cache') === null) { |
||
| 117 | $config['cache']['enabled'] = false; |
||
| 118 | } |
||
| 119 | |||
| 120 | $builder = $this->getBuilder($config); |
||
| 121 | |||
| 122 | if ($input->getOption('drafts')) { |
||
| 123 | $options['drafts'] = true; |
||
| 124 | $messageOpt .= ' with drafts'; |
||
| 125 | } |
||
| 126 | if ($input->getOption('dry-run')) { |
||
| 127 | $options['dry-run'] = true; |
||
| 128 | $messageOpt .= ' (dry-run)'; |
||
| 129 | } |
||
| 130 | if ($input->getOption('page')) { |
||
| 131 | $options['page'] = $input->getOption('page'); |
||
| 132 | } |
||
| 133 | if ($input->getOption('render-subset')) { |
||
| 134 | $options['render-subset'] = (string) $input->getOption('render-subset'); |
||
| 135 | } |
||
| 136 | if ($input->getOption('clear-cache')) { |
||
| 137 | if (0 < $removedFiles = (new \Cecil\Cache($this->getBuilder()))->clearByPattern((string) $input->getOption('clear-cache'))) { |
||
| 138 | $output->writeln(\sprintf('<info>%s cache files removed by regular expression "%s"</info>', $removedFiles, $input->getOption('clear-cache'))); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | $output->writeln(\sprintf('Building website%s...', $messageOpt)); |
||
| 143 | $output->writeln(\sprintf('<comment>Path: %s</comment>', $this->getPath()), OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 144 | if (!empty($this->getConfigFiles())) { |
||
| 145 | $output->writeln(\sprintf('<comment>Config: %s</comment>', implode(', ', $this->getConfigFiles())), OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 146 | } |
||
| 147 | $output->writeln(\sprintf('<comment>Output: %s</comment>', $this->getBuilder()->getConfig()->getOutputPath()), OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 148 | if ($builder->getConfig()->isEnabled('cache') !== false) { |
||
| 149 | $output->writeln(\sprintf('<comment>Cache: %s</comment>', $builder->getConfig()->getCachePath()), OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 150 | } |
||
| 151 | |||
| 152 | // build |
||
| 153 | $builder->build($options); |
||
| 154 | $output->writeln('Build done 🎉'); |
||
| 155 | |||
| 156 | // notification |
||
| 157 | if ($input->getOption('notif')) { |
||
| 158 | $notifier = new DefaultNotifier(); |
||
| 159 | $this->notification->setBody('Build done 🎉'); |
||
| 160 | $notifier->send($this->notification); |
||
| 161 | } |
||
| 162 | |||
| 163 | // show build steps metrics |
||
| 164 | if ($input->getOption('metrics')) { |
||
| 165 | $table = new Table($output); |
||
| 166 | $table |
||
| 167 | ->setHeaderTitle('Build steps metrics') |
||
| 168 | ->setHeaders(['Step', 'Duration', 'Memory']) |
||
| 169 | ->setRows($builder->getMetrics()['steps']) |
||
| 170 | ; |
||
| 171 | $table->setStyle('box')->render(); |
||
| 172 | } |
||
| 173 | |||
| 174 | // show built pages as table |
||
| 175 | if ($input->getOption('show-pages')) { |
||
| 176 | $pagesAsArray = []; |
||
| 177 | foreach ( |
||
| 178 | $this->getBuilder()->getPages()->filter(function (\Cecil\Collection\Page\Page $page) { |
||
| 179 | return $page->getVariable('published'); |
||
| 180 | })->usort(function (\Cecil\Collection\Page\Page $pageA, \Cecil\Collection\Page\Page $pageB) { |
||
| 181 | return strnatcmp((string) $pageA['language'], (string) $pageB['language']); |
||
| 182 | }) as $page |
||
| 183 | ) { |
||
| 184 | /** @var \Cecil\Collection\Page\Page $page */ |
||
| 185 | $pagesAsArray[] = [ |
||
| 186 | $page->getId(), |
||
| 187 | $page->getVariable('language'), |
||
| 188 | \sprintf("%s %s", $page->getType(), $page->getType() !== \Cecil\Collection\Page\Type::PAGE->value ? "(" . \count($page->getPages() ?: []) . ")" : ''), |
||
| 189 | $page->getSection(), |
||
| 190 | $page->isVirtual() ? 'true' : 'false', |
||
| 191 | ]; |
||
| 192 | } |
||
| 193 | $table = new Table($output); |
||
| 194 | $table |
||
| 195 | ->setHeaderTitle(\sprintf("Built pages (%s)", \count($pagesAsArray))) |
||
| 196 | ->setHeaders(['ID', 'Lang', 'Type', 'Section', 'Virtual']) |
||
| 197 | ->setRows($pagesAsArray) |
||
| 198 | ; |
||
| 199 | $table->setStyle('box')->render(); |
||
| 200 | } |
||
| 201 | |||
| 202 | return 0; |
||
| 203 | } |
||
| 205 |