Completed
Push — master ( 68925d...28540c )
by Freek
01:18
created

Sitemap::toResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Sitemap;
4
5
use Spatie\Sitemap\Tags\Tag;
6
use Spatie\Sitemap\Tags\Url;
7
use Illuminate\Support\Facades\Response;
8
use Illuminate\Contracts\Support\Responsable;
9
10
class Sitemap implements Responsable
11
{
12
    /** @var array */
13
    protected $tags = [];
14
15
    public static function create(): self
16
    {
17
        return new static();
18
    }
19
20
    /**
21
     * @param string|\Spatie\Sitemap\Tags\Tag $tag
22
     *
23
     * @return $this
24
     */
25
    public function add($tag): self
26
    {
27
        if (is_string($tag)) {
28
            $tag = Url::create($tag);
29
        }
30
31
        if (! in_array($tag, $this->tags)) {
32
            $this->tags[] = $tag;
33
        }
34
35
        return $this;
36
    }
37
38
    public function getTags(): array
39
    {
40
        return $this->tags;
41
    }
42
43
    public function getUrl(string $url): ?Url
44
    {
45
        return collect($this->tags)->first(function (Tag $tag) use ($url) {
46
            return $tag->getType() === 'url' && $tag->url === $url;
0 ignored issues
show
Bug introduced by
The property url does not seem to exist in Spatie\Sitemap\Tags\Tag.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
        });
48
    }
49
50
    public function hasUrl(string $url): bool
51
    {
52
        return (bool) $this->getUrl($url);
53
    }
54
55
    public function render(): string
56
    {
57
        sort($this->tags);
58
59
        $tags = collect($this->tags)->unique('url');
60
61
        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...
62
            ->with(compact('tags'))
63
            ->render();
64
    }
65
66
    public function writeToFile(string $path): self
67
    {
68
        file_put_contents($path, $this->render());
69
70
        return $this;
71
    }
72
73
    /**
74
     * Create an HTTP response that represents the object.
75
     *
76
     * @param  \Illuminate\Http\Request  $request
77
     * @return \Symfony\Component\HttpFoundation\Response
78
     */
79
    public function toResponse($request)
80
    {
81
        return Response::make($this->render(), 200, [
82
            'Content-Type' => 'text/xml',
83
        ]);
84
    }
85
}
86