Total Complexity | 51 |
Total Lines | 383 |
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 |
||
66 | class Week extends Calendar |
||
67 | { |
||
68 | /** |
||
69 | * Instance of Calendar_Table_Helper |
||
70 | * @var \PEAR\Calendar\Table\Helper |
||
71 | * @access private |
||
72 | */ |
||
73 | var $tableHelper; |
||
74 | |||
75 | /** |
||
76 | * Stores the timestamp of the first day of this week |
||
77 | * @access private |
||
78 | * @var object |
||
79 | */ |
||
80 | var $thisWeek; |
||
81 | |||
82 | /** |
||
83 | * Stores the timestamp of first day of previous week |
||
84 | * @access private |
||
85 | * @var object |
||
86 | */ |
||
87 | var $prevWeek; |
||
88 | |||
89 | /** |
||
90 | * Stores the timestamp of first day of next week |
||
91 | * @access private |
||
92 | * @var object |
||
93 | */ |
||
94 | var $nextWeek; |
||
95 | |||
96 | /** |
||
97 | * Used by build() to set empty days |
||
98 | * @access private |
||
99 | * @var boolean |
||
100 | */ |
||
101 | var $firstWeek = false; |
||
102 | |||
103 | /** |
||
104 | * Used by build() to set empty days |
||
105 | * @access private |
||
106 | * @var boolean |
||
107 | */ |
||
108 | var $lastWeek = false; |
||
109 | |||
110 | /** |
||
111 | * First day of the week (0=sunday, 1=monday...) |
||
112 | * @access private |
||
113 | * @var boolean |
||
114 | */ |
||
115 | var $firstDay = 1; |
||
116 | |||
117 | /** |
||
118 | * Constructs Week |
||
119 | * |
||
120 | * @param int $y year e.g. 2003 |
||
121 | * @param int $m month e.g. 5 |
||
122 | * @param int $d a day of the desired week |
||
123 | * @param int $firstDay (optional) first day of week (e.g. 0 for Sunday, 2 for Tuesday etc.) |
||
124 | * |
||
125 | * @access public |
||
126 | */ |
||
127 | function __construct($y, $m, $d, $firstDay = null) |
||
|
|||
128 | { |
||
129 | parent::__construct($y, $m, $d); |
||
130 | $this->firstDay = $this->defineFirstDayOfWeek($firstDay); |
||
131 | $this->tableHelper = new Helper($this, $this->firstDay); |
||
132 | $this->thisWeek = $this->tableHelper->getWeekStart($y, $m, $d, $this->firstDay); |
||
133 | $this->prevWeek = $this->tableHelper->getWeekStart( |
||
134 | $y, |
||
135 | $m, |
||
136 | $d - $this->cE->getDaysInWeek( |
||
137 | $this->thisYear(), |
||
138 | $this->thisMonth(), |
||
139 | $this->thisDay() |
||
140 | ), |
||
141 | $this->firstDay |
||
142 | ); |
||
143 | $this->nextWeek = $this->tableHelper->getWeekStart( |
||
144 | $y, |
||
145 | $m, |
||
146 | $d + $this->cE->getDaysInWeek( |
||
147 | $this->thisYear(), |
||
148 | $this->thisMonth(), |
||
149 | $this->thisDay() |
||
150 | ), |
||
151 | $this->firstDay |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values |
||
157 | * passed to the constructor |
||
158 | * |
||
159 | * @param int|string $ts Unix or ISO-8601 timestamp |
||
160 | * |
||
161 | * @return void |
||
162 | * @access public |
||
163 | */ |
||
164 | function setTimestamp($ts) |
||
189 | ); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Builds Calendar_Day objects for this Week |
||
194 | * |
||
195 | * @param array $sDates (optional) Calendar_Day objects representing selected dates |
||
196 | * |
||
197 | * @return boolean |
||
198 | * @access public |
||
199 | */ |
||
200 | function build($sDates = array()) |
||
201 | { |
||
202 | $year = $this->cE->stampToYear($this->thisWeek); |
||
203 | $month = $this->cE->stampToMonth($this->thisWeek); |
||
204 | $day = $this->cE->stampToDay($this->thisWeek); |
||
205 | $end = $this->cE->getDaysInWeek( |
||
206 | $this->thisYear(), |
||
207 | $this->thisMonth(), |
||
208 | $this->thisDay() |
||
209 | ); |
||
210 | |||
211 | for ($i=1; $i <= $end; $i++) { |
||
212 | $stamp = $this->cE->dateToStamp($year, $month, $day++); |
||
213 | $this->children[$i] = new Day( |
||
214 | $this->cE->stampToYear($stamp), |
||
215 | $this->cE->stampToMonth($stamp), |
||
216 | $this->cE->stampToDay($stamp) |
||
217 | ); |
||
218 | } |
||
219 | |||
220 | //set empty days (@see Calendar_Month_Weeks::build()) |
||
221 | if ($this->firstWeek) { |
||
222 | $eBefore = $this->tableHelper->getEmptyDaysBefore(); |
||
223 | for ($i=1; $i <= $eBefore; $i++) { |
||
224 | $this->children[$i]->setEmpty(); |
||
225 | } |
||
226 | } |
||
227 | if ($this->lastWeek) { |
||
228 | $eAfter = $this->tableHelper->getEmptyDaysAfterOffset(); |
||
229 | for ($i = $eAfter+1; $i <= $end; $i++) { |
||
230 | $this->children[$i]->setEmpty(); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | if (count($sDates) > 0) { |
||
235 | $this->setSelection($sDates); |
||
236 | } |
||
237 | return true; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Set as first week of the month |
||
242 | * |
||
243 | * @param boolean $state whether it's first or not |
||
244 | * |
||
245 | * @return void |
||
246 | * @access private |
||
247 | */ |
||
248 | function setFirst($state = true) |
||
249 | { |
||
250 | $this->firstWeek = $state; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Set as last week of the month |
||
255 | * |
||
256 | * @param boolean $state whether it's lasst or not |
||
257 | * |
||
258 | * @return void |
||
259 | * @access private |
||
260 | */ |
||
261 | function setLast($state = true) |
||
262 | { |
||
263 | $this->lastWeek = $state; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Called from build() |
||
268 | * |
||
269 | * @param array $sDates Calendar_Day objects representing selected dates |
||
270 | * |
||
271 | * @return void |
||
272 | * @access private |
||
273 | */ |
||
274 | function setSelection($sDates) |
||
275 | { |
||
276 | foreach ($sDates as $sDate) { |
||
277 | foreach ($this->children as $key => $child) { |
||
278 | if ($child->thisDay() == $sDate->thisDay() && |
||
279 | $child->thisMonth() == $sDate->thisMonth() && |
||
280 | $child->thisYear() == $sDate->thisYear() |
||
281 | ) { |
||
282 | $this->children[$key] = $sDate; |
||
283 | $this->children[$key]->setSelected(); |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | reset($this->children); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Returns the value for this year |
||
292 | * |
||
293 | * When a on the first/last week of the year, the year of the week is |
||
294 | * calculated according to ISO-8601 |
||
295 | * |
||
296 | * @param string $format return value format ['int' | 'timestamp' | 'object' | 'array'] |
||
297 | * |
||
298 | * @return int e.g. 2003 or timestamp |
||
299 | * @access public |
||
300 | */ |
||
301 | function thisYear($format = 'int') |
||
302 | { |
||
303 | if (null !== $this->thisWeek) { |
||
304 | $tmp_cal = new Calendar(); |
||
305 | $tmp_cal->setTimestamp($this->thisWeek); |
||
306 | $first_dow = $tmp_cal->thisDay('array'); |
||
307 | $days_in_week = $tmp_cal->cE->getDaysInWeek($tmp_cal->year, $tmp_cal->month, $tmp_cal->day); |
||
308 | $tmp_cal->day += $days_in_week; |
||
309 | $last_dow = $tmp_cal->thisDay('array'); |
||
310 | |||
311 | if ($first_dow['year'] == $last_dow['year']) { |
||
312 | return $first_dow['year']; |
||
313 | } |
||
314 | |||
315 | if ($last_dow['day'] > floor($days_in_week / 2)) { |
||
316 | return $last_dow['year']; |
||
317 | } |
||
318 | return $first_dow['year']; |
||
319 | } |
||
320 | return parent::thisYear(); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Gets the value of the previous week, according to the requested format |
||
325 | * |
||
326 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
327 | * |
||
328 | * @return mixed |
||
329 | * @access public |
||
330 | */ |
||
331 | function prevWeek($format = 'n_in_month') |
||
332 | { |
||
333 | switch (strtolower($format)) { |
||
334 | case 'int': |
||
335 | case 'n_in_month': |
||
336 | return ($this->firstWeek) ? null : $this->thisWeek('n_in_month') -1; |
||
337 | case 'n_in_year': |
||
338 | return $this->cE->getWeekNInYear( |
||
339 | $this->cE->stampToYear($this->prevWeek), |
||
340 | $this->cE->stampToMonth($this->prevWeek), |
||
341 | $this->cE->stampToDay($this->prevWeek)); |
||
342 | case 'array': |
||
343 | return $this->toArray($this->prevWeek); |
||
344 | case 'object': |
||
345 | return Factory::createByTimestamp('Week', $this->prevWeek); |
||
346 | case 'timestamp': |
||
347 | default: |
||
348 | return $this->prevWeek; |
||
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Gets the value of the current week, according to the requested format |
||
354 | * |
||
355 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
356 | * |
||
357 | * @return mixed |
||
358 | * @access public |
||
359 | */ |
||
360 | function thisWeek($format = 'n_in_month') |
||
391 | } |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Gets the value of the following week, according to the requested format |
||
396 | * |
||
397 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
398 | * |
||
399 | * @return mixed |
||
400 | * @access public |
||
401 | */ |
||
402 | function nextWeek($format = 'n_in_month') |
||
403 | { |
||
404 | switch (strtolower($format)) { |
||
405 | case 'int': |
||
406 | case 'n_in_month': |
||
407 | return ($this->lastWeek) ? null : $this->thisWeek('n_in_month') +1; |
||
408 | case 'n_in_year': |
||
409 | return $this->cE->getWeekNInYear( |
||
410 | $this->cE->stampToYear($this->nextWeek), |
||
411 | $this->cE->stampToMonth($this->nextWeek), |
||
412 | $this->cE->stampToDay($this->nextWeek)); |
||
413 | case 'array': |
||
414 | return $this->toArray($this->nextWeek); |
||
415 | case 'object': |
||
416 | return Factory::createByTimestamp('Week', $this->nextWeek); |
||
417 | case 'timestamp': |
||
418 | default: |
||
419 | return $this->nextWeek; |
||
420 | } |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Returns the instance of Calendar_Table_Helper. |
||
425 | * Called from Calendar_Validator::isValidWeek |
||
426 | * |
||
427 | * @return \PEAR\Calendar\Table\Helper |
||
428 | * @access protected |
||
429 | */ |
||
430 | function & getHelper() |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Makes sure theres a value for $this->day |
||
437 | * |
||
438 | * @return void |
||
439 | * @access private |
||
440 | */ |
||
441 | function findFirstDay() |
||
449 | } |
||
450 | } |
||
451 | } |
||
452 | } |
||
454 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.