CalenderFactory   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 77
Duplicated Lines 58.44 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 8
Bugs 4 Features 0
Metric Value
wmc 15
c 8
b 4
f 0
lcom 1
cbo 1
dl 45
loc 77
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDay() 9 9 3
A fromMonthDay() 9 9 3
A fromMonth() 9 9 3
A fromYear() 9 9 3
A fromYearMonth() 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
namespace AlgoWeb\xsdTypes\AxillaryClasses;
4
5
class CalenderFactory extends Calender
6
{
7
    /**
8
     * @param string $day should match regex pattern ----\d\d
9
     *
10
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
11
     */
12 View Code Duplication
    public static function fromDay($day)
13
    {
14
        $dayTimezone = '/----(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
15
        preg_match_all($dayTimezone, $day, $matches, PREG_SET_ORDER, 0);
16
        if (count($matches) != 1 || count($matches[0]) != 3) {
17
            throw new \InvalidArgumentException('Unable to extract day from input string');
18
        }
19
        return new self(null, null, $matches[0][1], $matches[0][2]);
20
    }
21
22
    /**
23
     * @param string $monthDay
24
     *
25
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
26
     */
27 View Code Duplication
    public static function fromMonthDay($monthDay)
28
    {
29
        $monthDayTimezone = '/--((?:1[0-2])|(?:0[0-9]))-(0[1-9]|1[0-9]|2[0-9]|3[0-1]|[1-9])([-+][0-1]\d:[0-6]\d|Z*)/';
30
        preg_match_all($monthDayTimezone, $monthDay, $matches, PREG_SET_ORDER, 0);
31
        if (count($matches) != 1 || count($matches[0]) != 4) {
32
            throw new \InvalidArgumentException('Unable to extract month day from input string');
33
        }
34
        return new self(null, $matches[0][1], $matches[0][2], $matches[0][3]);
35
    }
36
37
    /**
38
     * @param string $month
39
     *
40
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
41
     */
42 View Code Duplication
    public static function fromMonth($month)
43
    {
44
        $monthTimezone = '/--((?:1[0-2])|(?:0[0-9]))([-+][0-1]\d:[0-6]\d|Z*)/';
45
        preg_match_all($monthTimezone, $month, $matches, PREG_SET_ORDER, 0);
46
        if (count($matches) != 1 || count($matches[0]) != 3) {
47
            throw new \InvalidArgumentException('Unable to extract month from input string');
48
        }
49
        return new self(null, $matches[0][1], null, $matches[0][2]);
50
    }
51
52
    /**
53
     * @param string $yearMonth
54
     *
55
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
56
     */
57 View Code Duplication
    public static function fromYearMonth($yearMonth)
58
    {
59
        $yearMonthTimezone = '/(\d{4})-((?:1[0-2])|(?:0[0-9]))([-+][0-1]\d:[0-6]\d|Z*)/';
60
        preg_match_all($yearMonthTimezone, $yearMonth, $matches, PREG_SET_ORDER, 0);
61
        if (count($matches) != 1 || count($matches[0]) != 4) {
62
            throw new \InvalidArgumentException('Unable to extract year month from input string');
63
        }
64
        return new self($matches[0][1], $matches[0][2], null, $matches[0][3]);
65
    }
66
67
    /**
68
     * @param string $year
69
     *
70
     * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender
71
     */
72 View Code Duplication
    public static function fromYear($year)
73
    {
74
        $yearTimezone = '/(\d{4})([-+][0-1]\d:[0-6]\d|Z*)/';
75
        preg_match_all($yearTimezone, $year, $matches, PREG_SET_ORDER, 0);
76
        if (count($matches) != 1 || count($matches[0]) != 3) {
77
            throw new \InvalidArgumentException('Unable to extract month from input string');
78
        }
79
        return new self($matches[0][1], null, null, $matches[0][2]);
80
    }
81
}
82