Completed
Pull Request — master (#41)
by Vladimir
02:38
created

MenuManager::addToSiteMenu()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 53
Code Lines 25

Duplication

Lines 13
Ratio 24.53 %

Code Coverage

Tests 35
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 25
c 1
b 0
f 0
nc 13
nop 1
dl 13
loc 53
ccs 35
cts 35
cp 1
crap 13
rs 6.3327

How to fix   Long Method    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
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Manager;
9
10
use allejo\stakx\Object\JailObject;
11
use allejo\stakx\Object\PageView;
12
13
class MenuManager extends BaseManager
14
{
15
    /** @var PageView */
16
    private $siteMenu;
17
18 8
    public function __construct()
19
    {
20 8
        parent::__construct();
21
22 8
        $this->siteMenu = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<allejo\stakx\Object\PageView> of property $siteMenu.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23 8
    }
24
25
    /**
26
     * An array representing the website's menu structure with children and grandchildren made from static PageViews
27
     *
28
     * @return JailObject[]
29
     */
30 8
    public function getSiteMenu ()
31
    {
32 8
        $jailedMenu = array();
33
34 8
        foreach ($this->siteMenu as $key => $value)
0 ignored issues
show
Bug introduced by
The expression $this->siteMenu of type object<allejo\stakx\Object\PageView> is not traversable.
Loading history...
35
        {
36
            // If it's an array, it means the parent is hidden from the site menu therefore its children should be too
37 7
            if (is_array($this->siteMenu[$key]))
38 7
            {
39 1
                continue;
40
            }
41
42 7
            $jailedMenu[$key] = $value->createJail();
43 8
        }
44
45 8
        return $jailedMenu;
46
    }
47
48
    /**
49
     * @param PageView[] $pageViews
50
     */
51 8
    public function buildFromPageViews ($pageViews)
52
    {
53 8
        foreach ($pageViews as &$pageView)
54
        {
55 8
            $this->addToSiteMenu($pageView);
56 8
        }
57 8
    }
58
59
    /**
60
     * @param PageView $pageView
61
     */
62 8
    public function addToSiteMenu (&$pageView)
63
    {
64 8
        $frontMatter = $pageView->getFrontMatter();
65
66 8
        if (isset($frontMatter['menu']) && !$frontMatter['menu'])
67 8
        {
68 1
            return;
69
        }
70
71 8
        $url = trim($pageView->getPermalink(), '/');
72
73
        // @TODO in the next breaking release, remove this check and allow the homepage to be indexed as '.'
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
74 8
        if (empty($url))
75 8
        {
76 1
            return;
77
        }
78
79 7
        $root = &$this->siteMenu;
80 7
        $dirs = explode('/', $url);
81
82 7
        while (count($dirs) > 0)
83
        {
84 7
            $name = array_shift($dirs);
85 7
            $name = (!empty($name)) ? $name : '.';
86
87 7
            if (!is_null($name) && count($dirs) == 0)
88 7
            {
89 7 View Code Duplication
                if (isset($root[$name]) && is_array($root[$name]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90 7
                {
91 1
                    $children = &$pageView->getChildren();
92 1
                    $children = $root[$name]['children'];
93 1
                }
94
95 7
                $root[$name] = &$pageView;
96 7
            }
97
            else
98
            {
99 4
                if (!isset($root[$name]))
100 4
                {
101 2
                    $root[$name]['children'] = array();
102 2
                    $root = &$root[$name]['children'];
103 2
                }
104 4 View Code Duplication
                else if (isset($root[$name]) && is_array($root[$name]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105 4
                {
106 2
                    $root = &$root[$name]['children'];
107 2
                }
108
                else
109
                {
110 2
                    $root = &$root[$name]->getChildren();
111
                }
112
            }
113 7
        }
114 7
    }
115
}
116