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