Completed
Pull Request — master (#478)
by Luc
02:08
created

SubEvents   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 96
Duplicated Lines 21.88 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 4
dl 21
loc 96
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createEmpty() 0 4 1
A createFromCalendar() 0 14 3
A getSubEvents() 0 4 1
A hasSubEvent() 0 10 3
A hasSubEventWithTimestamp() 0 10 3
A addSubEvent() 10 10 2
A updateSubEvent() 11 11 2
A removeSubEventWithTimestamp() 0 13 3

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
declare(strict_types=1);
4
5
namespace CultuurNet\UDB3\Event\ValueObjects;
6
7
use CultuurNet\UDB3\Calendar;
8
use CultuurNet\UDB3\Timestamp;
9
10
final class SubEvents
11
{
12
    /**
13
     * @var SubEvent[]
14
     */
15
    private $subEvents;
16
17
    private function __construct(SubEvent ...$subEvents)
18
    {
19
        $this->subEvents = $subEvents;
20
    }
21
22
    public static function createEmpty(): SubEvents
23
    {
24
        return new SubEvents(...[]);
25
    }
26
27
    public static function createFromCalendar(Calendar $calendar): SubEvents
28
    {
29
        if (empty($calendar->getTimestamps())) {
30
            return self::createEmpty();
31
        }
32
33
        $subEvents = [];
34
        // TODO III-3570: Take into account a calendar with the same timestamps.
35
        foreach ($calendar->getTimestamps() as $timestamp) {
36
            $subEvents[] = new SubEvent($timestamp, Status::scheduled());
37
        }
38
39
        return new SubEvents(...$subEvents);
40
    }
41
42
    public function getSubEvents(): array
43
    {
44
        return $this->subEvents;
45
    }
46
47
    public function hasSubEvent(SubEvent $subEventToSearch): bool
48
    {
49
        foreach ($this->subEvents as $subEvent) {
50
            if ($subEvent->equals($subEventToSearch)) {
51
                return true;
52
            }
53
        }
54
55
        return false;
56
    }
57
58
    public function hasSubEventWithTimestamp(Timestamp $timestampToSearch): bool
59
    {
60
        foreach ($this->subEvents as $subEvent) {
61
            if ($subEvent->getTimestamp()->equals($timestampToSearch)) {
62
                return true;
63
            }
64
        }
65
66
        return false;
67
    }
68
69 View Code Duplication
    public function addSubEvent(SubEvent $subEvent): SubEvents
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...
70
    {
71
        if ($this->hasSubEventWithTimestamp($subEvent->getTimestamp())) {
72
            return $this;
73
        }
74
75
        $this->subEvents[] = $subEvent;
76
77
        return $this;
78
    }
79
80 View Code Duplication
    public function updateSubEvent(SubEvent $subEvent): SubEvents
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...
81
    {
82
        if (!$this->hasSubEventWithTimestamp($subEvent->getTimestamp())) {
83
            return $this;
84
        }
85
86
        $this->removeSubEventWithTimestamp($subEvent->getTimestamp());
87
        $this->subEvents[] = $subEvent;
88
89
        return $this;
90
    }
91
92
    private function removeSubEventWithTimestamp(Timestamp $timestamp): SubEvents
93
    {
94
        $nrOfSubEvents = count($this->subEvents);
95
        for ($index = 0; $index < $nrOfSubEvents; $index++) {
96
            if ($this->subEvents[$index]->getTimestamp()->equals($timestamp)) {
97
                unset($this->subEvents[$index]);
98
            }
99
        }
100
101
        $this->subEvents = \array_values($this->subEvents);
102
103
        return $this;
104
    }
105
}
106