| Conditions | 7 |
| Paths | 14 |
| Total Lines | 54 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 34 | public static function validate($str): array |
||
| 35 | { |
||
| 36 | $segments = preg_split(sprintf('/%s/', Expr::PIPE), $str); |
||
| 37 | |||
| 38 | $matches = []; |
||
| 39 | foreach($segments as $segment) { |
||
| 40 | $structure = trim($segment); |
||
| 41 | $partOfAnotherStructure = strpos($segment, ',') !== false; |
||
| 42 | if ($partOfAnotherStructure) { |
||
| 43 | $structure = trim(explode(',', $segment)[1]); |
||
| 44 | } |
||
| 45 | |||
| 46 | if(preg_match(sprintf('/^%s$/', Expr::RANGE), $structure, $matches) === 0) { |
||
| 47 | throw new RangeException( |
||
| 48 | sprintf('Invalid range specified "%s"', $structure), |
||
| 49 | -1 |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | $matches = []; |
||
| 55 | $numMatches = preg_match_all(sprintf('/%s/', Expr::RANGE), $str, $matches); |
||
| 56 | if ($numMatches === 0) { |
||
| 57 | throw new RangeConflictException('No range values provided', RangeException::NO_RANGE_FOUND); |
||
| 58 | } |
||
| 59 | |||
| 60 | $numAstericks = substr_count($str, '*'); |
||
| 61 | if ($numAstericks > 1) { |
||
| 62 | throw new RangeConflictException( |
||
| 63 | 'More than one * provided in the ranges. There can be only one within a set of ranges to be examined.', |
||
| 64 | RangeException::MULTIPLE_OPEN_ENDED_UPPER_LIMIT |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | $ranges = self::getRangeLimits($str); |
||
| 69 | $overlappingRanges = self::findOverlappingRanges($ranges); |
||
| 70 | |||
| 71 | if (count($overlappingRanges) !== 0) { |
||
| 72 | $message = sprintf( |
||
| 73 | 'The ranges %s - %s and %s - %s are overlapping and will lead to unexpected results.', |
||
| 74 | $overlappingRanges[0][0], |
||
| 75 | $overlappingRanges[0][1], |
||
| 76 | $overlappingRanges[1][0], |
||
| 77 | $overlappingRanges[1][1] |
||
| 78 | ); |
||
| 79 | |||
| 80 | throw new RangeOverlapException( |
||
| 81 | $message, |
||
| 82 | RangeException::NO_RANGE_FOUND, |
||
| 83 | $overlappingRanges |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $ranges; |
||
| 88 | } |
||
| 177 |