Completed
Push — master ( 0d3592...0bb29a )
by Peter
06:28
created

SmartUrl::changeFreqFromPriority()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
rs 5.1612
c 1
b 0
f 0
cc 12
eloc 26
nc 12
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Component\Sitemap\Url;
11
12
class SmartUrl extends Url
13
{
14
    /**
15
     * @param string                  $loc
16
     * @param \DateTimeImmutable|null $last_mod
17
     * @param string|null             $change_freq
18
     * @param string|null             $priority
19
     */
20
    public function __construct($loc, \DateTimeImmutable $last_mod = null, $change_freq = null, $priority = null)
21
    {
22
        // priority from loc
23
        if (!$priority) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $priority of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
24
            $priority = $this->priorityFromLoc($loc);
25
        }
26
27
        // change freq from last mod
28
        if (!$change_freq && $last_mod instanceof \DateTimeImmutable) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $change_freq of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
29
            $change_freq = $this->changeFreqFromLastMod($last_mod);
30
        }
31
32
        // change freq from priority
33
        if (!$change_freq) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $change_freq of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
34
            $change_freq = $this->changeFreqFromPriority($priority);
35
        }
36
37
        parent::__construct($loc, $last_mod, $change_freq, $priority);
38
    }
39
40
    /**
41
     * @param string $loc
42
     *
43
     * @return string
44
     */
45
    private function priorityFromLoc($loc)
46
    {
47
        // number of slashes
48
        $num = count(array_filter(explode('/', trim($loc, '/'))));
49
50
        if (!$num) {
51
            return '1.0';
52
        }
53
54
        if (($p = (10 - $num) / 10) > 0) {
55
            return '0.'.($p * 10);
56
        }
57
58
        return '0.1';
59
    }
60
61
    /**
62
     * @param \DateTimeImmutable $last_mod
63
     *
64
     * @return string|null
65
     */
66
    private function changeFreqFromLastMod(\DateTimeImmutable $last_mod)
67
    {
68
        if ($last_mod < new \DateTimeImmutable('-1 year')) {
69
            return self::CHANGE_FREQ_YEARLY;
70
        }
71
72
        if ($last_mod < new \DateTimeImmutable('-1 month')) {
73
            return self::CHANGE_FREQ_MONTHLY;
74
        }
75
76
        return null;
77
    }
78
79
    /**
80
     * @param string $priority
81
     *
82
     * @return string
83
     */
84
    private function changeFreqFromPriority($priority)
85
    {
86
        switch ($priority) {
87
            case '1.0':
88
                return self::CHANGE_FREQ_HOURLY;
89
            case '0.9':
90
                return self::CHANGE_FREQ_DAILY;
91
            case '0.8':
92
                return self::CHANGE_FREQ_DAILY;
93
            case '0.7':
94
                return self::CHANGE_FREQ_WEEKLY;
95
            case '0.6':
96
                return self::CHANGE_FREQ_WEEKLY;
97
            case '0.5':
98
                return self::CHANGE_FREQ_WEEKLY;
99
            case '0.4':
100
                return self::CHANGE_FREQ_MONTHLY;
101
            case '0.3':
102
                return self::CHANGE_FREQ_MONTHLY;
103
            case '0.2':
104
                return self::CHANGE_FREQ_YEARLY;
105
            case '0.1':
106
                return self::CHANGE_FREQ_YEARLY;
107
            case '0.0':
108
                return self::CHANGE_FREQ_NEVER;
109
            default:
110
                return null;
111
        }
112
    }
113
}
114