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 |
||
56 | public static function ip_range_to_subnet_array($startip, $endip) |
||
57 | { |
||
58 | |||
59 | if (!Util::is_ipaddr($startip) || !Util::is_ipaddr($endip)) { |
||
60 | return []; |
||
61 | } |
||
62 | |||
63 | // Container for subnets within this range. |
||
64 | $rangesubnets = []; |
||
65 | |||
66 | // Figure out what the smallest subnet is that holds the number of IPs in the |
||
67 | // given range. |
||
68 | $cidr = Util::find_smallest_cidr(Util::ip_range_size($startip, $endip)); |
||
69 | |||
70 | // Loop here to reduce subnet size and retest as needed. We need to make sure |
||
71 | // that the target subnet is wholly contained between $startip and $endip. |
||
72 | for ($cidr; $cidr <= 32; $cidr++) { |
||
73 | // Find the network and broadcast addresses for the subnet being tested. |
||
74 | $targetsub_min = Util::gen_subnet($startip, $cidr); |
||
75 | $targetsub_max = Util::gen_subnet_max($startip, $cidr); |
||
76 | |||
77 | // Check best case where the range is exactly one subnet. |
||
78 | if (($targetsub_min == $startip) && ($targetsub_max == $endip)) { |
||
79 | // Hooray, the range is exactly this subnet! |
||
80 | return ["{$startip}/{$cidr}"]; |
||
81 | } |
||
82 | |||
83 | // These remaining scenarios will find a subnet that uses the largest |
||
84 | // chunk possible of the range being tested, and leave the rest to be |
||
85 | // tested recursively after the loop. |
||
86 | |||
87 | // Check if the subnet begins with $startip and ends before $endip |
||
88 | if (($targetsub_min == $startip) && Util::ip_less_than($targetsub_max, $endip)) { |
||
89 | break; |
||
90 | } |
||
91 | |||
92 | // Check if the subnet ends at $endip and starts after $startip |
||
93 | if (Util::ip_greater_than($targetsub_min, $startip) && ($targetsub_max == $endip)) { |
||
94 | break; |
||
95 | } |
||
96 | |||
97 | // Check if the subnet is between $startip and $endip |
||
98 | if (Util::ip_greater_than($targetsub_min, $startip) && Util::ip_less_than($targetsub_max, $endip)) { |
||
99 | break; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // Some logic that will recursively search from $startip to the first IP before |
||
104 | // the start of the subnet we just found. |
||
105 | // NOTE: This may never be hit, the way the above algo turned out, but is left |
||
106 | // for completeness. |
||
107 | if ($startip != $targetsub_min) { |
||
108 | $rangesubnets = array_merge( |
||
109 | $rangesubnets, |
||
110 | self::ip_range_to_subnet_array($startip, Util::ip_before($targetsub_min)) |
||
|
|||
111 | ); |
||
112 | } |
||
113 | |||
114 | // Add in the subnet we found before, to preserve ordering |
||
115 | $rangesubnets[] = "{$targetsub_min}/{$cidr}"; |
||
116 | |||
117 | // And some more logic that will search after the subnet we found to fill in |
||
118 | // to the end of the range. |
||
119 | if ($endip != $targetsub_max) { |
||
120 | $rangesubnets = array_merge( |
||
121 | $rangesubnets, |
||
122 | self::ip_range_to_subnet_array(Util::ip_after($targetsub_max), $endip) |
||
123 | ); |
||
124 | } |
||
125 | |||
126 | return $rangesubnets; |
||
127 | } |
||
128 | |||
264 |
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: