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