| Total Complexity | 49 |
| Total Lines | 232 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 0 |
Complex classes like Calendar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Calendar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Calendar |
||
| 11 | { |
||
| 12 | public $format = 'Y-m-d'; |
||
| 13 | private static $_instance; |
||
| 14 | protected static $holidays; |
||
| 15 | /** |
||
| 16 | * @var \DateTime |
||
| 17 | */ |
||
| 18 | private static $date; |
||
| 19 | |||
| 20 | public function __toString() |
||
| 21 | { |
||
| 22 | return $this->date()->format($this->format); |
||
| 23 | } |
||
| 24 | |||
| 25 | public static function date() |
||
| 26 | { |
||
| 27 | return static::$date; |
||
|
|
|||
| 28 | } |
||
| 29 | |||
| 30 | public function timestamp() |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param \DateTime|string $date |
||
| 37 | * |
||
| 38 | * @param array $weekend |
||
| 39 | * |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | public static function isWorking($date, $weekend = [6, 0]) |
||
| 49 | } |
||
| 50 | |||
| 51 | protected static function findDateInArray($date, $array) |
||
| 52 | { |
||
| 53 | $date = static::prepareDate($date); |
||
| 54 | return in_array($date->format('Y-m-d'), $array); |
||
| 55 | |||
| 56 | } |
||
| 57 | |||
| 58 | public static function isPreHoliday($date) |
||
| 59 | { |
||
| 60 | return static::findDateInArray($date, static::getPreHolidaysByYear($date)); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param $date |
||
| 65 | * @return bool |
||
| 66 | */ |
||
| 67 | public static function isHoliday($date = null) |
||
| 68 | { |
||
| 69 | return (static::isWeekend($date) && !static::isPreHoliday($date)) || static::findDateInArray($date, static::getHolidaysByYear($date)); |
||
| 70 | } |
||
| 71 | |||
| 72 | public static function isWeekend($date, $weekend = [6, 0]) |
||
| 73 | { |
||
| 74 | $date = static::prepareDate($date); |
||
| 75 | return in_array($date->format('w'), $weekend); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param integer|string|\DateTime $year |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | public static function getHolidaysByYear($year) |
||
| 83 | { |
||
| 84 | if (!is_numeric($year)) { |
||
| 85 | $year = static::prepareDate($year)->format('Y'); |
||
| 86 | } |
||
| 87 | $holidays = static::getHolidays(); |
||
| 88 | return isset($holidays[$year]['holidays']) ? $holidays[$year]['holidays'] : []; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param integer|string|\DateTime $year |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public static function getWorkingsByYear($year) |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param integer|string|\DateTime $year |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public static function getPreHolidaysByYear($year) |
||
| 109 | { |
||
| 110 | if (!is_numeric($year)) { |
||
| 111 | $year = static::prepareDate($year)->format('Y'); |
||
| 112 | } |
||
| 113 | $holidays = static::getHolidays(); |
||
| 114 | return isset($holidays[$year]['preholidays']) ? $holidays[$year]['preholidays'] : []; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function day() |
||
| 121 | { |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param null|string $format |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function format($format = null) |
||
| 130 | { |
||
| 131 | return $this->date()->format($format ? $format : $this->format); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function working() |
||
| 138 | { |
||
| 139 | while (!static::isWorking(static::$date)) { |
||
| 140 | $this->next(); |
||
| 141 | } |
||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function holiday() |
||
| 149 | { |
||
| 150 | while (!static::isHoliday(static::$date) && static::haveData()) { |
||
| 151 | $this->next(); |
||
| 152 | } |
||
| 153 | return $this; |
||
| 154 | } |
||
| 155 | |||
| 156 | protected static function haveData($date = null) |
||
| 157 | { |
||
| 158 | $date = $date ? static::prepareDate($date) : static::date(); |
||
| 159 | return isset(static::getHolidays()[$date->format('Y')]); |
||
| 160 | |||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return $this |
||
| 165 | */ |
||
| 166 | public function preHoliday() |
||
| 167 | { |
||
| 168 | while (!static::isPreHoliday(static::$date) && static::haveData($this->date())) { |
||
| 169 | $this->next(); |
||
| 170 | } |
||
| 171 | return $this; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function next() |
||
| 178 | { |
||
| 179 | static::$date->add(new \DateInterval('P1D')); |
||
| 180 | return $this; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function prev() |
||
| 187 | { |
||
| 188 | static::$date->sub(new \DateInterval('P1D')); |
||
| 189 | return $this; |
||
| 190 | } |
||
| 191 | |||
| 192 | public static function getInstance() |
||
| 193 | { |
||
| 194 | return static::$_instance ?: static::find(); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param null|string|\DateTime $date |
||
| 199 | * @return Calendar |
||
| 200 | */ |
||
| 201 | public static function find($date = null) |
||
| 202 | { |
||
| 203 | static::$date = static::prepareDate($date); |
||
| 204 | if (static::$_instance) { |
||
| 205 | return static::$_instance; |
||
| 206 | } else { |
||
| 207 | $json = file_get_contents(__DIR__ . '/holidays.json'); |
||
| 208 | static::$_instance = new self(); |
||
| 209 | static::$holidays = json_decode($json, true); |
||
| 210 | return static::$_instance; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string|\DateTime $date |
||
| 216 | * @return \DateTime |
||
| 217 | */ |
||
| 218 | protected static function prepareDate($date) |
||
| 219 | { |
||
| 220 | if (is_null($date) && static::$date) { |
||
| 221 | $date = static::$date; |
||
| 222 | } elseif (!$date instanceof \DateTime) { |
||
| 223 | $date = new \DateTime($date); |
||
| 224 | } |
||
| 225 | return $date; |
||
| 226 | } |
||
| 227 | |||
| 228 | public static function getHolidays() |
||
| 229 | { |
||
| 230 | if (!static::$_instance) { |
||
| 231 | static::find(); |
||
| 232 | } |
||
| 233 | return static::$holidays; |
||
| 234 | } |
||
| 235 | |||
| 236 | private function __construct() |
||
| 238 | } |
||
| 239 | |||
| 240 | protected function __clone() |
||
| 241 | { |
||
| 242 | } |
||
| 243 | } |