Completed
Pull Request — master (#90)
by Arnaud
02:15
created

MenuBuilder::mainMenu()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 12
nc 4
nop 1
crap 12
1
<?php
2
3
namespace LAG\AdminBundle\Menu;
4
5
use Exception;
6
use Knp\Menu\ItemInterface;
7
use LAG\AdminBundle\Admin\Request\RequestHandler;
8
use LAG\AdminBundle\Menu\Factory\MenuFactory;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11
/**
12
 * Create menu handled by the AdminBundle (main, top)
13
 */
14
class MenuBuilder
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $menusConfiguration;
20
21
    /**
22
     * @var MenuFactory
23
     */
24
    protected $menuFactory;
25
26
    /**
27
     * @var RequestHandler
28
     */
29
    protected $requestHandler;
30
31
    /**
32
     * @var RequestStack
33
     */
34
    protected $requestStack;
35
36
    /**
37
     * MenuBuilder constructor.
38
     *
39
     * @param array $menusConfiguration
40
     * @param MenuFactory $menuFactory
41
     * @param RequestHandler $requestHandler
42
     * @param RequestStack $requestStack
43
     */
44
    public function __construct(
45
        array $menusConfiguration,
46
        MenuFactory $menuFactory,
47
        RequestHandler $requestHandler,
48
        RequestStack $requestStack
49
    ) {
50
        $this->menusConfiguration = $menusConfiguration;
51
        $this->menuFactory = $menuFactory;
52
        $this->requestHandler = $requestHandler;
53
        $this->requestStack = $requestStack;
54
    }
55
56
    /**
57
     * Create main menu.
58
     *
59
     * @param array $options
60
     * @return ItemInterface
61
     */
62
    public function mainMenu(array $options = [])
63
    {
64
        if (!array_key_exists('main', $this->menusConfiguration)) {
65
            $this->menusConfiguration['main'] = [];
66
        }
67
        $this->menusConfiguration['main']['attr'] = [
68
            'id' => 'side-menu',
69
            'class' => 'nav in',
70
        ];
71
        $entity = null;
72
73
        if (array_key_exists('entity', $options)) {
74
            $entity = $options['entity'];
75
        }
76
       
77
        return $this
78
            ->menuFactory
79
            ->create('main', $this->menusConfiguration['main'], $entity);
80
    }
81
82
    /**
83
     * Create dynamic top menu.
84
     *
85
     * @param array $options
86
     * @return ItemInterface
87
     * @throws Exception
88
     */
89
    public function topMenu(array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        // get current request
92
        $request = $this
93
            ->requestStack
94
            ->getCurrentRequest();
95
        $entity = null;
96
        $configuration = [];
97
    
98
        // request should exists and should have admin parameters
99
        if ($this->requestHandler->supports($request)) {
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 92 can be null; however, LAG\AdminBundle\Admin\Re...uestHandler::supports() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
100
            // get current action from admin
101
            $admin = $this
102
                ->requestHandler
103
                ->handle($request)
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 92 can be null; however, LAG\AdminBundle\Admin\Re...equestHandler::handle() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
104
            ;
105
106
            if ($admin->hasView()) {
107
                // menu configuration
108
                $menus = $admin
109
                    ->getView()
110
                    ->getConfiguration()
111
                    ->getParameter('menus')
112
                ;
113
114
                if (key_exists('top', $menus)) {
115
                    //$configuration = $menus['top'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
                    $configuration['attr'] = [
117
                        //'class' => 'nav navbar-top-links navbar-right in',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
                        'class' => 'navbar-nav bd-navbar-nav flex-row',
119
                    ];
120
                    $entity = $admin->getView()->getEntities()->first();
0 ignored issues
show
Bug introduced by
The method first does only exist in Doctrine\Common\Collections\Collection, but not in Pagerfanta\Pagerfanta.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
121
                }
122
            }
123
        }
124
125
        return $this
126
            ->menuFactory
127
            ->create('top', $configuration, $entity)
128
        ;
129
    }
130
}
131