GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2406f3...fb0479 )
by Andrea
02:43
created

AdminMenuComposer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A compose() 0 6 1
C menu() 0 81 12
1
<?php
2
3
namespace Afrittella\BackProject\Http\ViewComposers;
4
5
use Illuminate\View\View;
6
use Illuminate\Support\Facades\Auth;
7
use Afrittella\BackProject\Repositories\Menus as MenuRepository;
8
9
class AdminMenuComposer
10
{
11
12
    protected $menuRepository;
13
14
    public function __construct(MenuRepository $menuRepository)
15
    {
16
        $this->menuRepository = $menuRepository;
17
    }
18
19
    public function compose(View $view)
20
    {
21
        $mainMenu = $this->menuRepository->tree('admin-menu');
22
        $view->with('mainMenu', $mainMenu);
23
        $view->with('htmlMenu', $this->menu($mainMenu));
24
    }
25
26
    /**
27
     * Format an AdminLTE Menu
28
     *
29
     * $menus must be formatted in this way:
30
     *
31
     * Menu
32
     *    children
33
     *        children
34
     *
35
     * @param array $menus
36
     * @return string
37
     *
38
     */
39
    function menu($menus)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
40
    {
41
        // Retrieve current user (for permissions)
42
        $user = Auth::user();
43
44
        $templates = [
45
            "menu" => '
46
              <ul class="sidebar-menu">
47
                %s
48
              </ul>
49
        ',
50
51
            "menu_row" => '
52
          <li class="%s">
53
            %s
54
          </li>
55
        ', // class, link . submenu
56
57
            "menu_caret" => '
58
            <span class="pull-right-container">
59
              <i class="fa fa-angle-left pull-right"></i>
60
            </span>',
61
62
            "menu_subrow" => '
63
          <ul class="treeview-menu">
64
            %s
65
          </ul>
66
        ',
67
68
            "menu_link" => '
69
          <a href="%s"><i class="%s text-red"></i><span>%s</span>%s</a>
70
        ' // url, icon, title
71
        ];
72
73
        $traverse = function ($rows) use (&$traverse, $templates, $user) {
74
            $menuString = "";
75
            $hasActive = false;
76
            foreach ($rows as $menu) {
77
                if (!empty($menu->permission) and !$user->hasPermission($menu->permission)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
78
                    continue;
79
                }
80
81
                $hasActive = false;
82
                $submenu = "";
83
                $authorized = true;
84
85
                if ($menu->children->count() > 0) {
86
                    list($submenuString, $hasActive) = $traverse($menu->children);
87
                    $submenu = "";
88
                    if (!empty($submenuString)) {
89
                        $submenu = sprintf($templates['menu_subrow'], $submenuString);
90
                    } else {
91
                        $authorized = false;
92
                    }
93
                }
94
95
                $menu_caret = (!empty($submenu) ? $templates['menu_caret'] : '');
96
                $link = sprintf($templates['menu_link'],
97
                    (!empty($menu->route) ? url($menu->route) : '#'),
98
                    (!empty($menu->icon) ? $menu->icon : 'fa fa-circle-o'),
99
                    //trans('back-project::base.dashboard')
100
                    \Lang::has('back-project::menu.' . $menu->title) ? __('back-project::menu.' . $menu->title) : $menu->title,
101
                    $menu_caret
102
                );
103
                $class = (!empty($submenu) ? 'treeview' : '');
104
                $current_url = \Route::current()->uri();
0 ignored issues
show
Unused Code introduced by
$current_url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
106
                if ($authorized) {
107
                    $menuString .= sprintf($templates['menu_row'], $class, $link . $submenu);
108
                }
109
            }
110
111
            return [
112
                $menuString,
113
                $hasActive
114
            ];
115
        };
116
117
        list($menu, $hasActive) = $traverse($menus);
0 ignored issues
show
Unused Code introduced by
The assignment to $hasActive is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
118
        return sprintf($templates['menu'], $menu);
119
    }
120
}
121