| Conditions | 27 |
| Paths | 36 |
| Total Lines | 119 |
| Code Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 756 |
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 |
||
| 110 | function checkLogin($login, $password, $requestedPath="/") { |
||
| 111 | global $ARCurrent, $AR; |
||
| 112 | debug("checkLogin($login, [password])", "all"); |
||
| 113 | $result = null; |
||
| 114 | if ($login) { |
||
| 115 | debug("checkLogin: initiating new login ($login)", "all"); |
||
| 116 | if ($ARCurrent->session) { |
||
| 117 | $ARUserDir = $ARCurrent->session->get("ARUserDir", true); |
||
| 118 | if (!$ARUserDir) { |
||
| 119 | $ARUserDir = "/system/users/"; |
||
| 120 | } |
||
| 121 | |||
| 122 | if (!$ARCurrent->session->get("ARLogin") || |
||
| 123 | $ARCurrent->session->get("ARLogin") == "public") { |
||
| 124 | debug("checkLogin: logging into a public session (".$ARCurrent->session->id.")", "all"); |
||
| 125 | $result = $this->authUser($login, $password, $requestedPath); |
||
| 126 | if ($result !== true) { |
||
| 127 | $this->getUser('public'); |
||
| 128 | } |
||
| 129 | } else { |
||
| 130 | if (ldCheckCredentials($login)) { |
||
| 131 | debug("checkLogin: succesfully logged into private session (".$ARCurrent->session->id.")", "all"); |
||
| 132 | $result = $this->getUser($login, $ARUserDir); |
||
| 133 | } else { |
||
| 134 | if ($ARCurrent->session->get("ARLogin") == $login) { |
||
| 135 | debug("checkLogin: user ($login) tries to login to his session without a cookie set @ $ARUserDir", "all"); |
||
| 136 | $result = $this->authUser($login, $password, $ARUserDir); |
||
| 137 | if ($result !== true) { |
||
| 138 | $this->getUser('public'); |
||
| 139 | } |
||
| 140 | } else |
||
| 141 | if (ldCheckCredentials($ARCurrent->session->get("ARLogin"))) { |
||
| 142 | debug("checkLogin: user tries to login as another user", "all"); |
||
| 143 | $result = $this->authUser($login, $password, $requestedPath); |
||
| 144 | if ($result !== true) { |
||
| 145 | $this->getUser('public'); |
||
| 146 | } |
||
| 147 | } else { |
||
| 148 | debug("checkLogin: could not login to private session (".$ARCurrent->session->id."): creating a new one", "all"); |
||
| 149 | ldStartSession(); |
||
| 150 | $result = $this->authUser($login, $password, $ARUserDir); |
||
| 151 | if ($result !== true) { |
||
| 152 | $this->getUser('public'); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } else { |
||
| 158 | debug("checkLogin: trying to log on", "all"); |
||
| 159 | $result = $this->authUser($login, $password, $requestedPath); |
||
| 160 | if ($result !== true) { |
||
| 161 | $this->getUser('public'); |
||
| 162 | } |
||
| 163 | |||
| 164 | } |
||
| 165 | } else { |
||
| 166 | if ($ARCurrent->session) { |
||
| 167 | $ARUserDir = $ARCurrent->session->get("ARUserDir", true); |
||
| 168 | if (!$ARUserDir) { |
||
| 169 | $ARUserDir = "/system/users/"; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (!$ARCurrent->session->get("ARLogin")) { |
||
| 173 | if ($ARCurrent->session->get("ARSessionTimedout", 1)) { |
||
| 174 | $ARCurrent->session->put("ARSessionTimedout", 0, 1); |
||
| 175 | } |
||
| 176 | debug("checkLogin: logging in with public session (".$ARCurrent->session->id.")", "all"); |
||
| 177 | $result = $this->checkLogin("public", "none"); |
||
| 178 | } else |
||
| 179 | if ($ARCurrent->session->get("ARSessionTimedout", 1)) { |
||
| 180 | debug("checkLogin: session has been timedout, forcing login", "all"); |
||
| 181 | // become public |
||
| 182 | $this->getUser('public'); |
||
| 183 | $result = LD_ERR_SESSION; |
||
| 184 | } else { |
||
| 185 | $login = $ARCurrent->session->get("ARLogin"); |
||
| 186 | if (ldCheckCredentials($login)) { |
||
| 187 | debug("checkLogin: logging ($login) into a private session (".$ARCurrent->session->id.") with credentials from cookie", "all"); |
||
| 188 | $result = $this->getUser($login, $ARUserDir); |
||
| 189 | } else { |
||
| 190 | debug("checkLogin: could not login ($login) on private session (".$ARCurrent->session->id.") with credentials from cookie: removing cookie", "all"); |
||
| 191 | // FIXME: only the loader should know about cookies for sessions |
||
| 192 | setcookie("ARSessionCookie[".$ARCurrent->session->id."]", false); |
||
| 193 | $this->getUser('public'); |
||
| 194 | $result = LD_ERR_ACCESS; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | if ($AR->arSessionRespawn) { |
||
| 199 | debug("checkLogin: trying to respawn a session", "all"); |
||
| 200 | $cookies = ldGetCredentials(); |
||
| 201 | if (is_array($cookies)) { |
||
| 202 | reset($cookies); |
||
| 203 | while (!$result && (list($sid, $sval)=each($cookies))) { |
||
| 204 | ldStartSession($sid); |
||
| 205 | $login = $ARCurrent->session->get("ARLogin"); |
||
| 206 | debug("checkLogin: trying to respawn session ($sid) for user ($login)", "all"); |
||
| 207 | if (ldCheckCredentials($login)) { |
||
| 208 | $ARUserDir = $ARCurrent->session->get("ARUserDir", true); |
||
| 209 | if (!$ARUserDir) { |
||
| 210 | $ARUserDir = "/system/users/"; |
||
| 211 | } |
||
| 212 | |||
| 213 | debug("checkLogin: credentials matched, loading user", "all"); |
||
| 214 | $result = $this->getUser($login, $ARUserDir); |
||
| 215 | } else { |
||
| 216 | debug("checkLogin: credentials didn't match", "all"); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | if (!$result) { |
||
| 222 | debug("checkLogin: normal public login", "all"); |
||
| 223 | $result = $this->authUser("public", "none"); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | return $result; |
||
| 228 | } |
||
| 229 | } |
||
| 230 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: