OnceMonthRule::seconds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 */
8
9
namespace AnimeDb\SmartSleep\Rule;
10
11
class OnceMonthRule implements Rule
12
{
13
    /**
14
     * @var \DateTime
15
     */
16
    private $time;
17
18 3
    public function __construct()
19
    {
20 3
        $this->time = new \DateTimeImmutable(); // default time
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DateTimeImmutable() of type object<DateTimeImmutable> is incompatible with the declared type object<DateTime> of property $time.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
21 3
    }
22
23
    /**
24
     * @param \DateTimeImmutable $time
25
     *
26
     * @return bool
27
     */
28 2
    public function isMatched(\DateTimeImmutable $time)
29
    {
30 2
        $this->time = $time; // save current time
0 ignored issues
show
Documentation Bug introduced by
It seems like $time of type object<DateTimeImmutable> is incompatible with the declared type object<DateTime> of property $time.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
32 2
        return true;
33
    }
34
35
    /**
36
     * @return int
37
     */
38 3
    public function seconds()
39
    {
40
        // interval duration [next month, next month +1)
41 3
        $offset_time = $this->time->modify('first day of this month')->modify('+1 month')->setTime(0, 0, 0);
42
43 3
        $limit_time = $offset_time->modify('+1 month');
44 3
        $limit = $limit_time->getTimestamp() - $offset_time->getTimestamp();
45
46
        // offset to next month
47 3
        $offset = $offset_time->getTimestamp() - $this->time->getTimestamp();
48
49 3
        return $offset + rand(0, $limit);
50
    }
51
}
52