Completed
Push — master ( a61567...2da41b )
by Song
03:43
created

Tab::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Widgets;
4
5
use Illuminate\Contracts\Support\Renderable;
6
7
class Tab extends Widget implements Renderable
8
{
9
    const TYPE_CONTENT  = 1;
10
    const TYPE_LINK     = 2;
11
12
    /**
13
     * @var string
14
     */
15
    protected $view = 'admin::widgets.tab';
16
17
    /**
18
     * @var array
19
     */
20
    protected $data = [
21
        'id'       => '',
22
        'title'    => '',
23
        'tabs'     => [],
24
        'dropDown' => [],
25
        'active'   => 0,
26
    ];
27
28
    public function __construct()
29
    {
30
        $this->class('nav-tabs-custom');
31
    }
32
33
    /**
34
     * Add a tab and its contents.
35
     *
36
     * @param string            $title
37
     * @param string|Renderable $content
38
     * @param bool              $active
39
     *
40
     * @return $this
41
     */
42 View Code Duplication
    public function add($title, $content, $active = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
43
    {
44
        $this->data['tabs'][] = [
45
            'id'      => mt_rand(),
46
            'title'   => $title,
47
            'content' => $content,
48
            'type'    => static::TYPE_CONTENT,
49
        ];
50
51
        if ($active) {
52
            $this->data['active'] = count($this->data['tabs']) - 1;
53
        }
54
55
        return $this;
56
    }
57
58
    /**
59
     * Add a link on tab.
60
     *
61
     * @param string    $title
62
     * @param string    $href
63
     * @param bool      $active
64
     *
65
     * @return $this
66
     */
67 View Code Duplication
    public function addLink($title, $href, $active = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
    {
69
        $this->data['tabs'][] = [
70
            'id'      => mt_rand(),
71
            'title'   => $title,
72
            'href'    => $href,
73
            'type'    => static::TYPE_LINK,
74
        ];
75
76
        if ($active) {
77
            $this->data['active'] = count($this->data['tabs']) - 1;
78
        }
79
80
        return $this;
81
    }
82
83
    /**
84
     * Set title.
85
     *
86
     * @param string $title
87
     */
88
    public function title($title = '')
89
    {
90
        $this->data['title'] = $title;
91
    }
92
93
    /**
94
     * Set drop-down items.
95
     *
96
     * @param array $links
97
     *
98
     * @return $this
99
     */
100
    public function dropDown(array $links)
101
    {
102
        if (is_array($links[0])) {
103
            foreach ($links as $link) {
104
                call_user_func([$this, 'dropDown'], $link);
105
            }
106
107
            return $this;
108
        }
109
110
        $this->data['dropDown'][] = [
111
            'name' => $links[0],
112
            'href' => $links[1],
113
        ];
114
115
        return $this;
116
    }
117
118
    /**
119
     * Render Tab.
120
     *
121
     * @return string
122
     */
123
    public function render()
124
    {
125
        $data = array_merge(
126
            $this->data,
127
            ['attributes' => $this->formatAttributes()]
128
        );
129
130
        return view($this->view, $data)->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
131
    }
132
}
133