Passed
Pull Request — 6 (#1291)
by Arnaud
08:00 queued 11s
created

Homepage::generate()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
c 2
b 0
f 0
nc 36
nop 0
dl 0
loc 42
ccs 18
cts 18
cp 1
crap 10
rs 7.6666

How to fix   Complexity   

Long Method

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:

1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Generator;
12
13
use Cecil\Collection\Page\Page;
14
use Cecil\Collection\Page\Type;
15
use Cecil\Exception\RuntimeException;
16
17
/**
18
 * Class Generator\Homepage.
19
 */
20
class Homepage extends AbstractGenerator implements GeneratorInterface
21
{
22
    /**
23
     * {@inheritdoc}
24 1
     */
25
    public function generate(): void
26 1
    {
27 1
        // creates a new index page...
28 1
        $page = (new Page('index'))->setPath('')->setVariable('title', 'Home');
29 1
        // ... clones it if already exists
30 1
        if ($this->builder->getPages()->has('index')) {
31
            $page = clone $this->builder->getPages()->get('index');
32 1
        }
33
        // collects all pages
34
        $subPages = $this->builder->getPages()->filter(function (Page $page) {
35 1
            return $page->getType() == TYPE::PAGE
36
                && $page->isVirtual() === false // excludes virtual pages
37 1
                && $page->getVariable('exclude') !== true;
38 1
        });
39
        /** @var \Cecil\Collection\Page\Page $page */
40
        if ($page->hasVariable('pagesfrom') && $this->builder->getPages()->has($page->getVariable('pagesfrom'))) {
0 ignored issues
show
Bug introduced by
It seems like $page->getVariable('pagesfrom') can also be of type null; however, parameter $id of Cecil\Collection\Collection::has() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        if ($page->hasVariable('pagesfrom') && $this->builder->getPages()->has(/** @scrutinizer ignore-type */ $page->getVariable('pagesfrom'))) {
Loading history...
41 1
            $subPages = $this->builder->getPages()->get($page->getVariable('pagesfrom'))->getVariable('pages');
0 ignored issues
show
Bug introduced by
It seems like $page->getVariable('pagesfrom') can also be of type null; however, parameter $id of Cecil\Collection\Collection::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            $subPages = $this->builder->getPages()->get(/** @scrutinizer ignore-type */ $page->getVariable('pagesfrom'))->getVariable('pages');
Loading history...
42 1
        }
43 1
        // sorts
44 1
        /** @var \Cecil\Collection\Page\Collection $subPages */
45
        /** @var \Cecil\Collection\Page\Page $page */
46
        $pages = $subPages->sortByDate();
47 1
        if ($page->hasVariable('sortby')) {
48 1
            $sortMethod = sprintf('sortBy%s', ucfirst((string) $page->getVariable('sortby')));
49 1
            if (!method_exists($pages, $sortMethod)) {
50
                throw new RuntimeException(\sprintf('In "%s" section "%s" is not a valid value for "sortby" variable.', $page->getId(), $page->getVariable('sortby')));
51
            }
52 1
            $pages = $pages->$sortMethod();
53 1
        }
54
        /** @var \Cecil\Collection\Page\Page $page */
55
        $page->setType(Type::HOMEPAGE)
56
            ->setVariable('pages', $pages);
57
        if ($pages->first()) {
58
            $page->setVariable('date', $pages->first()->getVariable('date'));
59
        }
60
        // default menu
61
        if (!$page->getVariable('menu')) {
62
            $page->setVariable('menu', [
63
                'main' => ['weight' => 0],
64
            ]);
65
        }
66
        $this->generatedPages->add($page);
67
    }
68
}
69