Completed
Push — master ( 1f7f5f...3bc145 )
by Jan-Petter
06:44
created

UnavailableAfter::getDirective()   A

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
namespace vipnytt\XRobotsTagParser\directives;
3
4
final class UnavailableAfter implements directiveInterface
5
{
6
    const DIRECTIVE = 'unavailable_after';
7
8
    const DATE_GOOGLE = 'd M Y H:i:s T';
9
10
    private $supportedDateFormats = [
11
        DATE_RFC850,
12
        self::DATE_GOOGLE
13
    ];
14
15
    private $value;
16
17
    /**
18
     * Constructor
19
     *
20
     * @param string $rule
21
     * @param array $options
22
     */
23
    public function __construct($rule, $options = [])
24
    {
25
        foreach ($options as $key => $value) {
26
            if (isset($this->$key)) {
27
                $this->$key = $value;
28
            }
29
        }
30
        $this->value = $rule;
31
    }
32
33
    /**
34
     * Get directive name
35
     *
36
     * @return string
37
     */
38
    public function getDirective()
39
    {
40
        return self::DIRECTIVE;
41
    }
42
43
    /**
44
     * Get value
45
     *
46
     * @return string|null
47
     */
48
    public function getValue()
49
    {
50
        $parts = explode(',', trim(substr($this->value, mb_stripos($this->value, self::DIRECTIVE) + mb_strlen(self::DIRECTIVE) + 1)));
51
        for ($i = 1; $i <= count($parts); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
52
            foreach ($this->supportedDateFormats as $format) {
53
                $dateTime = date_create_from_format($format, trim(implode(',', array_slice($parts, 0, $i))));
54
                if ($dateTime !== false) {
55
                    return $dateTime->format(DATE_RFC850);
56
                }
57
            }
58
        }
59
        return null;
60
    }
61
}
62
63