Conditions | 16 |
Paths | 18 |
Total Lines | 76 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
23 | public function isSatisfiedBy(DateTime $date, $value) |
||
24 | { |
||
25 | if ($value == '?') { |
||
26 | return true; |
||
27 | } |
||
28 | |||
29 | // Convert text day of the week values to integers |
||
30 | $value = str_ireplace( |
||
31 | array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'), |
||
32 | range(0, 6), |
||
33 | $value |
||
34 | ); |
||
35 | |||
36 | $currentYear = $date->format('Y'); |
||
37 | $currentMonth = $date->format('m'); |
||
38 | $lastDayOfMonth = $date->format('t'); |
||
39 | |||
40 | // Find out if this is the last specific weekday of the month |
||
41 | if (strpos($value, 'L')) { |
||
42 | $weekday = str_replace('7', '0', substr($value, 0, strpos($value, 'L'))); |
||
43 | $tdate = clone $date; |
||
44 | $tdate->setDate($currentYear, $currentMonth, $lastDayOfMonth); |
||
45 | while ($tdate->format('w') != $weekday) { |
||
46 | $tdate->setDate($currentYear, $currentMonth, --$lastDayOfMonth); |
||
47 | } |
||
48 | |||
49 | return $date->format('j') == $lastDayOfMonth; |
||
50 | } |
||
51 | |||
52 | // Handle # hash tokens |
||
53 | if (strpos($value, '#')) { |
||
54 | list($weekday, $nth) = explode('#', $value); |
||
55 | // Validate the hash fields |
||
56 | if ($weekday < 1 || $weekday > 5) { |
||
57 | throw new InvalidArgumentException("Weekday must be a value between 1 and 5. {$weekday} given"); |
||
58 | } |
||
59 | if ($nth > 5) { |
||
60 | throw new InvalidArgumentException('There are never more than 5 of a given weekday in a month'); |
||
61 | } |
||
62 | // The current weekday must match the targeted weekday to proceed |
||
63 | if ($date->format('N') != $weekday) { |
||
64 | return false; |
||
65 | } |
||
66 | |||
67 | $tdate = clone $date; |
||
68 | $tdate->setDate($currentYear, $currentMonth, 1); |
||
69 | $dayCount = 0; |
||
70 | $currentDay = 1; |
||
71 | while ($currentDay < $lastDayOfMonth + 1) { |
||
72 | if ($tdate->format('N') == $weekday) { |
||
73 | if (++$dayCount >= $nth) { |
||
74 | break; |
||
75 | } |
||
76 | } |
||
77 | $tdate->setDate($currentYear, $currentMonth, ++$currentDay); |
||
78 | } |
||
79 | |||
80 | return $date->format('j') == $currentDay; |
||
81 | } |
||
82 | |||
83 | // Handle day of the week values |
||
84 | if (strpos($value, '-')) { |
||
85 | $parts = explode('-', $value); |
||
86 | if ($parts[0] == '7') { |
||
87 | $parts[0] = '0'; |
||
88 | } elseif ($parts[1] == '0') { |
||
89 | $parts[1] = '7'; |
||
90 | } |
||
91 | $value = implode('-', $parts); |
||
92 | } |
||
93 | |||
94 | // Test to see which Sunday to use -- 0 == 7 == Sunday |
||
95 | $format = in_array(7, str_split($value)) ? 'N' : 'w'; |
||
96 | $fieldValue = $date->format($format); |
||
97 | |||
98 | return $this->isSatisfied($fieldValue, $value); |
||
99 | } |
||
125 |