Test Failed
Pull Request — master (#28)
by Christopher
03:40 queued 01:10
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
/**
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
17
    private function __construct($year = null, $month = null, $day = null, $timezone = 'Z')
0 ignored issues
show
Unused Code introduced by
The parameter $timezone is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
    {
19
        $this->validateInput($year, $month, $day);
20
        $this->year = $year;
21
        $this->month = $month;
22
        $this->day = $day;
23
        $this->valdidateState();
24
    }
25
26
    /**
27
     * @param int $year
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...
28
     * @param int $month
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...
29
     * @param int $day
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...
30
     *
31
     * @return void
32
     */
33
    private function validateInput($year = null, $month = null, $day = null)
34
    {
35 View Code Duplication
        if (null === $year && null === $month && null === $day) {
1 ignored issue
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...
36
            throw new \InvalidArgumentException('A Caldender class must have at least a day, month, or year');
37
        }
38 View Code Duplication
        if (null !== $year && null === $month && null !== $day) {
1 ignored issue
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...
39
            throw new \InvalidArgumentException('a year day value is not valid');
40
        }
41
    }
42
43
    /**
44
     * @return void
45
     */
46
    private function valdidateState()
47
    {
48
        if (null !== $this->day && (1 > $this->day || $this->getMaxDays() < $this->day)) {
49
            throw new \InvalidArgumentException('the day must be greater 0 and less then 32');
50
        }
51
        if (null !== $this->month && (1 > $this->month || 12 < $this->month)) {
52
            throw new \InvalidArgumentException('the month must be greater 0 and less then 12');
53
        }
54
    }
55
56
    /**
57
     * @return integer
58
     */
59
    private function getMaxDays()
60
    {
61
        if (null !== $this->month) {
62
            return cal_days_in_month(CAL_GREGORIAN, $this->month, $this->getYearOrHolder());
63
        }
64
        return 31;
65
    }
66
67
    /**
68
     * @return integer
69
     */
70
    private function getYearOrHolder()
71
    {
72
        if (null == $this->year) {
73
            return '2016';
74
        }
75
        return $this->year;
76
    }
77
78
    /**
79
     * @param string $day should match regex pattern ----\d\d
80
     *
81
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
82
     */
83 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...
84
    {
85
        $re = '/----(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
86
        preg_match_all($re, $day, $matches, PREG_SET_ORDER, 0);
87
        if (count($matches) != 1 && count($matches[0]) != 3) {
88
            throw new \InvalidArgumentException('Unable to extract day from input string');
89
        }
90
        return new self(null, null, $matches[0][1], $matches[0][2]);
91
    }
92
93
    /**
94
     * @param string $monthDay
95
     *
96
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
97
     */
98 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...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
99
    {
100
        $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*)/';
101
        preg_match_all($re, $monthDay, $matches, PREG_SET_ORDER, 0);
102
        if (count($matches) != 1 && count($matches[0]) != 4) {
103
            throw new \InvalidArgumentException('Unable to extract month day from input string');
104
        }
105
        return new self(null, $matches[0][1], $matches[0][2], $matches[0][3]);
106
    }
107
108
    /**
109
     * @param string $month
110
     *
111
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
112
     */
113 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...
114
    {
115
        $re = '/--(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
116
        preg_match_all($re, $month, $matches, PREG_SET_ORDER, 0);
117
        if (count($matches) != 1 && count($matches[0]) != 3) {
118
            throw new \InvalidArgumentException('Unable to extract month from input string');
119
        }
120
        return new self(null, $matches[0][1], null, $matches[0][2]);
121
    }
122
123
    /**
124
     * @param string $yearMonth
125
     *
126
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
127
     */
128 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...
129
    {
130
        $re = '/(\d{4})-(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
131
        preg_match_all($re, $yearMonth, $matches, PREG_SET_ORDER, 0);
132
        if (count($matches) != 1 && count($matches[0]) != 3) {
133
            throw new \InvalidArgumentException('Unable to extract month from input string');
134
        }
135
        return new self($matches[0][1], null, $matches[0][2], $matches[0][3]);
136
    }
137
138
    /**
139
     * @param string $year
140
     *
141
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
142
     */
143 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...
144
    {
145
        $re = '/(\d{4})([-+][0-1]\d:[0-6]\d|Z*)/';
146
        preg_match_all($re, $year, $matches, PREG_SET_ORDER, 0);
147
        if (count($matches) != 1 && count($matches[0]) != 3) {
148
            throw new \InvalidArgumentException('Unable to extract month from input string');
149
        }
150
        return new self($matches[0][1], null, null, $matches[0][2]);
151
    }
152
}
153