Total Complexity | 48 |
Total Lines | 228 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | 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() |
||
31 | { |
||
32 | return static::date()->getTimestamp(); |
||
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]) |
||
43 | { |
||
44 | return static::isPreHoliday($date) || (!static::isHoliday($date) && !static::isWeekend($date, $weekend)); |
||
45 | } |
||
46 | |||
47 | protected static function findDateInArray($date, $array) |
||
48 | { |
||
49 | $date = static::prepareDate($date); |
||
50 | return in_array($date->format('Y-m-d'), $array); |
||
51 | |||
52 | } |
||
53 | |||
54 | public static function isPreHoliday($date) |
||
55 | { |
||
56 | return static::findDateInArray($date, static::getPreHolidaysByYear($date)); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param $date |
||
61 | * @return bool |
||
62 | */ |
||
63 | public static function isHoliday($date = null) |
||
64 | { |
||
65 | return (static::isWeekend($date) && !static::isPreHoliday($date)) || static::findDateInArray($date, static::getHolidaysByYear($date)); |
||
66 | } |
||
67 | |||
68 | public static function isWeekend($date, $weekend = [6, 0]) |
||
69 | { |
||
70 | $date = static::prepareDate($date); |
||
71 | return in_array($date->format('w'), $weekend); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param integer|string|\DateTime $year |
||
76 | * @return array |
||
77 | */ |
||
78 | public static function getHolidaysByYear($year) |
||
79 | { |
||
80 | if (!is_numeric($year)) { |
||
81 | $year = static::prepareDate($year)->format('Y'); |
||
82 | } |
||
83 | $holidays = static::getHolidays(); |
||
84 | return isset($holidays[$year]['holidays']) ? $holidays[$year]['holidays'] : []; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param integer|string|\DateTime $year |
||
89 | * @return array |
||
90 | */ |
||
91 | public static function getWorkingsByYear($year) |
||
92 | { |
||
93 | if (!is_numeric($year)) { |
||
94 | $year = static::prepareDate($year)->format('Y'); |
||
95 | } |
||
96 | $holidays = static::getHolidays(); |
||
97 | return isset($holidays[$year]['workings']) ? $holidays[$year]['workings'] : []; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param integer|string|\DateTime $year |
||
102 | * @return array |
||
103 | */ |
||
104 | public static function getPreHolidaysByYear($year) |
||
105 | { |
||
106 | if (!is_numeric($year)) { |
||
107 | $year = static::prepareDate($year)->format('Y'); |
||
108 | } |
||
109 | $holidays = static::getHolidays(); |
||
110 | return isset($holidays[$year]['preholidays']) ? $holidays[$year]['preholidays'] : []; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function day() |
||
117 | { |
||
118 | return $this; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param null|string $format |
||
123 | * @return string |
||
124 | */ |
||
125 | public function format($format = null) |
||
126 | { |
||
127 | return $this->date()->format($format ? $format : $this->format); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function working() |
||
134 | { |
||
135 | while (!static::isWorking(static::$date)) { |
||
136 | $this->next(); |
||
137 | } |
||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function holiday() |
||
150 | } |
||
151 | |||
152 | protected static function haveData($date = null) |
||
153 | { |
||
154 | $date = $date ? static::prepareDate($date) : static::date(); |
||
155 | return isset(static::getHolidays()[$date->format('Y')]); |
||
156 | |||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function preHoliday() |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function next() |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function prev() |
||
183 | { |
||
184 | static::$date->sub(new \DateInterval('P1D')); |
||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | public static function getInstance() |
||
189 | { |
||
190 | return static::$_instance ?: static::find(); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param null|string|\DateTime $date |
||
195 | * @return Calendar |
||
196 | */ |
||
197 | public static function find($date = null) |
||
198 | { |
||
199 | static::$date = static::prepareDate($date); |
||
200 | if (static::$_instance) { |
||
201 | return static::$_instance; |
||
202 | } else { |
||
203 | $json = file_get_contents(__DIR__ . '/holidays.json'); |
||
204 | static::$_instance = new self(); |
||
205 | static::$holidays = json_decode($json, true); |
||
206 | return static::$_instance; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param string|\DateTime $date |
||
212 | * @return \DateTime |
||
213 | */ |
||
214 | protected static function prepareDate($date) |
||
215 | { |
||
216 | if (is_null($date) && static::$date) { |
||
217 | $date = static::$date; |
||
218 | } elseif (!$date instanceof \DateTime) { |
||
219 | $date = new \DateTime($date); |
||
220 | } |
||
221 | return $date; |
||
222 | } |
||
223 | |||
224 | public static function getHolidays() |
||
225 | { |
||
226 | if (!static::$_instance) { |
||
227 | static::find(); |
||
228 | } |
||
229 | return static::$holidays; |
||
230 | } |
||
231 | |||
232 | private function __construct() |
||
234 | } |
||
235 | |||
236 | protected function __clone() |
||
237 | { |
||
238 | } |
||
239 | } |