Url   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 4
dl 0
loc 140
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 8 1
A setUrl() 0 6 1
A setLastModificationDate() 0 6 1
A setChangeFrequency() 0 6 1
A setPriority() 0 6 1
A addAlternate() 0 6 1
A path() 0 4 1
A segments() 0 15 2
A segment() 0 4 1
1
<?php
2
3
namespace Spatie\Sitemap\Tags;
4
5
use Carbon\Carbon;
6
use DateTime;
7
8
class Url extends Tag
9
{
10
    const CHANGE_FREQUENCY_ALWAYS = 'always';
11
    const CHANGE_FREQUENCY_HOURLY = 'hourly';
12
    const CHANGE_FREQUENCY_DAILY = 'daily';
13
    const CHANGE_FREQUENCY_WEEKLY = 'weekly';
14
    const CHANGE_FREQUENCY_MONTHLY = 'monthly';
15
    const CHANGE_FREQUENCY_YEARLY = 'yearly';
16
    const CHANGE_FREQUENCY_NEVER = 'never';
17
18
    /** @var string */
19
    public $url = '';
20
21
    /** @var \Carbon\Carbon */
22
    public $lastModificationDate;
23
24
    /** @var string */
25
    public $changeFrequency;
26
27
    /** @var float */
28
    public $priority = 0.8;
29
30
    /** @var array */
31
    public $alternates = [];
32
33
    public static function create(string $url): self
34
    {
35
        return new static($url);
36
    }
37
38
    public function __construct(string $url)
39
    {
40
        $this->url = $url;
41
42
        $this->lastModificationDate = Carbon::now();
43
44
        $this->changeFrequency = static::CHANGE_FREQUENCY_DAILY;
45
    }
46
47
    /**
48
     * @param string $url
49
     *
50
     * @return $this
51
     */
52
    public function setUrl(string $url = '')
53
    {
54
        $this->url = $url;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param \DateTime $lastModificationDate
61
     *
62
     * @return $this
63
     */
64
    public function setLastModificationDate(DateTime $lastModificationDate)
65
    {
66
        $this->lastModificationDate = $lastModificationDate;
0 ignored issues
show
Documentation Bug introduced by
$lastModificationDate is of type object<DateTime>, but the property $lastModificationDate was declared to be of type object<Carbon\Carbon>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $changeFrequency
73
     *
74
     * @return $this
75
     */
76
    public function setChangeFrequency(string $changeFrequency)
77
    {
78
        $this->changeFrequency = $changeFrequency;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param float $priority
85
     *
86
     * @return $this
87
     */
88
    public function setPriority(float $priority)
89
    {
90
        $this->priority = max(0, min(1, $priority));
0 ignored issues
show
Documentation Bug introduced by
It seems like max(0, min(1, $priority)) can also be of type integer. However, the property $priority is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param Alternate $alternate
0 ignored issues
show
Bug introduced by
There is no parameter named $alternate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
97
     *
98
     * @param string $url
99
     * @param string $locale
100
     * @return $this
101
     */
102
    public function addAlternate(string $url, string $locale = '')
103
    {
104
        $this->alternates[] = new Alternate($url, $locale);
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function path(): string
113
    {
114
        return parse_url($this->url)['path'] ?? '';
115
    }
116
117
    /**
118
     * @param int|null $index
119
     *
120
     * @return array|null|string
121
     */
122
    public function segments(int $index = null)
123
    {
124
        $segments = collect(explode('/', $this->path()))
125
            ->filter(function ($value) {
126
                return $value !== '';
127
            })
128
            ->values()
129
            ->toArray();
130
131
        if (! is_null($index)) {
132
            return $this->segment($index);
133
        }
134
135
        return $segments;
136
    }
137
138
    /**
139
     * @param int $index
140
     *
141
     * @return string|null
142
     */
143
    public function segment(int $index)
144
    {
145
        return $this->segments()[$index - 1] ?? null;
146
    }
147
}
148