| Conditions | 9 |
| Paths | 14 |
| Total Lines | 65 |
| Code Lines | 40 |
| 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 |
||
| 104 | private function parseFilter($filter, $option) { |
||
| 105 | preg_match_all("/(?<=%).*(?=%)/", $filter, $matches); |
||
| 106 | if (count($matches[0]) > 0) { |
||
| 107 | $filter = $matches[0][0]; |
||
| 108 | preg_match('/(last|next|current|to|yester)?/', $filter, $directionMatch); |
||
| 109 | preg_match('/[0-9]+/', $filter, $offsetMatch); |
||
| 110 | preg_match('/(day|days|week|weeks|month|months|year|years)$/', $filter, $unitMatch); |
||
| 111 | |||
| 112 | if (!$directionMatch[0] || !$unitMatch[0]) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | !$offsetMatch[0] ? $offset = 1: $offset = $offsetMatch[0]; |
||
| 117 | |||
| 118 | // remove s to unify e.g. weeks => week |
||
| 119 | $unit = rtrim($unitMatch[0], 's'); |
||
| 120 | |||
| 121 | if ($directionMatch[0] === "last" || $directionMatch[0] === "yester") { |
||
| 122 | $direction = '-'; |
||
| 123 | //$directionWord = $directionMatch[0]; |
||
| 124 | } elseif ($directionMatch[0] === "next") { |
||
| 125 | $direction = '+'; |
||
| 126 | //$directionWord = $directionMatch[0]; |
||
| 127 | } else { // current |
||
| 128 | $direction = '+'; |
||
| 129 | $offset = 0; |
||
| 130 | //$directionWord = 'this'; |
||
| 131 | } |
||
| 132 | |||
| 133 | $timestring = $direction . $offset . ' ' . $unit; |
||
| 134 | $baseDate = strtotime($timestring); |
||
| 135 | |||
| 136 | if ($unit === 'day') { |
||
| 137 | $startString = 'today'; |
||
| 138 | //$endString = 'yesterday'; |
||
| 139 | } else { |
||
| 140 | $startString = 'first day of this ' . $unit; |
||
| 141 | //$endString = 'last day of ' . $directionWord . ' ' . $unit; |
||
| 142 | } |
||
| 143 | $startTS = strtotime($startString, $baseDate); |
||
| 144 | $start = date("Y-m-d", $startTS); |
||
| 145 | //$endTS = strtotime($endString); |
||
| 146 | //$end = date("Y-m-d", $endTS); |
||
| 147 | |||
| 148 | $return = [ |
||
| 149 | 'value' => $startTS, |
||
| 150 | 'option' => 'GT', |
||
| 151 | '1$filter' => $filter, |
||
| 152 | '2$timestring' => $timestring, |
||
| 153 | '3$target' => $baseDate, |
||
| 154 | '4$target_clean' => date("Y-m-d", $baseDate), |
||
| 155 | '5$startString' => $startString, |
||
| 156 | '6$startDate' => $start, |
||
| 157 | '7$startTS' => $startTS, |
||
| 158 | //'8$endString' => $endString, |
||
| 159 | //'9$endDate' => $end, |
||
| 160 | //'9$endTS' => $endTS, |
||
| 161 | ]; |
||
| 162 | } else { |
||
| 163 | $return = [ |
||
| 164 | 'value' => $filter, |
||
| 165 | 'option' => $option |
||
| 166 | ]; |
||
| 167 | } |
||
| 168 | return $return; |
||
| 169 | } |
||
| 185 | } |