| Conditions | 23 |
| Paths | 224 |
| Total Lines | 66 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 28 | function writeJsConnect($user, $request, $clientID, $secret, $secure = true) { |
||
| 29 | $user = array_change_key_case($user); |
||
| 30 | |||
| 31 | // Error checking. |
||
| 32 | if ($secure) { |
||
| 33 | // Check the client. |
||
| 34 | if (!isset($request['v'])) { |
||
| 35 | $error = array('error' => 'invalid_request', 'message' => 'Missing the v parameter.'); |
||
| 36 | } elseif ($request['v'] !== JS_CONNECT_VERSION) { |
||
| 37 | $error = array('error' => 'invalid_request', 'message' => "Unsupported version {$request['v']}."); |
||
| 38 | } elseif (!isset($request['client_id'])) { |
||
| 39 | $error = array('error' => 'invalid_request', 'message' => 'Missing the client_id parameter.'); |
||
| 40 | } elseif ($request['client_id'] != $clientID) { |
||
| 41 | $error = array('error' => 'invalid_client', 'message' => "Unknown client {$request['client_id']}."); |
||
| 42 | } elseif (!isset($request['timestamp']) && !isset($request['sig'])) { |
||
| 43 | if (is_array($user) && count($user) > 0) { |
||
| 44 | // This isn't really an error, but we are just going to return public information when no signature is sent. |
||
| 45 | $error = array('name' => (string)@$user['name'], 'photourl' => @$user['photourl'], 'signedin' => true); |
||
| 46 | } else { |
||
| 47 | $error = array('name' => '', 'photourl' => ''); |
||
| 48 | } |
||
| 49 | } elseif (!isset($request['timestamp']) || !ctype_digit($request['timestamp'])) { |
||
| 50 | $error = array('error' => 'invalid_request', 'message' => 'The timestamp parameter is missing or invalid.'); |
||
| 51 | } elseif (!isset($request['sig'])) { |
||
| 52 | $error = array('error' => 'invalid_request', 'message' => 'Missing the sig parameter.'); |
||
| 53 | } // Make sure the timestamp hasn't timedout |
||
| 54 | elseif (abs($request['timestamp'] - JsTimestamp()) > JS_TIMEOUT) { |
||
| 55 | $error = array('error' => 'invalid_request', 'message' => 'The timestamp is invalid.'); |
||
| 56 | } elseif (!isset($request['nonce'])) { |
||
| 57 | $error = array('error' => 'invalid_request', 'message' => 'Missing the nonce parameter.'); |
||
| 58 | } elseif (!isset($request['ip'])) { |
||
| 59 | $error = array('error' => 'invalid_request', 'message' => 'Missing the ip parameter.'); |
||
| 60 | } else { |
||
| 61 | $signature = jsHash($request['ip'].$request['nonce'].$request['timestamp'].$secret, $secure); |
||
| 62 | if ($signature != $request['sig']) { |
||
| 63 | $error = array('error' => 'access_denied', 'message' => 'Signature invalid.'); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | if (isset($error)) { |
||
| 69 | $result = $error; |
||
| 70 | } elseif (is_array($user) && count($user) > 0) { |
||
| 71 | if ($secure === null) { |
||
|
|
|||
| 72 | $result = $user; |
||
| 73 | } else { |
||
| 74 | $user['ip'] = $request['ip']; |
||
| 75 | $user['nonce'] = $request['nonce']; |
||
| 76 | $result = signJsConnect($user, $clientID, $secret, $secure, true); |
||
| 77 | $result['v'] = JS_CONNECT_VERSION; |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | $result = array('name' => '', 'photourl' => ''); |
||
| 81 | } |
||
| 82 | |||
| 83 | $content = json_encode($result); |
||
| 84 | |||
| 85 | if (isset($request['callback'])) { |
||
| 86 | $content = "{$request['callback']}($content)"; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (!headers_sent()) { |
||
| 90 | $contentType = jsConnectContentType($request); |
||
| 91 | header($contentType, true); |
||
| 92 | } |
||
| 93 | echo $content; |
||
| 94 | } |
||
| 193 |