Test Failed
Pull Request — master (#28)
by Christopher
04:26 queued 02:02
created

Calender::valdidateState()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 9
rs 8.2222
cc 7
eloc 5
nc 3
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Barnso
5
 * Date: 14/07/2017
6
 * Time: 8:28 PM.
7
 */
8
namespace AlgoWeb\xsdTypes\AxillaryClasses;
9
10
class Calender
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
11
{
12
    const timezoneRegexPattern = '([-+][0-1]\d:[0-6]\d|Z*)';
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected TIMEZONEREGEXPATTERN).
Loading history...
13
    private $year;
14
    private $month;
15
    private $day;
16
    private $timeZone;
17
18
    /**
19
     * Calender constructor.
20
     *
21
     * @param int|null    $year
22
     * @param int|null    $month
23
     * @param int|null    $day
24
     * @param string|null $timezone
25
     */
26
    private function __construct($year = null, $month = null, $day = null, $timezone = null)
27
    {
28
        $this->validateInput($year, $month, $day);
29
        $this->year = $year;
30
        $this->month = $month;
31
        $this->day = $day;
32
        $this->timeZone = $timezone;
33
        $this->valdidateState();
34
    }
35
36
    /**
37
     * @param int $year  |null
0 ignored issues
show
Documentation introduced by
Should the type for parameter $year not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     * @param int $month |null
0 ignored issues
show
Documentation introduced by
Should the type for parameter $month not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
     * @param int $day   |null
0 ignored issues
show
Documentation introduced by
Should the type for parameter $day not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     *
41
     * @return void
42
     */
43
    private function validateInput($year = null, $month = null, $day = null)
44
    {
45 View Code Duplication
        if (null === $year && null === $month && null === $day) {
46
            throw new \InvalidArgumentException('A Caldender class must have at least a day, month, or year');
47
        }
48 View Code Duplication
        if (null !== $year && null === $month && null !== $day) {
49
            throw new \InvalidArgumentException('a year day value is not valid');
50
        }
51
    }
52
53
    /**
54
     * @return void
55
     */
56
    private function valdidateState()
57
    {
58
        if (null !== $this->day && (1 > $this->day || $this->getMaxDays() < $this->day)) {
59
            throw new \InvalidArgumentException('the day must be greater 0 and less then 32');
60
        }
61
        if (null !== $this->month && (1 > $this->month || 12 < $this->month)) {
62
            throw new \InvalidArgumentException('the month must be greater 0 and less then 12');
63
        }
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    private function getMaxDays()
70
    {
71
        if (null !== $this->month) {
72
            return cal_days_in_month(CAL_GREGORIAN, $this->month, $this->getYearOrHolder());
73
        }
74
        return 31;
75
    }
76
77
    /**
78
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
79
     */
80
    private function getYearOrHolder()
81
    {
82
        if (null == $this->year) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $this->year of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
83
            return '2016';
84
        }
85
        return $this->year;
86
    }
87
88
    /**
89
     * @param string $day should match regex pattern ----\d\d
90
     *
91
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
92
     */
93 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...
94
    {
95
        $re = '/----(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
96
        preg_match_all($re, $day, $matches, PREG_SET_ORDER, 0);
97
        if (count($matches) != 1 && count($matches[0]) != 3) {
98
            throw new \InvalidArgumentException('Unable to extract day from input string');
99
        }
100
        return new self(null, null, $matches[0][1], $matches[0][2]);
101
    }
102
103
    /**
104
     * @param string $monthDay
105
     *
106
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
107
     */
108 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...
109
    {
110
        $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*)/';
111
        preg_match_all($re, $monthDay, $matches, PREG_SET_ORDER, 0);
112
        if (count($matches) != 1 && count($matches[0]) != 4) {
113
            throw new \InvalidArgumentException('Unable to extract month day from input string');
114
        }
115
        return new self(null, $matches[0][1], $matches[0][2], $matches[0][3]);
116
    }
117
118
    /**
119
     * @param string $month
120
     *
121
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
122
     */
123 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...
124
    {
125
        $re = '/--(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
126
        preg_match_all($re, $month, $matches, PREG_SET_ORDER, 0);
127
        if (count($matches) != 1 && count($matches[0]) != 3) {
128
            throw new \InvalidArgumentException('Unable to extract month from input string');
129
        }
130
        return new self(null, $matches[0][1], null, $matches[0][2]);
131
    }
132
133
    /**
134
     * @param string $yearMonth
135
     *
136
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
137
     */
138 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...
139
    {
140
        $re = '/(\d{4})-(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
141
        preg_match_all($re, $yearMonth, $matches, PREG_SET_ORDER, 0);
142
        if (count($matches) != 1 && count($matches[0]) != 3) {
143
            throw new \InvalidArgumentException('Unable to extract month from input string');
144
        }
145
        return new self($matches[0][1], null, $matches[0][2], $matches[0][3]);
146
    }
147
148
    /**
149
     * @param string $year
150
     *
151
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
152
     */
153 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...
154
    {
155
        $re = '/(\d{4})([-+][0-1]\d:[0-6]\d|Z*)/';
156
        preg_match_all($re, $year, $matches, PREG_SET_ORDER, 0);
157
        if (count($matches) != 1 && count($matches[0]) != 3) {
158
            throw new \InvalidArgumentException('Unable to extract month from input string');
159
        }
160
        return new self($matches[0][1], null, null, $matches[0][2]);
161
    }
162
}
163