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