View::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Menu\Laravel;
4
5
use Illuminate\Support\Traits\Macroable;
6
use Spatie\Menu\Activatable;
7
use Spatie\Menu\HasParentAttributes;
8
use Spatie\Menu\Html\Attributes;
9
use Spatie\Menu\Item;
10
use Spatie\Menu\Traits\Activatable as ActivatableTrait;
11
use Spatie\Menu\Traits\HasParentAttributes as HasParentAttributesTrait;
12
13
class View implements Item, Activatable, HasParentAttributes
14
{
15
    use ActivatableTrait, Macroable, HasParentAttributesTrait;
16
17
    /** @var string */
18
    protected $name;
19
20
    /** @var array */
21
    protected $data;
22
23
    /** @var string|null */
24
    protected $url = null;
25
26
    /** @var bool */
27
    protected $active = false;
28
29
    /** @var Attributes */
30
    protected $parentAttributes;
31
32
    public function __construct(string $name, array $data = [])
33
    {
34
        $this->name = $name;
35
        $this->data = $data;
36
        $this->parentAttributes = new Attributes();
37
    }
38
39
    /**
40
     * @param string $name
41
     * @param array $data
42
     *
43
     * @return static
44
     */
45
    public static function create(string $name, array $data = [])
46
    {
47
        $view = new static($name, $data);
48
49
        if (array_key_exists('url', $data)) {
50
            $view->setUrl($data['url']);
51
        }
52
53
        return $view;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function render(): string
60
    {
61
        return view($this->name)
0 ignored issues
show
Bug introduced by
The method with 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...
62
            ->with($this->data + ['active' => $this->isActive()])
63
            ->render();
64
    }
65
}
66