| Conditions | 14 |
| Paths | 18 |
| Total Lines | 72 |
| Code Lines | 26 |
| 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 |
||
| 57 | public static function ip_range_to_subnet_array($startip, $endip) |
||
| 58 | { |
||
| 59 | |||
| 60 | if (!Util::is_ipaddr($startip) || !Util::is_ipaddr($endip)) { |
||
| 61 | return []; |
||
| 62 | } |
||
| 63 | |||
| 64 | // Container for subnets within this range. |
||
| 65 | $rangesubnets = []; |
||
| 66 | |||
| 67 | // Figure out what the smallest subnet is that holds the number of IPs in the |
||
| 68 | // given range. |
||
| 69 | $cidr = Util::find_smallest_cidr(Util::ip_range_size($startip, $endip)); |
||
| 70 | |||
| 71 | // Loop here to reduce subnet size and retest as needed. We need to make sure |
||
| 72 | // that the target subnet is wholly contained between $startip and $endip. |
||
| 73 | for ($cidr; $cidr <= 32; $cidr++) { |
||
| 74 | // Find the network and broadcast addresses for the subnet being tested. |
||
| 75 | $targetsub_min = Util::gen_subnet($startip, $cidr); |
||
| 76 | $targetsub_max = Util::gen_subnet_max($startip, $cidr); |
||
| 77 | |||
| 78 | // Check best case where the range is exactly one subnet. |
||
| 79 | if (($targetsub_min == $startip) && ($targetsub_max == $endip)) { |
||
| 80 | // Hooray, the range is exactly this subnet! |
||
| 81 | return ["{$startip}/{$cidr}"]; |
||
| 82 | } |
||
| 83 | |||
| 84 | // These remaining scenarios will find a subnet that uses the largest |
||
| 85 | // chunk possible of the range being tested, and leave the rest to be |
||
| 86 | // tested recursively after the loop. |
||
| 87 | |||
| 88 | // Check if the subnet begins with $startip and ends before $endip |
||
| 89 | if (($targetsub_min == $startip) && Util::ip_less_than($targetsub_max, $endip)) { |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | |||
| 93 | // Check if the subnet ends at $endip and starts after $startip |
||
| 94 | if (Util::ip_greater_than($targetsub_min, $startip) && ($targetsub_max == $endip)) { |
||
| 95 | break; |
||
| 96 | } |
||
| 97 | |||
| 98 | // Check if the subnet is between $startip and $endip |
||
| 99 | if (Util::ip_greater_than($targetsub_min, $startip) && Util::ip_less_than($targetsub_max, $endip)) { |
||
| 100 | break; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | // Some logic that will recursively search from $startip to the first IP before |
||
| 105 | // the start of the subnet we just found. |
||
| 106 | // NOTE: This may never be hit, the way the above algo turned out, but is left |
||
| 107 | // for completeness. |
||
| 108 | if ($startip != $targetsub_min) { |
||
| 109 | $rangesubnets = array_merge( |
||
| 110 | $rangesubnets, |
||
| 111 | self::ip_range_to_subnet_array($startip, Util::ip_before($targetsub_min)) |
||
|
|
|||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | // Add in the subnet we found before, to preserve ordering |
||
| 116 | $rangesubnets[] = "{$targetsub_min}/{$cidr}"; |
||
| 117 | |||
| 118 | // And some more logic that will search after the subnet we found to fill in |
||
| 119 | // to the end of the range. |
||
| 120 | if ($endip != $targetsub_max) { |
||
| 121 | $rangesubnets = array_merge( |
||
| 122 | $rangesubnets, |
||
| 123 | self::ip_range_to_subnet_array(Util::ip_after($targetsub_max), $endip) |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $rangesubnets; |
||
| 128 | } |
||
| 129 | |||
| 265 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: