Period   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 47.06 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 24
loc 51
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A days() 8 8 1
A months() 8 8 1
A years() 8 8 1
A __construct() 0 10 2

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
namespace Spatie\Analytics;
4
5
use Carbon\Carbon;
6
use DateTime;
7
use Spatie\Analytics\Exceptions\InvalidPeriod;
8
9
class Period
10
{
11
    /** @var \DateTime */
12
    public $startDate;
13
14
    /** @var \DateTime */
15
    public $endDate;
16
17
    public static function create(DateTime $startDate, DateTime $endDate): self
18
    {
19
        return new static($startDate, $endDate);
20
    }
21
22 View Code Duplication
    public static function days(int $numberOfDays): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        $endDate = Carbon::today();
25
26
        $startDate = Carbon::today()->subDays($numberOfDays)->startOfDay();
27
28
        return new static($startDate, $endDate);
29
    }
30
31 View Code Duplication
    public static function months(int $numberOfMonths): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        $endDate = Carbon::today();
34
35
        $startDate = Carbon::today()->subMonths($numberOfMonths)->startOfDay();
36
37
        return new static($startDate, $endDate);
38
    }
39
40 View Code Duplication
    public static function years(int $numberOfYears): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $endDate = Carbon::today();
43
44
        $startDate = Carbon::today()->subYears($numberOfYears)->startOfDay();
45
46
        return new static($startDate, $endDate);
47
    }
48
49
    public function __construct(DateTime $startDate, DateTime $endDate)
50
    {
51
        if ($startDate > $endDate) {
52
            throw InvalidPeriod::startDateCannotBeAfterEndDate($startDate, $endDate);
53
        }
54
55
        $this->startDate = $startDate;
56
57
        $this->endDate = $endDate;
58
    }
59
}
60