RenderService::renderMenuItemTemplate()   C
last analyzed

Complexity

Conditions 11
Paths 180

Size

Total Lines 51

Duplication

Lines 20
Ratio 39.22 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 20
loc 51
ccs 0
cts 45
cp 0
rs 6.389
c 0
b 0
f 0
cc 11
nc 180
nop 3
crap 132

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
namespace Kunstmaan\MenuBundle\Service;
4
5
use Kunstmaan\MenuBundle\Entity\MenuItem;
6
use Symfony\Component\Routing\RouterInterface;
7
use Twig\Environment;
8
9
class RenderService
10
{
11
    /**
12
     * @var RouterInterface
13
     */
14
    private $router;
15
16
    /**
17
     * @param RouterInterface $router
18
     */
19
    public function __construct(RouterInterface $router)
20
    {
21
        $this->router = $router;
22
    }
23
24
    /**
25
     * @param Environment $environment
26
     * @param $node
27
     * @param array $options
28
     *
29
     * @return string
30
     */
31
    public function renderMenuItemTemplate(Environment $environment, $node, $options = array())
32
    {
33
        $template = isset($options['template']) ? $options['template'] : false;
34
        if ($template === false) {
35
            $template = '@KunstmaanMenu/menu-item.html.twig';
36
        }
37
38
        $hasActiveChild = false;
39 View Code Duplication
        if ($node['__children']) {
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...
40
            foreach ($node['__children'] as $childNode) {
41
                if ($childNode['type'] == MenuItem::TYPE_PAGE_LINK) {
42
                    $childUrl = $this->router->generate('_slug', array('url' => $childNode['nodeTranslation']['url']));
43
                    if ($this->router->getContext()->getPathInfo() == $childUrl) {
44
                        $hasActiveChild = true;
45
46
                        break;
47
                    }
48
                }
49
            }
50
        }
51
52
        $active = false;
53 View Code Duplication
        if ($node['type'] == MenuItem::TYPE_PAGE_LINK) {
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...
54
            $url = $this->router->generate('_slug', array('url' => $node['nodeTranslation']['url']));
55
56
            if ($this->router->getContext()->getPathInfo() == $url) {
57
                $active = true;
58
            }
59
        } else {
60
            $url = $node['url'];
61
        }
62
63
        if ($node['type'] == MenuItem::TYPE_PAGE_LINK) {
64
            if ($node['title']) {
65
                $title = $node['title'];
66
            } else {
67
                $title = $node['nodeTranslation']['title'];
68
            }
69
        } else {
70
            $title = $node['title'];
71
        }
72
73
        return $environment->render($template, array(
74
            'menuItem' => $node,
75
            'url' => $url,
76
            'options' => $options,
77
            'title' => $title,
78
            'active' => $active,
79
            'hasActiveChild' => $hasActiveChild,
80
        ));
81
    }
82
}
83