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