Completed
Pull Request — master (#245)
by
unknown
01:21
created

Sitemap::setFormatDocument()   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
8
class Sitemap
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $tags = [];
14
15
    /**
16
     * @var bool
17
     */
18
    protected $formatDocument = false;
19
20
    /**
21
     * @return self
22
     */
23
    public static function create(): self
24
    {
25
        return new static();
26
    }
27
28
    /**
29
     * @param string|\Spatie\Sitemap\Tags\Tag $tag
30
     *
31
     * @return self
32
     */
33
    public function add($tag): self
34
    {
35
        if (is_string($tag)) {
36
            $tag = Url::create($tag);
37
        }
38
39
        if (!in_array($tag, $this->tags)) {
40
            $this->tags[] = $tag;
41
        }
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function getTags(): array
50
    {
51
        return $this->tags;
52
    }
53
54
    /**
55
     * @param string $url
56
     *
57
     * @return self
58
     */
59
    public function getUrl(string $url): ?Url
60
    {
61
        return collect($this->tags)->first(function (Tag $tag) use ($url) {
62
            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...
63
        });
64
    }
65
66
    /**
67
     * @param string $url
68
     *
69
     * @return bool
70
     */
71
    public function hasUrl(string $url): bool
72
    {
73
        return (bool)$this->getUrl($url);
74
    }
75
76
    /**
77
     * @return string
78
     * @throws \Throwable
79
     */
80
    public function render(): string
81
    {
82
        sort($this->tags);
83
84
        $tags = $this->tags;
85
86
        $header = view('laravel-sitemap::header')->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...
87
        $document = 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...
88
                ->with(compact('tags'))
89
                ->render();
90
91
        if ($this->formatDocument) {
92
            $document = self::formatDocument($document);
93
        }
94
95
        return $header . $document;
96
    }
97
98
    /**
99
     * @param string $path
100
     *
101
     * @return self
102
     * @throws \Throwable
103
     */
104
    public function writeToFile(string $path): self
105
    {
106
        file_put_contents($path, $this->render());
107
108
        return $this;
109
    }
110
111
    /**
112
     * @param bool $formatDocument
113
     *
114
     * @return self
115
     */
116
    public function setFormatDocument(bool $formatDocument = true)
117
    {
118
        $this->formatDocument = $formatDocument;
119
120
        return $this;
121
    }
122
123
    /**
124
     * @param string $xml
125
     *
126
     * @return string
127
     */
128
    protected static function formatDocument(string $xml): string
129
    {
130
        if (!extension_loaded('dom')) {
131
            return $xml;
132
        }
133
134
        $dom = new \DOMDocument();
135
        $dom->preserveWhiteSpace = false;
136
        $dom->formatOutput = true;
137
138
        $dom->loadXML($xml);
139
140
        return $dom->saveXML($dom->documentElement) . PHP_EOL;
141
    }
142
}
143