| Conditions | 18 |
| Paths | 160 |
| Total Lines | 115 |
| Code Lines | 77 |
| 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 |
||
| 141 | public static function consolidate_subnets($subnetsArray, $max = null) |
||
| 142 | { |
||
| 143 | |||
| 144 | $subnetsArray = Util::sort_cidrs($subnetsArray); |
||
| 145 | |||
| 146 | do { |
||
| 147 | $countSubnetsArray = count($subnetsArray); |
||
| 148 | $newSubnetsArray = []; |
||
| 149 | $subnetToMaskMap = []; |
||
| 150 | $ipReductionBySubnet = []; |
||
| 151 | reset($subnetsArray); |
||
| 152 | do { |
||
| 153 | $cidr = current($subnetsArray); |
||
| 154 | list($currentIP, $currentMask) = explode('/', $cidr); |
||
| 155 | $nextIP = null; |
||
| 156 | $nextMask = null; |
||
| 157 | |||
| 158 | if (next($subnetsArray) !== false) { |
||
| 159 | list($nextIP, $nextMask) = explode('/', current($subnetsArray)); |
||
| 160 | prev($subnetsArray); |
||
| 161 | } else { |
||
| 162 | end($subnetsArray); |
||
| 163 | } |
||
| 164 | |||
| 165 | $endIP = Util::gen_subnet_max($currentIP, $currentMask); |
||
| 166 | while (isset($nextIP) && Util::ip_after($endIP) == $nextIP) { |
||
| 167 | $nextEndIP = Util::gen_subnet_max($nextIP, $nextMask); |
||
| 168 | $consolidated = self::ip_range_to_subnet_array($currentIP, $nextEndIP); |
||
| 169 | if (count($consolidated) == 1) { |
||
| 170 | $endIP = $nextEndIP; |
||
| 171 | list($currentIP, $currentMask) = explode('/', $consolidated[0]); |
||
| 172 | if (next($subnetsArray) !== false) { |
||
| 173 | list($nextIP, $nextMask) = explode('/', current($subnetsArray)); |
||
| 174 | } else { |
||
| 175 | end($subnetsArray); |
||
| 176 | $nextIP = null; |
||
| 177 | $nextMask = null; |
||
| 178 | } |
||
| 179 | } else { |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | $newSubnetsArray[] = $currentIP . '/' . $currentMask; |
||
| 185 | |||
| 186 | $subnetToMaskMap[$currentIP] = [ |
||
| 187 | 'startIP' => $currentIP, |
||
| 188 | 'endIP' => $endIP, |
||
| 189 | 'mask' => $currentMask, |
||
| 190 | 'next' => isset($nextIP) ? $nextIP : 'none', |
||
| 191 | ]; |
||
| 192 | |||
| 193 | $toJoin = Util::get_single_subnet($currentIP, Util::gen_subnet_max($nextIP, $nextMask)); |
||
| 194 | if (!$toJoin) { |
||
| 195 | continue; |
||
| 196 | } |
||
| 197 | list($joinIP, $joinMask) = explode('/', $toJoin); |
||
| 198 | $diff = abs(Util::subnet_range_size($currentMask) - Util::subnet_range_size($joinMask)); |
||
| 199 | |||
| 200 | $ipReductionBySubnet[$joinIP] = [ |
||
| 201 | 'mask' => $joinMask, |
||
| 202 | 'diff' => $diff, |
||
| 203 | 'original' => $currentIP, |
||
| 204 | ]; |
||
| 205 | } while (next($subnetsArray) !== false); |
||
| 206 | $subnetsArray = $newSubnetsArray; |
||
| 207 | } while (count($subnetsArray) !== $countSubnetsArray); |
||
| 208 | |||
| 209 | // sort array by number of additional IPs introduced |
||
| 210 | uasort($ipReductionBySubnet, function ($a, $b) { |
||
| 211 | return $a['diff'] - $b['diff']; |
||
| 212 | }); |
||
| 213 | |||
| 214 | $returnCIDRs = []; |
||
| 215 | foreach ($subnetToMaskMap as $ip => $config) { |
||
| 216 | $returnCIDRs[] = $ip.'/'.$config['mask']; |
||
| 217 | } |
||
| 218 | |||
| 219 | if ($max === null || count($returnCIDRs) <= $max) { |
||
| 220 | return $returnCIDRs; |
||
| 221 | } |
||
| 222 | |||
| 223 | reset($ipReductionBySubnet); |
||
| 224 | do { |
||
| 225 | current($ipReductionBySubnet); |
||
| 226 | $injectedIP = key($ipReductionBySubnet); |
||
| 227 | |||
| 228 | $toUpdate = $ipReductionBySubnet[$injectedIP]['original']; |
||
| 229 | if (isset($subnetToMaskMap[$toUpdate])) { |
||
| 230 | $next = $subnetToMaskMap[$toUpdate]['next']; |
||
| 231 | |||
| 232 | // remove the two subnets we've just mushed |
||
| 233 | unset($subnetToMaskMap[$toUpdate]); |
||
| 234 | unset($subnetToMaskMap[$next]); |
||
| 235 | |||
| 236 | // chuck in the new one |
||
| 237 | $subnetToMaskMap[$injectedIP] = [ |
||
| 238 | 'mask' => $ipReductionBySubnet[$injectedIP]['mask'], |
||
| 239 | ]; |
||
| 240 | |||
| 241 | $returnCIDRs = []; |
||
| 242 | foreach ($subnetToMaskMap as $ip => $config) { |
||
| 243 | $returnCIDRs[] = $ip . '/' . $config['mask']; |
||
| 244 | } |
||
| 245 | |||
| 246 | $returnCIDRs = Util::sort_cidrs($returnCIDRs); |
||
| 247 | } |
||
| 248 | } while (count($returnCIDRs) > $max && next($ipReductionBySubnet) !== false); |
||
| 249 | |||
| 250 | if (count($returnCIDRs > $max)) { |
||
| 251 | return self::consolidate_subnets($returnCIDRs, $max); |
||
| 252 | } |
||
| 253 | |||
| 254 | return $returnCIDRs; |
||
| 255 | } |
||
| 256 | |||
| 292 |
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: