Conditions | 11 |
Paths | 14 |
Total Lines | 83 |
Code Lines | 47 |
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 |
||
133 | protected function collectPages(): void |
||
134 | { |
||
135 | $filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
||
136 | return $page->hasVariable('menu') |
||
137 | && $page->getVariable('published') |
||
138 | && \in_array($page->getVariable('language', $this->config->getLanguageDefault()), array_column($this->config->getLanguages(), 'code')); |
||
139 | }); |
||
140 | |||
141 | $total = \count($filteredPages); |
||
142 | $count = 0; |
||
143 | /** @var \Cecil\Collection\Page\Page $page */ |
||
144 | foreach ($filteredPages as $page) { |
||
145 | $count++; |
||
146 | $language = $page->getVariable('language', $this->config->getLanguageDefault()); |
||
147 | /** |
||
148 | * Array case. |
||
149 | * |
||
150 | * ie 1: |
||
151 | * menu: [main, navigation] |
||
152 | * ie 2: |
||
153 | * menu: |
||
154 | * main: |
||
155 | * weight: 999 |
||
156 | */ |
||
157 | if (\is_array($page->getVariable('menu'))) { |
||
158 | foreach ($page->getVariable('menu') as $key => $value) { |
||
159 | $menuName = $key; |
||
160 | $properties = $value; |
||
161 | $weight = null; |
||
162 | if (\is_int($key)) { |
||
163 | $menuName = $value; |
||
164 | $properties = null; |
||
165 | } |
||
166 | if (!\is_string($menuName)) { |
||
167 | $this->builder->getLogger()->error( |
||
168 | sprintf( |
||
169 | 'Menu\'s name of page "%s" must be a string, not "%s"', |
||
170 | $page->getId(), |
||
171 | PrintLogger::format($menuName) |
||
172 | ), |
||
173 | ['progress' => [$count, $total]] |
||
174 | ); |
||
175 | continue; |
||
176 | } |
||
177 | $item = (new Entry($page->getIdWithoutLang())) |
||
178 | ->setName($page->getVariable('title')) |
||
179 | ->setUrl((new PageRenderer($this->config))->getUrl($page)); |
||
180 | if (isset($properties['weight'])) { |
||
181 | $weight = $properties['weight']; |
||
182 | $item->setWeight((int) $properties['weight']); |
||
183 | } |
||
184 | // add Menu if not exists |
||
185 | if (!$this->menus[$language]->has($menuName)) { |
||
186 | $this->menus[$language]->add(new Menu($menuName)); |
||
187 | } |
||
188 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
189 | $menu = $this->menus[$language]->get($menuName); |
||
190 | $menu->add($item); |
||
191 | |||
192 | $message = sprintf('Page menu entry "%s (%s) > %s" created (name: %s, weight: %s)', $menu->getId(), $language, $item->getId(), $item->getName(), $weight ?? 'N/A'); |
||
193 | $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
||
194 | } |
||
195 | continue; |
||
196 | } |
||
197 | /** |
||
198 | * String case. |
||
199 | * |
||
200 | * ie: |
||
201 | * menu: main |
||
202 | */ |
||
203 | $item = (new Entry($page->getIdWithoutLang())) |
||
204 | ->setName($page->getVariable('title')) |
||
205 | ->setUrl((new PageRenderer($this->config))->getUrl($page)); |
||
206 | // add Menu if not exists |
||
207 | if (!$this->menus[$language]->has($page->getVariable('menu'))) { |
||
208 | $this->menus[$language]->add(new Menu($page->getVariable('menu'))); |
||
209 | } |
||
210 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
211 | $menu = $this->menus[$language]->get($page->getVariable('menu')); |
||
212 | $menu->add($item); |
||
213 | |||
214 | $message = sprintf('Page menu entry "%s (%s) > %s" created (name: %s)', $menu->getId(), $language, $item->getId(), $item->getName()); |
||
215 | $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
||
216 | } |
||
219 |