Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#23)
by
unknown
01:47
created

MenuItem   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 77
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parent() 0 4 1
A children() 0 4 1
A page() 0 4 1
A menu() 0 4 1
B getTree() 0 28 6
A url() 0 16 4
1
<?php
2
3
namespace Backpack\MenuCRUD\app\Models;
4
5
use Backpack\CRUD\CrudTrait;
6
use Backpack\MenuCRUD\app\Models\Menu;
7
use Illuminate\Database\Eloquent\Model;
8
9
class MenuItem extends Model
10
{
11
    use CrudTrait;
12
13
    protected $table = 'menu_items';
14
    protected $fillable = ['name', 'type', 'link', 'page_id', 'parent_id', 'menu_id'];
15
16
    public function parent()
17
    {
18
        return $this->belongsTo('Backpack\MenuCRUD\app\Models\MenuItem', 'parent_id');
19
    }
20
21
    public function children()
22
    {
23
        return $this->hasMany('Backpack\MenuCRUD\app\Models\MenuItem', 'parent_id');
24
    }
25
26
    public function page()
27
    {
28
        return $this->belongsTo('Backpack\PageManager\app\Models\Page', 'page_id');
29
    }
30
31
    public function menu()
32
    {
33
        return $this->belongsTo('Backpack\MenuCRUD\app\Models\Menu', 'menu_id', 'id');
34
    }
35
36
    /**
37
     * Get all menu items, in a hierarchical collection.
38
     * Only supports 2 levels of indentation.
39
     */
40
    public static function getTree($menuName = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
41
    {
42
        $menu = self::orderBy('lft')->get();
43
44
        if(!is_null($menuName)) {
45
            $menuId = Menu::where('name', $menuName)->first()->id;
46
            $menu = self::orderBy('lft')->where('menu_id', $menuId)->get();
47
        }
48
49
        if ($menu->count()) {
50
            foreach ($menu as $k => $menu_item) {
51
                $menu_item->children = collect([]);
52
53
                foreach ($menu as $i => $menu_subitem) {
54
                    if ($menu_subitem->parent_id == $menu_item->id) {
55
                        $menu_item->children->push($menu_subitem);
56
57
                        // remove the subitem for the first level
58
                        $menu = $menu->reject(function ($item) use ($menu_subitem) {
59
                            return $item->id == $menu_subitem->id;
60
                        });
61
                    }
62
                }
63
            }
64
        }
65
66
        return $menu;
67
    }
68
69
    public function url()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
70
    {
71
        switch ($this->type) {
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<Backpack\MenuCRUD\app\Models\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
            case 'external_link':
73
                return $this->link;
0 ignored issues
show
Documentation introduced by
The property link does not exist on object<Backpack\MenuCRUD\app\Models\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
75
76
            case 'internal_link':
77
                return is_null($this->link) ? '#' : url($this->link);
0 ignored issues
show
Documentation introduced by
The property link does not exist on object<Backpack\MenuCRUD\app\Models\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
78
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
79
80
            default: //page_link
81
                return url($this->page->slug);
0 ignored issues
show
Documentation introduced by
The property page does not exist on object<Backpack\MenuCRUD\app\Models\MenuItem>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
83
        }
84
    }
85
}
86