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

Calender::validateInput()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 6
Ratio 66.67 %

Importance

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