OnceWeekRule   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 0
dl 35
loc 35
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A isMatched() 6 6 1
A seconds() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
class OnceWeekRule implements Rule
12
{
13
    /**
14
     * @var \DateTime
15
     */
16
    private $time;
17
18 2
    public function __construct()
19
    {
20 2
        $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 2
    }
22
23
    /**
24
     * @param \DateTimeImmutable $time
25
     *
26
     * @return bool
27
     */
28 1
    public function isMatched(\DateTimeImmutable $time)
29
    {
30 1
        $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 1
        return true;
33
    }
34
35
    /**
36
     * @return int
37
     */
38 2
    public function seconds()
39
    {
40 2
        $offset_time = $this->time->modify('+1 week')->setTime(0, 0, 0);
41 2
        $offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next week
42
43 2
        return $offset + rand(0, 604800); // 604800 is a 1 week
44
    }
45
}
46