Completed
Push — master ( 3b705c...d8bcc0 )
by Freek
02:18
created

Sitemap::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 0
1
<?php
2
3
namespace Spatie\Sitemap;
4
5
use Spatie\Sitemap\Tags\Url;
6
7
class Sitemap
8
{
9
    /** @var array */
10
    protected $tags = [];
11
12
13
    /**
14
     * @return static
15
     */
16
    public static function create()
17
    {
18
        return new static();
19
    }
20
21
    /**
22
     * @param string|\Spatie\Sitemap\Tags\Tag $tag
23
     *
24
     * @return $this
25
     */
26
    public function add($tag)
27
    {
28
        if (is_string($tag)) {
29
            $tag = Url::create($tag);
30
        }
31
32
        $this->tags[] = $tag;
33
34
        return $this;
35
    }
36
37
    public function render(): string
38
    {
39
        $tags = $this->tags;
40
41
        return view('laravel-sitemap::sitemap')
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...
42
            ->with(compact('tags'))
43
            ->render();
44
    }
45
46
    public function writeToFile(string $path)
47
    {
48
        file_put_contents($path, $this->render());
49
50
        return $this;
51
    }
52
}
53