Completed
Pull Request — master (#121)
by
unknown
01:23
created

Sitemap::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
    /** @var array */
11
    protected $tags = [];
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
        if (! $this->hasUrl($tag->url)) {
33
            $this->tags[] = $tag;
34
        } else {
35
            $oldTag = $this->getUrl($tag->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...
36
            if ($tag->isNewer($oldTag)) {
37
                $this->update($oldTag, $tag);
0 ignored issues
show
Bug introduced by
It seems like $oldTag defined by $this->getUrl($tag->url) on line 35 can be null; however, Spatie\Sitemap\Sitemap::update() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Compatibility introduced by
$tag of type object<Spatie\Sitemap\Tags\Tag> is not a sub-type of object<Spatie\Sitemap\Tags\Url>. It seems like you assume a child class of the class Spatie\Sitemap\Tags\Tag to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
38
            }
39
        }
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param Url $oldTag
46
     * @param Url $newTag
47
     *
48
     * @return $this
49
     */
50
    public function update(Url $oldTag, Url $newTag)
51
    {
52
        array_splice($this->tags, array_search($oldTag, $this->tags), 1, [$newTag]);
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $url
59
     *
60
     * @return \Spatie\Sitemap\Tags\Url|null
61
     */
62
    public function getUrl(string $url)
63
    {
64
        return collect($this->tags)->first(function (Tag $tag) use ($url) {
65
            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...
66
        });
67
    }
68
69
    public function hasUrl(string $url): bool
70
    {
71
        return (bool) $this->getUrl($url);
72
    }
73
74
    public function render(): string
75
    {
76
        sort($this->tags);
77
78
        $tags = $this->tags;
79
80
        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...
81
            ->with(compact('tags'))
82
            ->render();
83
    }
84
85
    /**
86
     * @param string $path
87
     *
88
     * @return $this
89
     */
90
    public function writeToFile(string $path)
91
    {
92
        file_put_contents($path, $this->render());
93
94
        return $this;
95
    }
96
}
97