Total Complexity | 51 |
Total Lines | 387 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Week 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 Week, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
67 | class Week extends Calendar |
||
68 | { |
||
69 | /** |
||
70 | * Instance of Calendar_Table_Helper |
||
|
|||
71 | * @var Calendar_Table_Helper |
||
72 | * @access private |
||
73 | */ |
||
74 | var $tableHelper; |
||
75 | |||
76 | /** |
||
77 | * Stores the timestamp of the first day of this week |
||
78 | * @access private |
||
79 | * @var object |
||
80 | */ |
||
81 | var $thisWeek; |
||
82 | |||
83 | /** |
||
84 | * Stores the timestamp of first day of previous week |
||
85 | * @access private |
||
86 | * @var object |
||
87 | */ |
||
88 | var $prevWeek; |
||
89 | |||
90 | /** |
||
91 | * Stores the timestamp of first day of next week |
||
92 | * @access private |
||
93 | * @var object |
||
94 | */ |
||
95 | var $nextWeek; |
||
96 | |||
97 | /** |
||
98 | * Used by build() to set empty days |
||
99 | * @access private |
||
100 | * @var boolean |
||
101 | */ |
||
102 | var $firstWeek = false; |
||
103 | |||
104 | /** |
||
105 | * Used by build() to set empty days |
||
106 | * @access private |
||
107 | * @var boolean |
||
108 | */ |
||
109 | var $lastWeek = false; |
||
110 | |||
111 | /** |
||
112 | * First day of the week (0=sunday, 1=monday...) |
||
113 | * @access private |
||
114 | * @var boolean |
||
115 | */ |
||
116 | var $firstDay = 1; |
||
117 | |||
118 | /** |
||
119 | * Constructs Week |
||
120 | * |
||
121 | * @param int $y year e.g. 2003 |
||
122 | * @param int $m month e.g. 5 |
||
123 | * @param int $d a day of the desired week |
||
124 | * @param int $firstDay (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.) |
||
125 | * |
||
126 | * @access public |
||
127 | */ |
||
128 | function __construct($y, $m, $d, $firstDay = null) |
||
129 | { |
||
130 | parent::__construct($y, $m, $d); |
||
131 | $this->firstDay = $this->defineFirstDayOfWeek($firstDay); |
||
132 | $this->tableHelper = new Helper($this, $this->firstDay); |
||
133 | $this->thisWeek = $this->tableHelper->getWeekStart($y, $m, $d, $this->firstDay); |
||
134 | $this->prevWeek = $this->tableHelper->getWeekStart( |
||
135 | $y, |
||
136 | $m, |
||
137 | $d - $this->cE->getDaysInWeek( |
||
138 | $this->thisYear(), |
||
139 | $this->thisMonth(), |
||
140 | $this->thisDay() |
||
141 | ), |
||
142 | $this->firstDay |
||
143 | ); |
||
144 | $this->nextWeek = $this->tableHelper->getWeekStart( |
||
145 | $y, |
||
146 | $m, |
||
147 | $d + $this->cE->getDaysInWeek( |
||
148 | $this->thisYear(), |
||
149 | $this->thisMonth(), |
||
150 | $this->thisDay() |
||
151 | ), |
||
152 | $this->firstDay |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values |
||
158 | * passed to the constructor |
||
159 | * |
||
160 | * @param int|string $ts Unix or ISO-8601 timestamp |
||
161 | * |
||
162 | * @return void |
||
163 | * @access public |
||
164 | */ |
||
165 | function setTimestamp($ts) |
||
166 | { |
||
167 | parent::setTimestamp($ts); |
||
168 | $this->thisWeek = $this->tableHelper->getWeekStart( |
||
169 | $this->year, $this->month, $this->day, $this->firstDay |
||
170 | ); |
||
171 | $this->prevWeek = $this->tableHelper->getWeekStart( |
||
172 | $this->year, |
||
173 | $this->month, |
||
174 | $this->day - $this->cE->getDaysInWeek( |
||
175 | $this->thisYear(), |
||
176 | $this->thisMonth(), |
||
177 | $this->thisDay() |
||
178 | ), |
||
179 | $this->firstDay |
||
180 | ); |
||
181 | $this->nextWeek = $this->tableHelper->getWeekStart( |
||
182 | $this->year, |
||
183 | $this->month, |
||
184 | $this->day + $this->cE->getDaysInWeek( |
||
185 | $this->thisYear(), |
||
186 | $this->thisMonth(), |
||
187 | $this->thisDay() |
||
188 | ), |
||
189 | $this->firstDay |
||
190 | ); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Builds Calendar_Day objects for this Week |
||
195 | * |
||
196 | * @param array $sDates (optional) Calendar_Day objects representing selected dates |
||
197 | * |
||
198 | * @return boolean |
||
199 | * @access public |
||
200 | */ |
||
201 | function build($sDates = array()) |
||
202 | { |
||
203 | include_once CALENDAR_ROOT.'Day.php'; |
||
204 | $year = $this->cE->stampToYear($this->thisWeek); |
||
205 | $month = $this->cE->stampToMonth($this->thisWeek); |
||
206 | $day = $this->cE->stampToDay($this->thisWeek); |
||
207 | $end = $this->cE->getDaysInWeek( |
||
208 | $this->thisYear(), |
||
209 | $this->thisMonth(), |
||
210 | $this->thisDay() |
||
211 | ); |
||
212 | |||
213 | for ($i=1; $i <= $end; $i++) { |
||
214 | $stamp = $this->cE->dateToStamp($year, $month, $day++); |
||
215 | $this->children[$i] = new Day( |
||
216 | $this->cE->stampToYear($stamp), |
||
217 | $this->cE->stampToMonth($stamp), |
||
218 | $this->cE->stampToDay($stamp) |
||
219 | ); |
||
220 | } |
||
221 | |||
222 | //set empty days (@see Calendar_Month_Weeks::build()) |
||
223 | if ($this->firstWeek) { |
||
224 | $eBefore = $this->tableHelper->getEmptyDaysBefore(); |
||
225 | for ($i=1; $i <= $eBefore; $i++) { |
||
226 | $this->children[$i]->setEmpty(); |
||
227 | } |
||
228 | } |
||
229 | if ($this->lastWeek) { |
||
230 | $eAfter = $this->tableHelper->getEmptyDaysAfterOffset(); |
||
231 | for ($i = $eAfter+1; $i <= $end; $i++) { |
||
232 | $this->children[$i]->setEmpty(); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | if (count($sDates) > 0) { |
||
237 | $this->setSelection($sDates); |
||
238 | } |
||
239 | return true; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Set as first week of the month |
||
244 | * |
||
245 | * @param boolean $state whether it's first or not |
||
246 | * |
||
247 | * @return void |
||
248 | * @access private |
||
249 | */ |
||
250 | function setFirst($state = true) |
||
251 | { |
||
252 | $this->firstWeek = $state; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Set as last week of the month |
||
257 | * |
||
258 | * @param boolean $state whether it's lasst or not |
||
259 | * |
||
260 | * @return void |
||
261 | * @access private |
||
262 | */ |
||
263 | function setLast($state = true) |
||
264 | { |
||
265 | $this->lastWeek = $state; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Called from build() |
||
270 | * |
||
271 | * @param array $sDates Calendar_Day objects representing selected dates |
||
272 | * |
||
273 | * @return void |
||
274 | * @access private |
||
275 | */ |
||
276 | function setSelection($sDates) |
||
277 | { |
||
278 | foreach ($sDates as $sDate) { |
||
279 | foreach ($this->children as $key => $child) { |
||
280 | if ($child->thisDay() == $sDate->thisDay() && |
||
281 | $child->thisMonth() == $sDate->thisMonth() && |
||
282 | $child->thisYear() == $sDate->thisYear() |
||
283 | ) { |
||
284 | $this->children[$key] = $sDate; |
||
285 | $this->children[$key]->setSelected(); |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | reset($this->children); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Returns the value for this year |
||
294 | * |
||
295 | * When a on the first/last week of the year, the year of the week is |
||
296 | * calculated according to ISO-8601 |
||
297 | * |
||
298 | * @param string $format return value format ['int' | 'timestamp' | 'object' | 'array'] |
||
299 | * |
||
300 | * @return int e.g. 2003 or timestamp |
||
301 | * @access public |
||
302 | */ |
||
303 | function thisYear($format = 'int') |
||
304 | { |
||
305 | if (null !== $this->thisWeek) { |
||
306 | $tmp_cal = new Calendar(); |
||
307 | $tmp_cal->setTimestamp($this->thisWeek); |
||
308 | $first_dow = $tmp_cal->thisDay('array'); |
||
309 | $days_in_week = $tmp_cal->cE->getDaysInWeek($tmp_cal->year, $tmp_cal->month, $tmp_cal->day); |
||
310 | $tmp_cal->day += $days_in_week; |
||
311 | $last_dow = $tmp_cal->thisDay('array'); |
||
312 | |||
313 | if ($first_dow['year'] == $last_dow['year']) { |
||
314 | return $first_dow['year']; |
||
315 | } |
||
316 | |||
317 | if ($last_dow['day'] > floor($days_in_week / 2)) { |
||
318 | return $last_dow['year']; |
||
319 | } |
||
320 | return $first_dow['year']; |
||
321 | } |
||
322 | return parent::thisYear(); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Gets the value of the previous week, according to the requested format |
||
327 | * |
||
328 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
329 | * |
||
330 | * @return mixed |
||
331 | * @access public |
||
332 | */ |
||
333 | function prevWeek($format = 'n_in_month') |
||
334 | { |
||
335 | switch (strtolower($format)) { |
||
336 | case 'int': |
||
337 | case 'n_in_month': |
||
338 | return ($this->firstWeek) ? null : $this->thisWeek('n_in_month') -1; |
||
339 | case 'n_in_year': |
||
340 | return $this->cE->getWeekNInYear( |
||
341 | $this->cE->stampToYear($this->prevWeek), |
||
342 | $this->cE->stampToMonth($this->prevWeek), |
||
343 | $this->cE->stampToDay($this->prevWeek)); |
||
344 | case 'array': |
||
345 | return $this->toArray($this->prevWeek); |
||
346 | case 'object': |
||
347 | include_once CALENDAR_ROOT.'Factory.php'; |
||
348 | return Factory::createByTimestamp('Week', $this->prevWeek); |
||
349 | case 'timestamp': |
||
350 | default: |
||
351 | return $this->prevWeek; |
||
352 | } |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Gets the value of the current week, according to the requested format |
||
357 | * |
||
358 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
359 | * |
||
360 | * @return mixed |
||
361 | * @access public |
||
362 | */ |
||
363 | function thisWeek($format = 'n_in_month') |
||
364 | { |
||
365 | switch (strtolower($format)) { |
||
366 | case 'int': |
||
367 | case 'n_in_month': |
||
368 | if ($this->firstWeek) { |
||
369 | return 1; |
||
370 | } |
||
371 | if ($this->lastWeek) { |
||
372 | return $this->cE->getWeeksInMonth( |
||
373 | $this->thisYear(), |
||
374 | $this->thisMonth(), |
||
375 | $this->firstDay); |
||
376 | } |
||
377 | return $this->cE->getWeekNInMonth( |
||
378 | $this->thisYear(), |
||
379 | $this->thisMonth(), |
||
380 | $this->thisDay(), |
||
381 | $this->firstDay); |
||
382 | case 'n_in_year': |
||
383 | return $this->cE->getWeekNInYear( |
||
384 | $this->cE->stampToYear($this->thisWeek), |
||
385 | $this->cE->stampToMonth($this->thisWeek), |
||
386 | $this->cE->stampToDay($this->thisWeek)); |
||
387 | case 'array': |
||
388 | return $this->toArray($this->thisWeek); |
||
389 | case 'object': |
||
390 | include_once CALENDAR_ROOT.'Factory.php'; |
||
391 | return Factory::createByTimestamp('Week', $this->thisWeek); |
||
392 | case 'timestamp': |
||
393 | default: |
||
394 | return $this->thisWeek; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Gets the value of the following week, according to the requested format |
||
400 | * |
||
401 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
402 | * |
||
403 | * @return mixed |
||
404 | * @access public |
||
405 | */ |
||
406 | function nextWeek($format = 'n_in_month') |
||
425 | } |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Returns the instance of Calendar_Table_Helper. |
||
430 | * Called from Calendar_Validator::isValidWeek |
||
431 | * |
||
432 | * @return Calendar_Table_Helper |
||
433 | * @access protected |
||
434 | */ |
||
435 | function & getHelper() |
||
436 | { |
||
437 | return $this->tableHelper; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Makes sure theres a value for $this->day |
||
442 | * |
||
443 | * @return void |
||
444 | * @access private |
||
445 | */ |
||
446 | function findFirstDay() |
||
454 | } |
||
455 | } |
||
456 | } |
||
459 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths