Completed
Push — master ( 86815f...7e02e6 )
by wen
02:47
created

HasNavigation::addToNavigation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 4
nop 2
1
<?php
2
3
namespace Sco\Admin\Component\Concerns;
4
5
use KodiComponents\Navigation\Contracts\BadgeInterface;
6
use Sco\Admin\Navigation\Badge;
7
use Sco\Admin\Navigation\Page;
8
9
trait HasNavigation
10
{
11
    /**
12
     * @return \KodiComponents\Navigation\Contracts\NavigationInterface
13
     */
14
    public function getNavigation()
15
    {
16
        return $this->app['admin.navigation'];
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
    }
18
19
    /**
20
     * 添加菜单
21
     *
22
     * @param int  $priority
23
     * @param null $badge
24
     *
25
     * @return \Sco\Admin\Navigation\Page
26
     */
27
    public function addToNavigation($priority = 100, $badge = null)
28
    {
29
        $page = $this->makePage($priority, $badge);
30
        $this->getNavigation()->addPage($page);
31
        return $page;
32
    }
33
34
    /**
35
     * page
36
     *
37
     * @param int  $priority
38
     * @param null $badge
39
     *
40
     * @return \Sco\Admin\Navigation\Page
41
     */
42
    protected function makePage($priority = 100, $badge = null)
43
    {
44
        $page = new Page($this->getTitle(), $this->getViewUrl());
0 ignored issues
show
Bug introduced by
It seems like getTitle() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
        $page->setPriority($priority);
46
        $page->setAccessLogic(function () {
47
            return $this->isView();
0 ignored issues
show
Bug introduced by
It seems like isView() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
        });
49
50
        if ($badge) {
51
            if (!($badge instanceof BadgeInterface)) {
52
                $badge = new Badge($badge);
53
            }
54
            $page->addBadge($badge);
55
        }
56
57
        return $page;
58
    }
59
60
    /**
61
     * @param array $parameters
62
     *
63
     * @return string
64
     */
65
    public function getViewUrl(array $parameters = [])
66
    {
67
        array_unshift($parameters, $this->getName());
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68
69
        return route('admin.model.index', $parameters, false);
70
    }
71
}
72