| Conditions | 10 |
| Paths | 69 |
| Total Lines | 50 |
| Code Lines | 31 |
| 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 |
||
| 46 | public function configure(): bool |
||
| 47 | { |
||
| 48 | if(Util::isSystemctl() && ! Util::isDocker()){ |
||
| 49 | // Не настраиваем. |
||
| 50 | return true; |
||
| 51 | } |
||
| 52 | $lofFile = '/var/log/lastlog'; |
||
| 53 | if(!file_exists($lofFile)){ |
||
| 54 | file_put_contents($lofFile, ''); |
||
| 55 | } |
||
| 56 | $dropBearDir = '/etc/dropbear'; |
||
| 57 | Util::mwMkdir($dropBearDir); |
||
| 58 | |||
| 59 | $keytypes = [ |
||
| 60 | "rsa" => "SSHRsaKey", |
||
| 61 | "dss" => "SSHDssKey", |
||
| 62 | "ecdsa" => "SSHecdsaKey" |
||
| 63 | ]; |
||
| 64 | |||
| 65 | $options = ($this->mikoPBXConfig->getGeneralSettings('SSHDisablePasswordLogins') === '1')?'-s':''; |
||
| 66 | // Get keys from DB |
||
| 67 | $dropbearkeyPath = Util::which('dropbearkey'); |
||
| 68 | $dropbearPath = Util::which('dropbear'); |
||
| 69 | foreach ($keytypes as $keytype => $db_key) { |
||
| 70 | $res_keyfilepath = "{$dropBearDir}/dropbear_" . $keytype . "_host_key"; |
||
| 71 | $key = $this->mikoPBXConfig->getGeneralSettings($db_key); |
||
| 72 | $key = (isset($key) && is_string($key)) ? trim($key) : ""; |
||
| 73 | if (strlen($key) > 100) { |
||
| 74 | // Store key to file |
||
| 75 | file_put_contents($res_keyfilepath, base64_decode($key)); |
||
| 76 | } |
||
| 77 | // If key not exists, we will generate and store new one into file and database |
||
| 78 | if ( ! file_exists($res_keyfilepath)) { |
||
| 79 | // Generation |
||
| 80 | Processes::mwExec("{$dropbearkeyPath} -t $keytype -f $res_keyfilepath"); |
||
| 81 | // Storing |
||
| 82 | $new_key = base64_encode(file_get_contents($res_keyfilepath)); |
||
| 83 | $this->mikoPBXConfig->setGeneralSettings($db_key, $new_key); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | $ssh_port = escapeshellcmd($this->mikoPBXConfig->getGeneralSettings('SSHPort')); |
||
| 87 | // Restart dropbear |
||
| 88 | $this->updateShellPassword(); |
||
| 89 | |||
| 90 | Processes::killByName('dropbear'); |
||
| 91 | usleep(500000); |
||
| 92 | $result = Processes::mwExec("{$dropbearPath} -p '{$ssh_port}' $options -c /etc/rc/hello > /var/log/dropbear_start.log"); |
||
| 93 | $this->generateAuthorizedKeys(); |
||
| 94 | |||
| 95 | return ($result === 0); |
||
| 96 | } |
||
| 176 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.