| 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 |
||
| 66 | class Week extends Calendar |
||
| 67 | { |
||
| 68 | /** |
||
| 69 | * Instance of Calendar_Table_Helper |
||
|
|
|||
| 70 | * @var 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) |
||
| 165 | { |
||
| 166 | parent::setTimestamp($ts); |
||
| 167 | $this->thisWeek = $this->tableHelper->getWeekStart( |
||
| 168 | $this->year, $this->month, $this->day, $this->firstDay |
||
| 169 | ); |
||
| 170 | $this->prevWeek = $this->tableHelper->getWeekStart( |
||
| 171 | $this->year, |
||
| 172 | $this->month, |
||
| 173 | $this->day - $this->cE->getDaysInWeek( |
||
| 174 | $this->thisYear(), |
||
| 175 | $this->thisMonth(), |
||
| 176 | $this->thisDay() |
||
| 177 | ), |
||
| 178 | $this->firstDay |
||
| 179 | ); |
||
| 180 | $this->nextWeek = $this->tableHelper->getWeekStart( |
||
| 181 | $this->year, |
||
| 182 | $this->month, |
||
| 183 | $this->day + $this->cE->getDaysInWeek( |
||
| 184 | $this->thisYear(), |
||
| 185 | $this->thisMonth(), |
||
| 186 | $this->thisDay() |
||
| 187 | ), |
||
| 188 | $this->firstDay |
||
| 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 | include_once CALENDAR_ROOT.'Day.php'; |
||
| 203 | $year = $this->cE->stampToYear($this->thisWeek); |
||
| 204 | $month = $this->cE->stampToMonth($this->thisWeek); |
||
| 205 | $day = $this->cE->stampToDay($this->thisWeek); |
||
| 206 | $end = $this->cE->getDaysInWeek( |
||
| 207 | $this->thisYear(), |
||
| 208 | $this->thisMonth(), |
||
| 209 | $this->thisDay() |
||
| 210 | ); |
||
| 211 | |||
| 212 | for ($i=1; $i <= $end; $i++) { |
||
| 213 | $stamp = $this->cE->dateToStamp($year, $month, $day++); |
||
| 214 | $this->children[$i] = new Day( |
||
| 215 | $this->cE->stampToYear($stamp), |
||
| 216 | $this->cE->stampToMonth($stamp), |
||
| 217 | $this->cE->stampToDay($stamp) |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | //set empty days (@see Calendar_Month_Weeks::build()) |
||
| 222 | if ($this->firstWeek) { |
||
| 223 | $eBefore = $this->tableHelper->getEmptyDaysBefore(); |
||
| 224 | for ($i=1; $i <= $eBefore; $i++) { |
||
| 225 | $this->children[$i]->setEmpty(); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | if ($this->lastWeek) { |
||
| 229 | $eAfter = $this->tableHelper->getEmptyDaysAfterOffset(); |
||
| 230 | for ($i = $eAfter+1; $i <= $end; $i++) { |
||
| 231 | $this->children[$i]->setEmpty(); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | if (count($sDates) > 0) { |
||
| 236 | $this->setSelection($sDates); |
||
| 237 | } |
||
| 238 | return true; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set as first week of the month |
||
| 243 | * |
||
| 244 | * @param boolean $state whether it's first or not |
||
| 245 | * |
||
| 246 | * @return void |
||
| 247 | * @access private |
||
| 248 | */ |
||
| 249 | function setFirst($state = true) |
||
| 250 | { |
||
| 251 | $this->firstWeek = $state; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set as last week of the month |
||
| 256 | * |
||
| 257 | * @param boolean $state whether it's lasst or not |
||
| 258 | * |
||
| 259 | * @return void |
||
| 260 | * @access private |
||
| 261 | */ |
||
| 262 | function setLast($state = true) |
||
| 263 | { |
||
| 264 | $this->lastWeek = $state; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Called from build() |
||
| 269 | * |
||
| 270 | * @param array $sDates Calendar_Day objects representing selected dates |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | * @access private |
||
| 274 | */ |
||
| 275 | function setSelection($sDates) |
||
| 276 | { |
||
| 277 | foreach ($sDates as $sDate) { |
||
| 278 | foreach ($this->children as $key => $child) { |
||
| 279 | if ($child->thisDay() == $sDate->thisDay() && |
||
| 280 | $child->thisMonth() == $sDate->thisMonth() && |
||
| 281 | $child->thisYear() == $sDate->thisYear() |
||
| 282 | ) { |
||
| 283 | $this->children[$key] = $sDate; |
||
| 284 | $this->children[$key]->setSelected(); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | reset($this->children); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns the value for this year |
||
| 293 | * |
||
| 294 | * When a on the first/last week of the year, the year of the week is |
||
| 295 | * calculated according to ISO-8601 |
||
| 296 | * |
||
| 297 | * @param string $format return value format ['int' | 'timestamp' | 'object' | 'array'] |
||
| 298 | * |
||
| 299 | * @return int e.g. 2003 or timestamp |
||
| 300 | * @access public |
||
| 301 | */ |
||
| 302 | function thisYear($format = 'int') |
||
| 303 | { |
||
| 304 | if (null !== $this->thisWeek) { |
||
| 305 | $tmp_cal = new Calendar(); |
||
| 306 | $tmp_cal->setTimestamp($this->thisWeek); |
||
| 307 | $first_dow = $tmp_cal->thisDay('array'); |
||
| 308 | $days_in_week = $tmp_cal->cE->getDaysInWeek($tmp_cal->year, $tmp_cal->month, $tmp_cal->day); |
||
| 309 | $tmp_cal->day += $days_in_week; |
||
| 310 | $last_dow = $tmp_cal->thisDay('array'); |
||
| 311 | |||
| 312 | if ($first_dow['year'] == $last_dow['year']) { |
||
| 313 | return $first_dow['year']; |
||
| 314 | } |
||
| 315 | |||
| 316 | if ($last_dow['day'] > floor($days_in_week / 2)) { |
||
| 317 | return $last_dow['year']; |
||
| 318 | } |
||
| 319 | return $first_dow['year']; |
||
| 320 | } |
||
| 321 | return parent::thisYear(); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Gets the value of the previous week, according to the requested format |
||
| 326 | * |
||
| 327 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
| 328 | * |
||
| 329 | * @return mixed |
||
| 330 | * @access public |
||
| 331 | */ |
||
| 332 | function prevWeek($format = 'n_in_month') |
||
| 333 | { |
||
| 334 | switch (strtolower($format)) { |
||
| 335 | case 'int': |
||
| 336 | case 'n_in_month': |
||
| 337 | return ($this->firstWeek) ? null : $this->thisWeek('n_in_month') -1; |
||
| 338 | case 'n_in_year': |
||
| 339 | return $this->cE->getWeekNInYear( |
||
| 340 | $this->cE->stampToYear($this->prevWeek), |
||
| 341 | $this->cE->stampToMonth($this->prevWeek), |
||
| 342 | $this->cE->stampToDay($this->prevWeek)); |
||
| 343 | case 'array': |
||
| 344 | return $this->toArray($this->prevWeek); |
||
| 345 | case 'object': |
||
| 346 | include_once CALENDAR_ROOT.'Factory.php'; |
||
| 347 | return Factory::createByTimestamp('Week', $this->prevWeek); |
||
| 348 | case 'timestamp': |
||
| 349 | default: |
||
| 350 | return $this->prevWeek; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Gets the value of the current week, according to the requested format |
||
| 356 | * |
||
| 357 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
| 358 | * |
||
| 359 | * @return mixed |
||
| 360 | * @access public |
||
| 361 | */ |
||
| 362 | function thisWeek($format = 'n_in_month') |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Gets the value of the following week, according to the requested format |
||
| 399 | * |
||
| 400 | * @param string $format ['timestamp' | 'n_in_month' | 'n_in_year' | 'array'] |
||
| 401 | * |
||
| 402 | * @return mixed |
||
| 403 | * @access public |
||
| 404 | */ |
||
| 405 | function nextWeek($format = 'n_in_month') |
||
| 406 | { |
||
| 407 | switch (strtolower($format)) { |
||
| 408 | case 'int': |
||
| 409 | case 'n_in_month': |
||
| 410 | return ($this->lastWeek) ? null : $this->thisWeek('n_in_month') +1; |
||
| 411 | case 'n_in_year': |
||
| 412 | return $this->cE->getWeekNInYear( |
||
| 413 | $this->cE->stampToYear($this->nextWeek), |
||
| 414 | $this->cE->stampToMonth($this->nextWeek), |
||
| 415 | $this->cE->stampToDay($this->nextWeek)); |
||
| 416 | case 'array': |
||
| 417 | return $this->toArray($this->nextWeek); |
||
| 418 | case 'object': |
||
| 419 | include_once CALENDAR_ROOT.'Factory.php'; |
||
| 420 | return Factory::createByTimestamp('Week', $this->nextWeek); |
||
| 421 | case 'timestamp': |
||
| 422 | default: |
||
| 423 | return $this->nextWeek; |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Returns the instance of Calendar_Table_Helper. |
||
| 429 | * Called from Calendar_Validator::isValidWeek |
||
| 430 | * |
||
| 431 | * @return Calendar_Table_Helper |
||
| 432 | * @access protected |
||
| 433 | */ |
||
| 434 | function & getHelper() |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Makes sure theres a value for $this->day |
||
| 441 | * |
||
| 442 | * @return void |
||
| 443 | * @access private |
||
| 444 | */ |
||
| 445 | function findFirstDay() |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } |
||
| 456 | } |
||
| 457 | } |
||
| 458 |
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