Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

SettingsMenuAdaptor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 15.58 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 12
loc 77
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B adaptChildren() 12 45 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\Menu;
4
5
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * SettingsMenuAdaptor to add the Settings MenuItem to the top menu and build the Settings tree
10
 */
11
class SettingsMenuAdaptor implements MenuAdaptorInterface
12
{
13
    /**
14
     * @var AuthorizationCheckerInterface
15
     */
16
    private $authorizationChecker;
17
18
    /**
19
     * @var bool
20
     */
21
    private $isEnabledVersionChecker;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param AuthorizationCheckerInterface $container
0 ignored issues
show
Bug introduced by
There is no parameter named $container. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
27
     */
28
    public function __construct(AuthorizationCheckerInterface $authorizationChecker, $isEnabledVersionChecker)
29
    {
30
        $this->authorizationChecker = $authorizationChecker;
31
        $this->isEnabledVersionChecker = (bool) $isEnabledVersionChecker;
32
    }
33
34
    /**
35
     * In this method you can add children for a specific parent, but also remove and change the already created children
36
     *
37
     * @param MenuBuilder $menu      The MenuBuilder
38
     * @param MenuItem[]  &$children The current children
39
     * @param MenuItem    $parent    The parent Menu item
0 ignored issues
show
Documentation introduced by
Should the type for parameter $parent not be null|MenuItem?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     * @param Request     $request   The Request
0 ignored issues
show
Documentation introduced by
Should the type for parameter $request not be null|Request?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     */
42
    public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null)
43
    {
44
        if (is_null($parent)) {
45
            $menuItem = new TopMenuItem($menu);
46
            $menuItem
47
                ->setRoute('KunstmaanAdminBundle_settings')
48
                ->setLabel('settings.title')
49
                ->setUniqueId('settings')
50
                ->setParent($parent)
51
                ->setRole('settings');
52
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
53
                $menuItem->setActive(true);
54
            }
55
            $children[] = $menuItem;
56
        } elseif ('KunstmaanAdminBundle_settings' == $parent->getRoute()) {
57
            if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN')) {
58 View Code Duplication
                if ($this->isEnabledVersionChecker) {
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...
59
                    $menuItem = new MenuItem($menu);
60
                    $menuItem
61
                        ->setRoute('KunstmaanAdminBundle_settings_bundle_version')
62
                        ->setLabel('settings.version.bundle')
63
                        ->setUniqueId('bundle_versions')
64
                        ->setParent($parent);
65
                    if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
66
                        $menuItem->setActive(true);
67
                    }
68
                    $children[] = $menuItem;
69
                }
70
            }
71
        }
72
73
        if (!is_null($parent) && 'KunstmaanAdminBundle_settings' == $parent->getRoute()) {
74
            $menuItem = new MenuItem($menu);
75
            $menuItem
76
                ->setRoute('kunstmaanadminbundle_admin_exception')
77
                ->setLabel('settings.exceptions.title')
78
                ->setUniqueId('exceptions')
79
                ->setParent($parent);
80
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
81
                $menuItem->setActive(true);
82
                $parent->setActive(true);
83
            }
84
            $children[] = $menuItem;
85
        }
86
    }
87
}
88