| Conditions | 9 |
| Paths | 14 |
| Total Lines | 60 |
| Code Lines | 38 |
| 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 |
||
| 153 | private function parseFilter($filter) { |
||
| 154 | preg_match_all("/(?<=%).*(?=%)/", $filter, $matches); |
||
| 155 | if (count($matches[0]) > 0) { |
||
| 156 | $filter = $matches[0][0]; |
||
| 157 | preg_match('/(last|next|current|to|yester)?/', $filter, $directionMatch); // direction |
||
| 158 | preg_match('/[0-9]+/', $filter, $offsetMatch); // how much |
||
| 159 | preg_match('/(day|days|week|weeks|month|months|year|years)$/', $filter, $unitMatch); // unit |
||
| 160 | |||
| 161 | if (!$directionMatch[0] || !$unitMatch[0]) { |
||
| 162 | // no known text variables found |
||
| 163 | return false; |
||
| 164 | } |
||
| 165 | |||
| 166 | // if no offset is specified, apply 1 as default |
||
| 167 | !$offsetMatch[0] ? $offset = 1: $offset = $offsetMatch[0]; |
||
| 168 | |||
| 169 | // remove "s" to unify e.g. weeks => week |
||
| 170 | $unit = rtrim($unitMatch[0], 's'); |
||
| 171 | |||
| 172 | if ($directionMatch[0] === "last" || $directionMatch[0] === "yester") { |
||
| 173 | // go back |
||
| 174 | $direction = '-'; |
||
| 175 | } elseif ($directionMatch[0] === "next") { |
||
| 176 | // go forward |
||
| 177 | $direction = '+'; |
||
| 178 | } else { |
||
| 179 | // current |
||
| 180 | $direction = '+'; |
||
| 181 | $offset = 0; |
||
| 182 | } |
||
| 183 | |||
| 184 | // create a usable string for php like "+3 days" |
||
| 185 | $timeString = $direction . $offset . ' ' . $unit; |
||
| 186 | // get a timestamp of the target date |
||
| 187 | $baseDate = strtotime($timeString); |
||
| 188 | |||
| 189 | // get the correct format depending of the unit. e.g. first day of the month in case unit is "month" |
||
| 190 | if ($unit === 'day') { |
||
| 191 | $startString = 'today'; |
||
| 192 | } else { |
||
| 193 | $startString = 'first day of this ' . $unit; |
||
| 194 | } |
||
| 195 | $startTS = strtotime($startString, $baseDate); |
||
| 196 | $start = date("Y-m-d", $startTS); |
||
| 197 | |||
| 198 | $return = [ |
||
| 199 | 'value' => $startTS, |
||
| 200 | 'option' => 'GT', |
||
| 201 | '1$filter' => $filter, |
||
| 202 | '2$timestring' => $timeString, |
||
| 203 | '3$target' => $baseDate, |
||
| 204 | '4$target_clean' => date("Y-m-d", $baseDate), |
||
| 205 | '5$startString' => $startString, |
||
| 206 | '6$startDate' => $start, |
||
| 207 | '7$startTS' => $startTS, |
||
| 208 | ]; |
||
| 209 | } else { |
||
| 210 | $return = false; |
||
| 211 | } |
||
| 212 | return $return; |
||
| 213 | } |
||
| 229 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.