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