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.

LastOf   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 69
loc 69
ccs 26
cts 27
cp 0.963
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A select() 17 17 2
A getLastInPeriod() 17 17 4

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 COG\ChronoShifter\Selector;
4
5
use COG\ChronoShifter\Direction\Direction;
6
use COG\ChronoShifter\Evaluator\Evaluator;
7
use COG\ChronoShifter\Period\Period;
8
9
/**
10
 * @author Kristjan Siimson <[email protected]>
11
 * @package Selector\Domain
12
 */
13 View Code Duplication
class LastOf implements Selector
0 ignored issues
show
Duplication introduced by
This class 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...
14
{
15
    /**
16
     * @var Direction
17
     */
18
    private $direction;
19
20
    /**
21
     * @var Evaluator
22
     */
23
    private $evaluator;
24
25
    /**
26
     * @param Direction $direction
27
     * @param Evaluator $evaluator
28
     */
29 22
    public function __construct(Direction $direction, Evaluator $evaluator)
30
    {
31 22
        $this->direction = $direction;
32 22
        $this->evaluator = $evaluator;
33 22
    }
34
35
    /**
36
     * @param string $date
37
     * @param Period $period
38
     * @return string
39
     */
40 22
    public function select($date, Period $period)
41
    {
42 22
        $match = $this->getLastInPeriod(
43 22
            new \DateTime($period->getStartDate()),
44 22
            new \DateTime($period->getEndDate())
45 22
        );
46
47 22
        if ($this->direction->compare($date, $match) !== -1) {
48 15
            $this->direction->next($period);
49 15
            $match = $this->getLastInPeriod(
50 15
                new \DateTime($period->getStartDate()),
51 15
                new \DateTime($period->getEndDate())
52 15
            );
53 15
        }
54
55 22
        return $match;
56
    }
57
58
    /**
59
     * @param \DateTime $startDate
60
     * @param \DateTime $endDate
61
     * @return null|string
62
     * @throws \LogicException
63
     */
64 22
    private function getLastInPeriod(\DateTime $startDate, \DateTime $endDate)
65
    {
66 22
        $match = null;
67
68 22
        for ($i = clone $endDate; $i > $startDate; $i->sub(new \DateInterval('P1D'))) {
69 22
            if ($this->evaluator->is($i->format('Y-m-d'))) {
70 22
                $match = $i->format('Y-m-d');
71 22
                break;
72
            }
73 17
        }
74
75 22
        if ($match === null) {
76
            throw new \LogicException('No match in period');
77
        }
78
79 22
        return $match;
80
    }
81
}
82