Test Failed
Pull Request — master (#28)
by Christopher
02:27
created

Calender::validateInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
namespace AlgoWeb\xsdTypes\AxillaryClasses;
3
4
final class Calender
5
{
6
    private $year;
7
    private $month;
8
    private $day;
9
    private $timeZone;
10
11
    /**
12
     * Calender constructor.
13
     *
14
     * @param string|null $year
15
     * @param string|null $month
16
     * @param string|null $day
17
     * @param string|null $timezone
18
     */
19
    private function __construct($year = null, $month = null, $day = null, $timezone = null)
20
    {
21
        $this->validateInput($year, $month, $day);
22
        $this->year = $year;
23
        $this->month = $month;
24
        $this->day = $day;
25
        $this->timeZone = $timezone;
26
        $this->validateState();
27
    }
28
29
    /**
30
     * @param string|null $year
31
     * @param string|null $month
32
     * @param string|null $day
33
     *
34
     * @return void
35
     */
36
    private function validateInput($year, $month, $day)
37
    {
38
        $this->validateInputNotAllNull($year, $month, $day);
39
        $this->validateInputNotYearAndDay($year, $month, $day);
40
    }
41
42
    private function validateInputNotAllNull($year, $month, $day)
43
    {
44 View Code Duplication
        if (null === $year && null === $month && null === $day) {
45
            throw new \InvalidArgumentException('A Caldender class must have at least a day, month, or year');
46
        }
47
    }
48
49
    private function validateInputNotYearAndDay($year, $month, $day)
50
    {
51 View Code Duplication
        if (null !== $year && null === $month && null !== $day) {
52
            throw new \InvalidArgumentException('a year day value is not valid');
53
        }
54
    }
55
56
    /**
57
     * @return void
58
     */
59
    private function validateState()
60
    {
61
        $this->validateDay();
62
        $this->validateMonth();
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    private function validateDay()
69
    {
70
        if (null !== $this->day && (1 > $this->day || $this->getMaxDays() < $this->day)) {
71
            throw new \InvalidArgumentException('the day must be greater 0 and less then 32');
72
        }
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    private function getMaxDays()
79
    {
80
        if (null !== $this->month) {
81
            return (string)cal_days_in_month(CAL_GREGORIAN, $this->month, $this->getYearOrHolder());
82
        }
83
        return '31';
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    private function getYearOrHolder()
90
    {
91
        if (null === $this->year) {
92
            return '2016';
93
        }
94
        return $this->year;
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    private function validateMonth()
101
    {
102
        if (null !== $this->month && (1 > $this->month || 12 < $this->month)) {
103
            throw new \InvalidArgumentException('the month must be greater 0 and less then 12');
104
        }
105
    }
106
107
    /**
108
     * @param string $day should match regex pattern ----\d\d
109
     *
110
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
111
     */
112 View Code Duplication
    public static function fromDay($day)
1 ignored issue
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...
113
    {
114
        $re = '/----(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
115
        preg_match_all($re, $day, $matches, PREG_SET_ORDER, 0);
116
        if (count($matches) != 1 && count($matches[0]) != 3) {
117
            throw new \InvalidArgumentException('Unable to extract day from input string');
118
        }
119
        return new self(null, null, $matches[0][1], $matches[0][2]);
120
    }
121
122
    /**
123
     * @param string $monthDay
124
     *
125
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
126
     */
127 View Code Duplication
    public static function fromMonthDay($monthDay)
1 ignored issue
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...
128
    {
129
        $re = '/--(1[0-2]|0[1-9]|[1-9])-(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
130
        preg_match_all($re, $monthDay, $matches, PREG_SET_ORDER, 0);
131
        if (count($matches) != 1 && count($matches[0]) != 4) {
132
            throw new \InvalidArgumentException('Unable to extract month day from input string');
133
        }
134
        return new self(null, $matches[0][1], $matches[0][2], $matches[0][3]);
135
    }
136
137
    /**
138
     * @param string $month
139
     *
140
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
141
     */
142 View Code Duplication
    public static function fromMonth($month)
1 ignored issue
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...
143
    {
144
        $re = '/--(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
145
        preg_match_all($re, $month, $matches, PREG_SET_ORDER, 0);
146
        if (count($matches) != 1 && count($matches[0]) != 3) {
147
            throw new \InvalidArgumentException('Unable to extract month from input string');
148
        }
149
        return new self(null, $matches[0][1], null, $matches[0][2]);
150
    }
151
152
    /**
153
     * @param string $yearMonth
154
     *
155
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
156
     */
157 View Code Duplication
    public static function fromYearMonth($yearMonth)
1 ignored issue
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...
158
    {
159
        $re = '/(\d{4})-(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
160
        preg_match_all($re, $yearMonth, $matches, PREG_SET_ORDER, 0);
161
        if (count($matches) != 1 && count($matches[0]) != 3) {
162
            throw new \InvalidArgumentException('Unable to extract month from input string');
163
        }
164
        return new self($matches[0][1], null, $matches[0][2], $matches[0][3]);
165
    }
166
167
    /**
168
     * @param string $year
169
     *
170
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
171
     */
172 View Code Duplication
    public static function fromYear($year)
1 ignored issue
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...
173
    {
174
        $re = '/(\d{4})([-+][0-1]\d:[0-6]\d|Z*)/';
175
        preg_match_all($re, $year, $matches, PREG_SET_ORDER, 0);
176
        if (count($matches) != 1 && count($matches[0]) != 3) {
177
            throw new \InvalidArgumentException('Unable to extract month from input string');
178
        }
179
        return new self($matches[0][1], null, null, $matches[0][2]);
180
    }
181
}
182