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 |
||
| 10 | class Calender |
||
|
|
|||
| 11 | { |
||
| 12 | const timezoneRegexPattern = '([-+][0-1]\d:[0-6]\d|Z*)'; |
||
| 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) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param int $year |null |
||
| 38 | * @param int $month |null |
||
| 39 | * @param int $day |null |
||
| 40 | * |
||
| 41 | * @return void |
||
| 42 | */ |
||
| 43 | private function validateInput($year = null, $month = null, $day = null) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | private function valdidateState() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return int |
||
| 68 | */ |
||
| 69 | private function getMaxDays() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return int |
||
| 79 | */ |
||
| 80 | private function getYearOrHolder() |
||
| 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
|
|||
| 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
|
|||
| 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) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $yearMonth |
||
| 135 | * |
||
| 136 | * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
||
| 137 | */ |
||
| 138 | View Code Duplication | public static function fromYearMonth($yearMonth) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $year |
||
| 150 | * |
||
| 151 | * @return \AlgoWeb\xsdTypes\AxillaryClasses\Calender |
||
| 152 | */ |
||
| 153 | View Code Duplication | public static function fromYear($year) |
|
| 162 | } |
||
| 163 |