Completed
Push — master ( 0240a3...41c1d1 )
by Sebastian
40:18
created

View::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Menu\Laravel;
4
5
use Spatie\Menu\Activatable;
6
use Spatie\Menu\Item;
7
use Spatie\Menu\Traits\Activatable as ActivatableTrait;
8
9
class View implements Item, Activatable
10
{
11
    use ActivatableTrait;
12
13
    /** @var string */
14
    protected $name;
15
16
    /** @var array */
17
    protected $data;
18
19
    public function __construct(string $name, array $data = [])
20
    {
21
        $this->name = $name;
22
        $this->data = $data;
23
    }
24
25
    public static function create(string $name, array $data = [])
26
    {
27
        return new static($name, $data);
28
    }
29
30
    public function render(): string
31
    {
32
        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...
33
            ->with($this->data + ['active' => $this->isActive()])
34
            ->render();
35
    }
36
}
37