Test Failed
Pull Request — master (#28)
by Christopher
03:22 queued 01:09
created

Calender   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 104
Duplicated Lines 49.04 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 35
c 2
b 1
f 0
lcom 2
cbo 0
dl 51
loc 104
rs 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B validateInput() 6 9 7
B valdidateState() 0 14 10
A getYearOrHolder() 0 7 2
A fromDay() 9 9 3
A FromMonthDay() 9 9 3
A fromMonth() 9 9 3
A fromYearMonth() 9 9 3
A fromYear() 9 9 3

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
 * 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
    private function validateInput($year = null, $month = null, $day = null)
27
    {
28 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...
29
            throw new \InvalidArgumentException('A Caldender class must have at least a day, month, or year');
30
        }
31 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...
32
            throw new \InvalidArgumentException('a year day value is not valid');
33
        }
34
    }
35
36
    private function valdidateState()
37
    {
38
        if (null !== $this->day && (1 > $this->day || 31 < $this->day)) {
39
            throw new \InvalidArgumentException('the day must be greater 0 and less then 32');
40
        }
41
        if (null !== $this->month && (1 > $this->month || 12 < $this->month)) {
42
            throw new \InvalidArgumentException('the month must be greater 0 and less then 12');
43
        }
44
        if ($this->month !== null && $this->day !== null &&
45
            cal_days_in_month($this->getYearOrHolder(), $this->month, $this->day) < $this->day
46
        ) {
47
            throw new \InvalidArgumentException('there are not that many days in the assigned month');
48
        }
49
    }
50
51
    private function getYearOrHolder()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
52
    {
53
        if (null == $this->year) {
54
            return '2016';
55
        }
56
        return $this->year;
57
    }
58
59
    /**
60
     * @param string $day should match regex pattern ----\d\d
61
     *
62
     * @return Calender
63
     */
64 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...
65
    {
66
        $re = '/----(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
67
        preg_match_all($re, $day, $matches, PREG_SET_ORDER, 0);
68
        if (count($matches) != 1 && count($matches[0]) != 3) {
69
            throw new \InvalidArgumentException('Unable to extract day from input string');
70
        }
71
        return new self(null, null, $matches[0][1], $matches[0][2]);
72
    }
73
74 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...
75
    {
76
        $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*)/';
77
        preg_match_all($re, $monthDay, $matches, PREG_SET_ORDER, 0);
78
        if (count($matches) != 1 && count($matches[0]) != 4) {
79
            throw new \InvalidArgumentException('Unable to extract month day from input string');
80
        }
81
        return new self(null, $matches[0][1], $matches[0][2], $matches[0][3]);
82
    }
83
84 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...
85
    {
86
        $re = '/--(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
87
        preg_match_all($re, $month, $matches, PREG_SET_ORDER, 0);
88
        if (count($matches) != 1 && count($matches[0]) != 3) {
89
            throw new \InvalidArgumentException('Unable to extract month from input string');
90
        }
91
        return new self(null, $matches[0][1], null, $matches[0][2]);
92
    }
93
94 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...
95
    {
96
        $re = '/(\d{4})-(1[0-2]|0[1-9]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
97
        preg_match_all($re, $yearMonth, $matches, PREG_SET_ORDER, 0);
98
        if (count($matches) != 1 && count($matches[0]) != 3) {
99
            throw new \InvalidArgumentException('Unable to extract month from input string');
100
        }
101
        return new self($matches[0][1], null, $matches[0][2], $matches[0][3]);
102
    }
103
104 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...
105
    {
106
        $re = '/(\d{4})([-+][0-1]\d:[0-6]\d|Z*)/';
107
        preg_match_all($re, $year, $matches, PREG_SET_ORDER, 0);
108
        if (count($matches) != 1 && count($matches[0]) != 3) {
109
            throw new \InvalidArgumentException('Unable to extract month from input string');
110
        }
111
        return new self($matches[0][1], null, null, $matches[0][2]);
112
    }
113
}
114