| Conditions | 15 |
| Paths | 21 |
| Total Lines | 99 |
| Code Lines | 58 |
| 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 |
||
| 111 | public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) |
||
| 112 | { |
||
| 113 | $this->WriteLog('Virtualmin: Try to change password for '.$oAccount->Email()); |
||
| 114 | |||
| 115 | $bResult = false; |
||
| 116 | if (!empty($this->sHost) && !empty($this->sAdminUser) && !empty($this->sAdminPassword) && $oAccount) |
||
| 117 | { |
||
| 118 | $this->WriteLog('Virtualmin:[Check] Required Fields Present'); |
||
| 119 | |||
| 120 | $sEmail = \trim(\strtolower($oAccount->Email())); |
||
| 121 | $sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail); |
||
| 122 | $sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); |
||
| 123 | |||
| 124 | $sHost = \rtrim(\trim($this->sHost), '/'); |
||
| 125 | $sUrl = $sHost.'/virtual-server/remote.cgi'; |
||
| 126 | |||
| 127 | $sAdminUser = $this->sAdminUser; |
||
| 128 | $sAdminPassword = $this->sAdminPassword; |
||
| 129 | |||
| 130 | $iCode = 0; |
||
| 131 | |||
| 132 | $aPost = array( |
||
| 133 | 'user' => $sEmailUser, |
||
| 134 | 'pass' => $sNewPassword, |
||
| 135 | 'domain' => $sEmailDomain, |
||
| 136 | 'program' => 'modify-user' |
||
| 137 | ); |
||
| 138 | |||
| 139 | $aOptions = array( |
||
| 140 | CURLOPT_URL => $sUrl, |
||
| 141 | CURLOPT_HEADER => false, |
||
| 142 | CURLOPT_FAILONERROR => true, |
||
| 143 | CURLOPT_SSL_VERIFYPEER => false, |
||
| 144 | CURLOPT_RETURNTRANSFER => true, |
||
| 145 | CURLOPT_POST => true, |
||
| 146 | CURLOPT_POSTFIELDS => \http_build_query($aPost, '', '&'), |
||
| 147 | CURLOPT_TIMEOUT => 20, |
||
| 148 | CURLOPT_SSL_VERIFYHOST => false, |
||
| 149 | CURLOPT_USERPWD => $sAdminUser.':'.$sAdminPassword |
||
| 150 | ); |
||
| 151 | |||
| 152 | $oCurl = \curl_init(); |
||
| 153 | \curl_setopt_array($oCurl, $aOptions); |
||
| 154 | |||
| 155 | $this->WriteLog('Virtualmin: Send post request: '.$sUrl); |
||
| 156 | |||
| 157 | $mResult = \curl_exec($oCurl); |
||
| 158 | |||
| 159 | $iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE); |
||
| 160 | $sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE); |
||
| 161 | |||
| 162 | $this->WriteLog('Virtualmin: Post request result: (Status: '.$iCode.', ContentType: '.$sContentType.')'); |
||
| 163 | if (false === $mResult || 200 !== $iCode) |
||
| 164 | { |
||
| 165 | $this->WriteLog('Virtualmin: Error: '.\curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING); |
||
| 166 | } |
||
| 167 | |||
| 168 | if (\is_resource($oCurl)) |
||
| 169 | { |
||
| 170 | \curl_close($oCurl); |
||
| 171 | } |
||
| 172 | |||
| 173 | if (false !== $mResult && 200 === $iCode) |
||
| 174 | { |
||
| 175 | $aRes = null; |
||
| 176 | @\parse_str($mResult, $aRes); |
||
| 177 | if (\is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1)) |
||
| 178 | { |
||
| 179 | $iPos = \strpos($mResult, 'Exit status: '); |
||
| 180 | |||
| 181 | if ($iPos !== false) |
||
| 182 | { |
||
| 183 | $aStatus = \explode(' ', $mResult); |
||
| 184 | $sStatus = \trim(\array_pop($aStatus)); |
||
| 185 | |||
| 186 | if ('0' === $sStatus) |
||
| 187 | { |
||
| 188 | $this->WriteLog('Virtualmin: Password Change Status: Success'); |
||
| 189 | $bResult = true; |
||
| 190 | } |
||
| 191 | else |
||
| 192 | { |
||
| 193 | $this->WriteLog('Virtualmin[Error]: Response: '.$mResult); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | else |
||
| 198 | { |
||
| 199 | $this->WriteLog('Virtualmin[Error]: Response: '.$mResult); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | else |
||
| 203 | { |
||
| 204 | $this->WriteLog('Virtualmin[Error]: Empty Response: Code: '.$iCode); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | return $bResult; |
||
| 209 | } |
||
| 210 | } |
||
| 211 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.