| Conditions | 14 |
| Paths | 42 |
| Total Lines | 69 |
| Lines | 23 |
| Ratio | 33.33 % |
| 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 |
||
| 83 | public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) |
||
| 84 | { |
||
| 85 | if ($this->oLogger) |
||
| 86 | { |
||
| 87 | $this->oLogger->Write('DirectAdmin: Try to change password for '.$oAccount->Email()); |
||
| 88 | } |
||
| 89 | |||
| 90 | $bResult = false; |
||
| 91 | if (!empty($this->sHost) && 0 < $this->iPort && $oAccount) |
||
| 92 | { |
||
| 93 | $sEmail = \trim(\strtolower($oAccount->Email())); |
||
| 94 | |||
| 95 | $sHost = \trim($this->sHost); |
||
| 96 | $sHost = \str_replace('{user:host-imap}', $oAccount->Domain()->IncHost(), $sHost); |
||
| 97 | $sHost = \str_replace('{user:host-smtp}', $oAccount->Domain()->OutHost(), $sHost); |
||
| 98 | $sHost = \str_replace('{user:domain}', \MailSo\Base\Utils::GetDomainFromEmail($sEmail), $sHost); |
||
| 99 | $sHost = \rtrim($this->sHost, '/\\'); |
||
| 100 | |||
| 101 | if (!\preg_match('/^http[s]?:\/\//i', $sHost)) |
||
| 102 | { |
||
| 103 | $sHost = 'http://'.$sHost; |
||
| 104 | } |
||
| 105 | |||
| 106 | $sUrl = $sHost.':'.$this->iPort.'/CMD_CHANGE_EMAIL_PASSWORD'; |
||
| 107 | |||
| 108 | $iCode = 0; |
||
| 109 | $oHttp = \MailSo\Base\Http::SingletonInstance(); |
||
| 110 | |||
| 111 | if ($this->oLogger) |
||
| 112 | { |
||
| 113 | $this->oLogger->Write('DirectAdmin[Api Request]:'.$sUrl); |
||
| 114 | } |
||
| 115 | |||
| 116 | $mResult = $oHttp->SendPostRequest($sUrl, |
||
| 117 | array( |
||
| 118 | 'email' => $sEmail, |
||
| 119 | 'oldpassword' => $sPrevPassword, |
||
| 120 | 'password1' => $sNewPassword, |
||
| 121 | 'password2' => $sNewPassword, |
||
| 122 | 'api' => '1' |
||
| 123 | ), 'MailSo Http User Agent (v1)', $iCode, $this->oLogger); |
||
| 124 | |||
| 125 | View Code Duplication | if (false !== $mResult && 200 === $iCode) |
|
| 126 | { |
||
| 127 | $aRes = null; |
||
| 128 | @\parse_str($mResult, $aRes); |
||
| 129 | if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1)) |
||
| 130 | { |
||
| 131 | $bResult = true; |
||
| 132 | } |
||
| 133 | else |
||
| 134 | { |
||
| 135 | if ($this->oLogger) |
||
| 136 | { |
||
| 137 | $this->oLogger->Write('DirectAdmin[Error]: Response: '.$mResult); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | else |
||
| 142 | { |
||
| 143 | if ($this->oLogger) |
||
| 144 | { |
||
| 145 | $this->oLogger->Write('DirectAdmin[Error]: Empty Response: Code:'.$iCode); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return $bResult; |
||
| 151 | } |
||
| 152 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.