| Conditions | 9 |
| Paths | 14 |
| Total Lines | 62 |
| 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 |
||
| 124 | private function parseFilter($filter, $option) { |
||
| 125 | preg_match_all("/(?<=%).*(?=%)/", $filter, $matches); |
||
| 126 | if (count($matches[0]) > 0) { |
||
| 127 | $filter = $matches[0][0]; |
||
| 128 | preg_match('/(last|next|current|to|yester)?/', $filter, $directionMatch); |
||
| 129 | preg_match('/[0-9]+/', $filter, $offsetMatch); |
||
| 130 | preg_match('/(day|days|week|weeks|month|months|year|years)$/', $filter, $unitMatch); |
||
| 131 | |||
| 132 | if (!$directionMatch[0] || !$unitMatch[0]) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | |||
| 136 | !$offsetMatch[0] ? $offset = 1: $offset = $offsetMatch[0]; |
||
| 137 | |||
| 138 | // remove s to unify e.g. weeks => week |
||
| 139 | $unit = rtrim($unitMatch[0], 's'); |
||
| 140 | |||
| 141 | if ($directionMatch[0] === "last" || $directionMatch[0] === "yester") { |
||
| 142 | $direction = '-'; |
||
| 143 | //$directionWord = $directionMatch[0]; |
||
| 144 | } elseif ($directionMatch[0] === "next") { |
||
| 145 | $direction = '+'; |
||
| 146 | //$directionWord = $directionMatch[0]; |
||
| 147 | } else { // current |
||
| 148 | $direction = '+'; |
||
| 149 | $offset = 0; |
||
| 150 | //$directionWord = 'this'; |
||
| 151 | } |
||
| 152 | |||
| 153 | $timestring = $direction . $offset . ' ' . $unit; |
||
| 154 | $baseDate = strtotime($timestring); |
||
| 155 | |||
| 156 | if ($unit === 'day') { |
||
| 157 | $startString = 'today'; |
||
| 158 | //$endString = 'yesterday'; |
||
| 159 | } else { |
||
| 160 | $startString = 'first day of this ' . $unit; |
||
| 161 | //$endString = 'last day of ' . $directionWord . ' ' . $unit; |
||
| 162 | } |
||
| 163 | $startTS = strtotime($startString, $baseDate); |
||
| 164 | $start = date("Y-m-d", $startTS); |
||
| 165 | //$endTS = strtotime($endString); |
||
| 166 | //$end = date("Y-m-d", $endTS); |
||
| 167 | |||
| 168 | $return = [ |
||
| 169 | 'value' => $startTS, |
||
| 170 | 'option' => 'GT', |
||
| 171 | '1$filter' => $filter, |
||
| 172 | '2$timestring' => $timestring, |
||
| 173 | '3$target' => $baseDate, |
||
| 174 | '4$target_clean' => date("Y-m-d", $baseDate), |
||
| 175 | '5$startString' => $startString, |
||
| 176 | '6$startDate' => $start, |
||
| 177 | '7$startTS' => $startTS, |
||
| 178 | //'8$endString' => $endString, |
||
| 179 | //'9$endDate' => $end, |
||
| 180 | //'9$endTS' => $endTS, |
||
| 181 | ]; |
||
| 182 | } else { |
||
| 183 | $return = false; |
||
| 184 | } |
||
| 185 | return $return; |
||
| 186 | } |
||
| 202 | } |
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.