Conditions | 12 |
Paths | 23 |
Total Lines | 40 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
20 | public static function validate($str) |
||
21 | { |
||
22 | $ranges = []; |
||
|
|||
23 | $matches = []; |
||
24 | |||
25 | $numMatches = preg_match_all(sprintf('/%s/', Expr::RANGE), $str, $matches); |
||
26 | if ($numMatches === 0) { |
||
27 | return [self::VALIDATION_NO_RANGE_FOUND, []]; |
||
28 | } |
||
29 | |||
30 | $numAstericks = substr_count($str, '*'); |
||
31 | if ($numAstericks > 1) { |
||
32 | return [self::VALIDATION_CONFLICT, []]; |
||
33 | } |
||
34 | |||
35 | $lowerLimits = $matches[1]; |
||
36 | $upperLimits = $matches[2]; |
||
37 | $ranges = []; |
||
38 | for ($i = 0; $i < count($lowerLimits); $i++) { |
||
39 | if (is_numeric($upperLimits[$i]) && floatval($lowerLimits[$i]) > floatval($upperLimits[$i])) { |
||
40 | return [self::VALIDATION_CONFLICT, []]; |
||
41 | } |
||
42 | $ranges[] = [$lowerLimits[$i], $upperLimits[$i]]; |
||
43 | } |
||
44 | |||
45 | for ($i = 0; $i < count($ranges); $i++) { |
||
46 | for ($j = $i + 1; $j < count($ranges); $j++) { |
||
47 | $inRange = self::isInRange($ranges[$i], $ranges[$j][0]) || |
||
48 | self::isInRange($ranges[$i], $ranges[$j][1]) || |
||
49 | |||
50 | self::isInRange($ranges[$j], $ranges[$i][0]) || |
||
51 | self::isInRange($ranges[$j], $ranges[$i][1]); |
||
52 | |||
53 | if ($inRange) { |
||
54 | return [self::VALIDATION_OVERLAPPING_VALUES, []]; |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | |||
59 | return [self::VALIDATION_OK, $ranges]; |
||
60 | } |
||
96 |