SiteMapUrl::getFrequency()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of sitemap-common.
4
 *
5
 * (c) 2016 Daniele Moraschi
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SiteMap\Schema;
12
13
14
use SiteMap\Http\Url;
15
16
class SiteMapUrl
17
{
18
19
    const DAILY     = 'daily';
20
    const WEEKLY    = 'weekly';
21
    const MONTHLY   = 'monthly';
22
    const YEARLY    = 'yearly';
23
24
    /**
25
     * @var Url
26
     */
27
    private $url;
28
29
    /**
30
     * @var string
31
     */
32
    private $frequency;
33
34
    /**
35
     * @var float
36
     */
37
    private $priority;
38
39
    /**
40
     * SiteMapUrl constructor.
41
     * @param Url $url
42
     * @param string $frequency
43
     * @param float $priority
44
     */
45
    public function __construct(Url $url, $frequency, $priority)
46
    {
47
        $this->url = $url;
48
        $this->priority = abs(number_format($priority, 1));
0 ignored issues
show
Documentation Bug introduced by
It seems like abs(number_format($priority, 1)) 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...
49
        $this->setFrequency($frequency);
50
    }
51
52
    /**
53
     * @return Url
54
     */
55
    public function getUrl()
56
    {
57
        return $this->url;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getFrequency()
64
    {
65
        return $this->frequency;
66
    }
67
68
    /**
69
     * @return float
70
     */
71
    public function getPriority()
72
    {
73
        return $this->priority;
74
    }
75
76
    /**
77
     * @param string $frequency
78
     */
79
    protected function setFrequency($frequency)
80
    {
81
        switch ($frequency) {
82
            case self::DAILY:
83
            case self::WEEKLY:
84
            case self::MONTHLY:
85
            case self::YEARLY:
86
                $this->frequency = $frequency;
87
                break;
88
            default:
89
                throw new \InvalidArgumentException(
90
                    sprintf('Invalid frequency "%s" for URL "%s"', $frequency, $this->url->getWebUrl())
91
                );
92
        }
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function toArray()
99
    {
100
        return array(
101
            'url' => $this->getUrl()->getWebUrl(),
102
            'priority' => $this->getPriority(),
103
            'frequency' => $this->getFrequency(),
104
        );
105
    }
106
107
}