GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7bf065...13ae28 )
by Axel
02:07
created

DatetimeComparison::getDatetimeFromFormat()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 13
cts 14
cp 0.9286
rs 8.5806
cc 4
eloc 21
nc 6
nop 1
crap 4.0058
1
<?php
2
3
namespace PhpAbac\Comparison;
4
5
class DatetimeComparison extends AbstractComparison
6
{
7
    /**
8
     * Return true if the given formatted datetime is between two other datetimes.
9
     * 
10
     * @param \DateTime $start
11
     * @param \DateTime $end
12
     * @param \DateTime $datetime
13
     *
14
     * @return bool
15
     */
16 1
    public function isBetween(\DateTime $start, \DateTime $end, $datetime)
17
    {
18 1
        return $start <= $datetime && $end >= $datetime;
19
    }
20
21
    /**
22
     * @param string $format
23
     * @param \DateTime $datetime
24
     *
25
     * @return bool
26
     */
27 3
    public function isMoreRecentThan($format, $datetime)
28
    {
29 3
        return $this->getDatetimeFromFormat($format) <= $datetime;
30
    }
31
32
    /**
33
     * @param string $format
34
     * @param \DateTime $datetime
35
     *
36
     * @return bool
37
     */
38 1
    public function isLessRecentThan($format, $datetime)
39
    {
40 1
        return $this->getDatetimeFromFormat($format) >= $datetime;
41
    }
42
43
    /**
44
     * @param string $format
45
     *
46
     * @return \DateTime
47
     */
48 4
    public function getDatetimeFromFormat($format)
49
    {
50
        $formats = [
51 4
            'Y' => 31104000,
52
            'M' => 2592000,
53
            'D' => 86400,
54
            'H' => 3600,
55
            'm' => 60,
56
            's' => 1,
57
        ];
58 4
        $operator = $format{0};
59 4
        $format = substr($format, 1);
60 4
        $time = 0;
61
62 4
        foreach ($formats as $scale => $seconds) {
63 4
            $data = explode($scale, $format);
64
65 4
            if (strlen($format) === strlen($data[0])) {
66 4
                continue;
67
            }
68 4
            $time += $data[0] * $seconds;
69
            // Remaining format string
70 4
            $format = $data[1];
71
        }
72
73
        return
74 4
            ($operator === '+')
75
            ? (new \DateTime())->setTimestamp((time() + $time))
76 4
            : (new \DateTime())->setTimestamp((time() - $time))
77
        ;
78
    }
79
}
80