Completed
Push — master ( d6426f...b73070 )
by steef
09:23 queued 03:48
created

AbstractSpecialDate   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 205
Duplicated Lines 3.9 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 89.06%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 0
dl 8
loc 205
ccs 57
cts 64
cp 0.8906
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 8 26 3
A normalizeDescription() 0 11 1
A generateDateTime() 0 10 2
A setupDateTimeObjects() 0 14 2
A getStartDate() 0 8 3
A getEndDate() 0 8 3
A getDescription() 0 4 1
A getTotalLength() 0 4 1
A isValid() 0 4 1
A isBankHoliday() 0 4 1
A isNationalAcceptedParty() 0 4 1
A getYear() 0 4 1
generate() 0 1 ?
A getNormalizedDescription() 0 4 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
namespace Stefanius\SpecialDates\SDK;
4
5
abstract class AbstractSpecialDate implements SpecialDateInterface
6
{
7
    /**
8
     * @var \DateTime
9
     */
10
    protected $startDate;
11
12
    /**
13
     * @var \DateTime
14
     */
15
    protected $endDate;
16
17
    /**
18
     * @var \DateTime
19
     */
20
    protected $zeroDate;
21
22
    /**
23
     * @var string
24
     */
25
    protected $description;
26
27
    /**
28
     * @var int
29
     */
30
    protected $totalLength = 0;
31
32
    /**
33
     * @var bool
34
     */
35
    protected $valid = true;
36
37
    /**
38
     * @var bool
39
     */
40
    protected $bankHoliday = false;
41
42
    /**
43
     * @var bool
44
     */
45
    protected $nationalAcceptedParty = false;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $nationalAcceptedParty exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
46
47
    /**
48
     * @var string
49
     */
50
    protected $normalizedDescription = false;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $normalizedDescription exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
51
    /**
52
     * @var int
53
     */
54
    protected $year;
55
56 68
    public function __construct($year)
57
    {
58 68
        $this->year     = $year;
59 68
        $this->zeroDate = new \DateTime();
60 68
        $this->zeroDate->setDate(1, 1, 1);
61
62 68
        $this->startDate = new \DateTime();
63 68
        $this->startDate->setDate(1, 1, 1);
64
65 68
        $this->endDate = new \DateTime();
66 68
        $this->endDate->setDate(1, 1, 1);
67
68 68
        $this->generate();
69
70 65 View Code Duplication
        if ($this->startDate->format('Y') . '-' . $this->startDate->format('m') . '-' . $this->startDate->format('d') === '0001-01-01') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71 27
            $this->valid = false;
72 27
            $this->totalLength = 0;
73 27
        }
74
75 65 View Code Duplication
        if ($this->endDate->format('Y') . '-' . $this->endDate->format('m') . '-' . $this->endDate->format('d') === '0001-01-01') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
76 27
            $this->valid = false;
77 27
            $this->totalLength = 0;
78 27
        }
79
80 65
        $this->normalizeDescription();
81 65
    }
82
83 65
    protected function normalizeDescription()
84
    {
85 65
        $string = iconv('UTF-8', 'ASCII//TRANSLIT', $this->description);
86 65
        $string = trim($string);
87 65
        $string = preg_replace("/[^a-zA-Z0-9_| -]/", ' ', $string);
88 65
        $string = preg_replace("/[| -]+/", '-', $string);
89 65
        $string = strtolower(trim($string, '-'));
90 65
        $string = preg_replace('/-{2,}/', ' ', $string);
91
92 65
        $this->normalizedDescription = $string;
93 65
    }
94
95
    /**
96
     * @param $year
97
     * @param $month
98
     * @param $day
99
     *
100
     * @return \DateTime
101
     */
102 15
    protected function generateDateTime($year, $month, $day)
103
    {
104 15
        $date = \DateTime::createFromFormat('Y-m-d', $year . '-' . $month . '-' . $day);
105
106 15
        if ($date === false) {
107
            return $this->zeroDate;
108
        }
109
110 15
        return $date;
111
    }
112
113 21
    protected function setupDateTimeObjects(\DateTime $start, \DateTime $end = null)
114
    {
115 21
        $this->startDate = $start;
116
117 21
        if ($end === null) {
118 17
            $this->endDate = $start;
119 17
        } else {
120 4
            $this->endDate = $end;
121
        }
122
123 21
        $interval = $this->startDate->diff($this->endDate);
124
125 21
        $this->totalLength = (int)$interval->format('%R%a') + 1;
126 21
    }
127
128
    /**
129
     * @return \DateTime
130
     */
131 63
    public function getStartDate()
132
    {
133 63
        if (!$this->valid || $this->startDate == null) {
134 25
            return $this->zeroDate;
135
        }
136
137 38
        return $this->startDate;
138
    }
139
140
    /**
141
     * @return \DateTime
142
     */
143 63
    public function getEndDate()
144
    {
145 63
        if (!$this->valid || $this->endDate == null) {
146 25
            return $this->zeroDate;
147
        }
148
149 38
        return $this->endDate;
150
    }
151
152
    /**
153
     * @return string
154
     */
155 63
    public function getDescription()
156
    {
157 63
        return $this->description;
158
    }
159
160
    /**
161
     * @return int
162
     */
163 63
    public function getTotalLength()
164
    {
165 63
        return $this->totalLength;
166
    }
167
168
    /**
169
     * @return bool
170
     */
171 63
    public function isValid()
172
    {
173 63
        return $this->valid;
174
    }
175
176
    /**
177
     * @return boolean
178
     */
179 63
    public function isBankHoliday()
180
    {
181 63
        return $this->bankHoliday;
182
    }
183
184
    /**
185
     * @return boolean
186
     */
187
    public function isNationalAcceptedParty()
188
    {
189
        return $this->nationalAcceptedParty;
190
    }
191
192
    /**
193
     * @return int
194
     */
195
    public function getYear()
196
    {
197
        return $this->year;
198
    }
199
200
    /**
201
     * @return string
202
     */
203
    public function getNormalizedDescription()
204
    {
205
        return $this->normalizedDescription;
206
    }
207
208
    abstract protected function generate();
209
}
210