| Conditions | 15 |
| Paths | 2048 |
| Total Lines | 102 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 157 | public function renewBoundAction(): void |
||
| 158 | { |
||
| 159 | // Initialize an array to store environment variables related to network configuration. |
||
| 160 | $env_vars = [ |
||
| 161 | 'broadcast' => '', // 10.0.0.255 |
||
| 162 | 'interface' => '', // eth0 |
||
| 163 | 'ip' => '', // 10.0.0.249 |
||
| 164 | 'router' => '', // 10.0.0.1 |
||
| 165 | 'timesvr' => '', |
||
| 166 | 'namesvr' => '', |
||
| 167 | 'dns' => '', // 10.0.0.254 |
||
| 168 | 'hostname' => '', // bad |
||
| 169 | 'subnet' => '', // 255.255.255.0 |
||
| 170 | 'serverid' => '', // 10.0.0.1 |
||
| 171 | 'ipttl' => '', |
||
| 172 | 'lease' => '', // 86400 |
||
| 173 | 'domain' => '', // bad |
||
| 174 | 'mtu'=>'' , // 1500 |
||
| 175 | 'staticroutes'=> '', // 0.0.0.0/0 10.0.0.1 169.254.169.254/32 10.0.0.65 0.0.0.0/0 10.0.0.1 |
||
| 176 | 'mask' => '', // 24 |
||
| 177 | ]; |
||
| 178 | |||
| 179 | // Check for debug mode to enable logging. |
||
| 180 | $debugMode = $this->di->getShared('config')->path('core.debugMode'); |
||
| 181 | |||
| 182 | // Retrieve and trim the values of the required environment variables. |
||
| 183 | foreach ($env_vars as $key => $value) { |
||
| 184 | $env_vars[$key] = trim(getenv($key)); |
||
| 185 | } |
||
| 186 | unset($value); |
||
| 187 | |||
| 188 | // Configure broadcast address if provided, otherwise leave it blank. |
||
| 189 | $BROADCAST = !empty($env_vars['broadcast']) ? "broadcast {$env_vars['broadcast']}" : ""; |
||
| 190 | |||
| 191 | // Handle subnet mask for /32 assignments and other cases. |
||
| 192 | $NET_MASK = (!empty($env_vars['subnet']) && $env_vars['subnet'] !== '255.255.255.255') ? "netmask {$env_vars['subnet']}" : ""; |
||
| 193 | |||
| 194 | // Configure the network interface with the provided IP, broadcast, and subnet mask. |
||
| 195 | $busyboxPath = Util::which('busybox'); |
||
| 196 | Processes::mwExec("{$busyboxPath} ifconfig {$env_vars['interface']} {$env_vars['ip']} $BROADCAST $NET_MASK"); |
||
| 197 | |||
| 198 | |||
| 199 | // Remove any existing default gateway routes associated with this interface. |
||
| 200 | while (true) { |
||
| 201 | $out = []; |
||
| 202 | Processes::mwExec("route del default gw 0.0.0.0 dev {$env_vars['interface']}", $out); |
||
| 203 | if (trim(implode('', $out)) !== '') { |
||
| 204 | // An error occurred, indicating that all routes have been cleared. |
||
| 205 | break; |
||
| 206 | } |
||
| 207 | if ($debugMode) { |
||
| 208 | break; |
||
| 209 | } // Otherwise, it will be an infinite loop. |
||
| 210 | } |
||
| 211 | |||
| 212 | // Add a default gateway route if a router address is provided and the interface is for the internet. |
||
| 213 | $if_data = LanInterfaces::findFirst("interface = '{$env_vars['interface']}'"); |
||
| 214 | $is_inet = ($if_data !== null) ? (int)$if_data->internet : 0; |
||
| 215 | if (!empty($env_vars['router']) && $is_inet === 1) { |
||
| 216 | // Only add the default route if this interface is for the internet. |
||
| 217 | $routers = explode(' ', $env_vars['router']); |
||
| 218 | foreach ($routers as $router) { |
||
| 219 | Processes::mwExec("route add default gw {$router} dev {$env_vars['interface']}"); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | // Add custom static routes if any are provided. |
||
| 224 | $this->addStaticRoutes($env_vars['staticroutes'], $env_vars['interface']); |
||
| 225 | |||
| 226 | // Add custom routes. |
||
| 227 | $this->addCustomStaticRoutes($env_vars['interface']); |
||
| 228 | |||
| 229 | |||
| 230 | // Setup DNS. |
||
| 231 | $named_dns = []; |
||
| 232 | if ('' !== $env_vars['dns']) { |
||
| 233 | $named_dns = explode(' ', $env_vars['dns']); |
||
| 234 | } |
||
| 235 | if ($is_inet === 1) { |
||
| 236 | // Only generate pdnsd config if this interface is for internet. |
||
| 237 | $this->generatePdnsdConfig($named_dns); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Save information to the database. |
||
| 241 | $data = [ |
||
| 242 | 'subnet' => '', |
||
| 243 | 'ipaddr' => $env_vars['ip'], |
||
| 244 | 'gateway' => $env_vars['router'], |
||
| 245 | ]; |
||
| 246 | if (Verify::isIpAddress($env_vars['ip'])) { |
||
| 247 | $data['subnet'] = $this->netMaskToCidr($env_vars['subnet']); |
||
| 248 | } |
||
| 249 | |||
| 250 | $this->updateIfSettings($data, $env_vars['interface']); |
||
| 251 | |||
| 252 | $data = [ |
||
| 253 | 'primarydns' => $named_dns[0] ?? '', |
||
| 254 | 'secondarydns' => $named_dns[1] ?? '', |
||
| 255 | ]; |
||
| 256 | $this->updateDnsSettings($data, $env_vars['interface']); |
||
| 257 | |||
| 258 | Processes::mwExecBg("/etc/rc/networking.set.mtu '{$env_vars['interface']}'"); |
||
| 259 | } |
||
| 314 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.