| Total Complexity | 40 |
| Total Lines | 406 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SystemLoader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SystemLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class SystemLoader extends Di\Injectable |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * Message displayed during the start of a stage. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private string $stageMessage = ''; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Check if the system is running in Docker |
||
| 64 | * |
||
| 65 | * @var bool |
||
| 66 | */ |
||
| 67 | private bool $isDocker = false; |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * Check if the system is running from live cd |
||
| 72 | * |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | private bool $isRecoveryMode = false; |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Constructor |
||
| 80 | */ |
||
| 81 | public function __construct() |
||
| 82 | { |
||
| 83 | $this->isDocker = Util::isDocker(); |
||
| 84 | $this->isRecoveryMode = Util::isRecoveryMode(); |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Echoes the starting message for a stage. |
||
| 90 | * |
||
| 91 | * @param string $message The message to echo. |
||
| 92 | */ |
||
| 93 | private function echoStartMsg(string $message):void |
||
| 94 | { |
||
| 95 | $this->stageMessage = $message; |
||
| 96 | SystemMessages::echoToTeletype($this->stageMessage); |
||
| 97 | SystemMessages::echoWithSyslog($this->stageMessage); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Echoes the result message for a stage. |
||
| 102 | * |
||
| 103 | * @param string $result The result of the stage. |
||
| 104 | */ |
||
| 105 | private function echoResultMsg(string $result = SystemMessages::RESULT_DONE):void |
||
| 106 | { |
||
| 107 | SystemMessages::teletypeEchoResult($this->stageMessage, $result); |
||
| 108 | SystemMessages::echoResult($this->stageMessage, $result); |
||
| 109 | $this->stageMessage = ''; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Starts the system services. |
||
| 114 | * |
||
| 115 | * @return bool True on success, false otherwise. |
||
| 116 | */ |
||
| 117 | public function startSystem(): bool |
||
| 118 | { |
||
| 119 | $system = new System(); |
||
| 120 | // Is the configuration default? |
||
| 121 | // Try restore config... |
||
| 122 | if($system->isDefaultConf() && !$this->isRecoveryMode){ |
||
| 123 | $this->echoStartMsg(' - Try restore backup of settings... '); |
||
| 124 | $system->tryRestoreConf(); |
||
| 125 | $this->echoResultMsg(); |
||
| 126 | } |
||
| 127 | |||
| 128 | // Check if the system is running on T2SDELinux |
||
| 129 | $itIsT2SDELinux = Util::isT2SdeLinux(); |
||
| 130 | |||
| 131 | // Mark the registry as booting |
||
| 132 | $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = true; |
||
| 133 | |||
| 134 | // Start the ACPID daemon |
||
| 135 | $this->echoStartMsg(PHP_EOL); |
||
| 136 | $this->echoStartMsg(' - Start acpid daemon...'); |
||
| 137 | $ACPIDConf = new ACPIDConf(); |
||
| 138 | $ACPIDConf->reStart(); |
||
| 139 | $this->echoResultMsg(); |
||
| 140 | |||
| 141 | // Start the Beanstalkd daemon |
||
| 142 | $this->echoStartMsg(' - Start beanstalkd daemon...'); |
||
| 143 | $beanstalkConf = new BeanstalkConf(); |
||
| 144 | $beanstalkConf->reStart(); |
||
| 145 | $this->echoResultMsg(); |
||
| 146 | |||
| 147 | // Start the Redis daemon |
||
| 148 | $this->echoStartMsg(' - Start redis daemon...'); |
||
| 149 | $redisConf = new RedisConf(); |
||
| 150 | $redisConf->reStart(); |
||
| 151 | $this->echoResultMsg(); |
||
| 152 | |||
| 153 | // Configure Sentry error logger |
||
| 154 | $this->echoStartMsg(' - Configuring sentry error logger ...'); |
||
| 155 | if (!$this->isRecoveryMode){ |
||
| 156 | $sentryConf = new SentryConf(); |
||
| 157 | $sentryConf->configure(); |
||
| 158 | $this->echoResultMsg(); |
||
| 159 | } else { |
||
| 160 | $this->echoResultMsg(SystemMessages::RESULT_SKIPPED); |
||
| 161 | } |
||
| 162 | |||
| 163 | // Configure the system timezone |
||
| 164 | $this->echoStartMsg(' - Configuring timezone...'); |
||
| 165 | if (!$this->isRecoveryMode){ |
||
| 166 | System::timezoneConfigure(); |
||
| 167 | $this->echoResultMsg(); |
||
| 168 | } else { |
||
| 169 | $this->echoResultMsg(SystemMessages::RESULT_SKIPPED); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Mount the storage disk |
||
| 173 | $storage = new Storage(); |
||
| 174 | if($itIsT2SDELinux){ |
||
| 175 | // Do not need to set on Docker or Debian linux |
||
| 176 | $storage->saveFstab(); |
||
| 177 | } |
||
| 178 | $storage->configure(); |
||
| 179 | $this->echoStartMsg(' - Mount storage disk...'); |
||
| 180 | $this->echoResultMsg(); |
||
| 181 | |||
| 182 | // Additional tasks for T2SDELinux |
||
| 183 | if($itIsT2SDELinux) { |
||
| 184 | $this->echoStartMsg(' - Connect swap...'); |
||
| 185 | Processes::mwExecBg('/etc/rc/connect_swap'); |
||
| 186 | $this->echoResultMsg(); |
||
| 187 | } |
||
| 188 | |||
| 189 | // Start the syslogd daemon |
||
| 190 | $this->echoStartMsg(' - Start syslogd daemon...'); |
||
| 191 | $syslogConf = new SyslogConf(); |
||
| 192 | $syslogConf->reStart(); |
||
| 193 | $this->echoResultMsg(); |
||
| 194 | |||
| 195 | // Update the database structure |
||
| 196 | $dbUpdater = new UpdateDatabase(); |
||
| 197 | $dbUpdater->updateDatabaseStructure(); |
||
| 198 | |||
| 199 | // Create directories required by modules after DB upgrade |
||
| 200 | $this->echoStartMsg(' - Create modules links and folders...'); |
||
| 201 | $storage->createWorkDirsAfterDBUpgrade(); |
||
| 202 | $this->echoResultMsg(); |
||
| 203 | |||
| 204 | // Update the system configuration and applications |
||
| 205 | $this->echoStartMsg(' - Update configs and applications...'.PHP_EOL); |
||
| 206 | $confUpdate = new UpdateSystemConfig(); |
||
| 207 | $confUpdate->updateConfigs(); |
||
| 208 | $this->echoStartMsg(' - Update configs...'); |
||
| 209 | $this->echoResultMsg(); |
||
| 210 | |||
| 211 | // Configure VM tools |
||
| 212 | $this->echoStartMsg(' - Configuring VM tools...'); |
||
| 213 | if (!$this->isRecoveryMode){ |
||
| 214 | $vmwareTools = new VmToolsConf(); |
||
| 215 | $resultVMTools = $vmwareTools->configure(); |
||
| 216 | $this->echoResultMsg((string)$resultVMTools); |
||
| 217 | } else { |
||
| 218 | $this->echoResultMsg(SystemMessages::RESULT_SKIPPED); |
||
| 219 | } |
||
| 220 | |||
| 221 | // Configure the system hostname |
||
| 222 | $this->echoStartMsg(' - Configuring hostname...'); |
||
| 223 | $network = new Network(); |
||
| 224 | $network->hostnameConfigure(); |
||
| 225 | $this->echoResultMsg(); |
||
| 226 | |||
| 227 | // Generate resolv.conf |
||
| 228 | $this->echoStartMsg(' - Configuring resolv.conf...'); |
||
| 229 | $network->resolvConfGenerate(); |
||
| 230 | $this->echoResultMsg(); |
||
| 231 | |||
| 232 | // Configure LAN interface |
||
| 233 | $this->echoStartMsg(' - Configuring LAN interface...'); |
||
| 234 | if ($this->isDocker){ |
||
| 235 | $network->configureLanInDocker(); |
||
| 236 | } else { |
||
| 237 | $network->lanConfigure(); |
||
| 238 | } |
||
| 239 | $this->echoResultMsg(); |
||
| 240 | |||
| 241 | // SSL rehash |
||
| 242 | $this->echoStartMsg(' - SSL rehash...'); |
||
| 243 | System::sslRehash(); |
||
| 244 | $this->echoResultMsg(); |
||
| 245 | |||
| 246 | // Configure the firewall |
||
| 247 | $this->echoStartMsg(' - Configuring Firewall...'); |
||
| 248 | $firewall = new IptablesConf(); |
||
| 249 | $firewall->applyConfig(); |
||
| 250 | $this->echoResultMsg(); |
||
| 251 | |||
| 252 | // Configure NTP |
||
| 253 | $this->echoStartMsg(' - Configuring ntpd...'); |
||
| 254 | if (!$this->isRecoveryMode){ |
||
| 255 | NTPConf::configure(); |
||
| 256 | $this->echoResultMsg(); |
||
| 257 | } else { |
||
| 258 | $this->echoResultMsg(SystemMessages::RESULT_SKIPPED); |
||
| 259 | } |
||
| 260 | |||
| 261 | // Do not need to set Debian SSH service |
||
| 262 | if(!Util::isSystemctl()){ |
||
| 263 | $this->echoStartMsg(' - Configuring SSH console...'); |
||
| 264 | $sshConf = new SSHConf(); |
||
| 265 | $resSsh = $sshConf->configure(); |
||
| 266 | $this->echoResultMsg((string)$resSsh); |
||
| 267 | } |
||
| 268 | |||
| 269 | // Start cloud provisioning |
||
| 270 | if (!$this->isDocker && !$this->isRecoveryMode) { |
||
| 271 | $this->echoStartMsg(' - Attempt to cloud provisioning...'.PHP_EOL); |
||
| 272 | CloudProvisioning::start(); |
||
| 273 | } else { |
||
| 274 | $this->echoStartMsg(' - Attempt to cloud provisioning...'); |
||
| 275 | $this->echoResultMsg(SystemMessages::RESULT_SKIPPED); |
||
| 276 | } |
||
| 277 | |||
| 278 | // Connect storage in a cloud if needed |
||
| 279 | $this->echoStartMsg(' - Auto connect storage for a cloud ...'); |
||
| 280 | if (!$this->isDocker && !$this->isRecoveryMode) { |
||
| 281 | $connectResult = Storage::connectStorageInCloud(); |
||
| 282 | $this->echoResultMsg($connectResult); |
||
| 283 | } else { |
||
| 284 | $this->echoResultMsg(SystemMessages::RESULT_SKIPPED); |
||
| 285 | } |
||
| 286 | |||
| 287 | // Update external IP if needed |
||
| 288 | if (!$this->isRecoveryMode) { |
||
| 289 | $this->echoStartMsg(' - Update external IP...'); |
||
| 290 | $network->updateExternalIp(); |
||
| 291 | $this->echoResultMsg(); |
||
| 292 | } else { |
||
| 293 | $this->echoResultMsg(SystemMessages::RESULT_SKIPPED); |
||
| 294 | } |
||
| 295 | $this->di->getShared(RegistryProvider::SERVICE_NAME)->booting = false; |
||
| 296 | |||
| 297 | return true; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Load Asterisk and Web interface |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function startMikoPBX(): bool |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Retrieves the information message containing available web interface addresses. |
||
| 372 | * |
||
| 373 | * @return string The information message. |
||
| 374 | */ |
||
| 375 | public static function getInfoMessage(): string |
||
| 459 | } |
||
| 460 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.