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.

DatetimeComparison   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 75
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isBetween() 0 4 2
A isMoreRecentThan() 0 4 1
A isLessRecentThan() 0 4 1
B getDatetimeFromFormat() 0 31 4
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 5
    public function isMoreRecentThan($format, $datetime)
28
    {
29 5
        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 6
    public function getDatetimeFromFormat($format)
49
    {
50
        $formats = [
51 6
            'Y' => 31104000,
52 6
            'M' => 2592000,
53 6
            'D' => 86400,
54 6
            'H' => 3600,
55 6
            'm' => 60,
56 6
            's' => 1,
57 6
        ];
58 6
        $operator = $format{0};
59 6
        $format = substr($format, 1);
60 6
        $time = 0;
61
62 6
        foreach ($formats as $scale => $seconds) {
63 6
            $data = explode($scale, $format);
64
65 6
            if (strlen($format) === strlen($data[0])) {
66 6
                continue;
67
            }
68 6
            $time += $data[0] * $seconds;
69
            // Remaining format string
70 6
            $format = $data[1];
71 6
        }
72
73
        return
74 6
            ($operator === '+')
75 6
            ? (new \DateTime())->setTimestamp((time() + $time))
76 6
            : (new \DateTime())->setTimestamp((time() - $time))
77 6
        ;
78
    }
79
}
80