| Conditions | 16 |
| Paths | 66 |
| Total Lines | 84 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 272 |
| 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 |
||
| 5 | public function extractData($columns, $data) { |
||
| 6 | $result = []; |
||
| 7 | foreach ($columns as $id => $metadata_name) { |
||
| 8 | if (!$metadata_name) { |
||
| 9 | continue; |
||
| 10 | } |
||
| 11 | |||
| 12 | $result[$metadata_name] = $data[$id]; |
||
| 13 | } |
||
| 14 | |||
| 15 | return $result; |
||
| 16 | } |
||
| 17 | |||
| 18 | public function registerUser($data) { |
||
| 19 | if (!$data["name"] || !$data["email"]) { |
||
| 20 | return false; |
||
| 21 | } |
||
| 22 | |||
| 23 | $user = false; |
||
| 24 | |||
| 25 | try { |
||
| 26 | $username = Import::generateUniqueUsername($data); |
||
| 27 | $password = generate_random_cleartext_password(); |
||
| 28 | $guid = register_user($username, $password, $data["name"], $data["email"]); |
||
| 29 | elgg_set_user_validation_status($guid, true, "email"); |
||
|
|
|||
| 30 | $user = get_entity($guid); |
||
| 31 | } catch (Exception $e) { |
||
| 32 | elgg_log("Could not register user " . $e->getMessage(), "ERROR"); |
||
| 33 | } |
||
| 34 | |||
| 35 | return $user; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function generateUniqueUsername($data) { |
||
| 39 | $email = $data["email"]; |
||
| 40 | list($name, $email) = explode("@", $email); |
||
| 41 | $name = trim($name); |
||
| 42 | |||
| 43 | $hidden = access_show_hidden_entities(true); |
||
| 44 | |||
| 45 | View Code Duplication | if (get_user_by_username($name)) { |
|
| 46 | $i = 1; |
||
| 47 | |||
| 48 | while (get_user_by_username($name . $i)) { |
||
| 49 | $i++; |
||
| 50 | } |
||
| 51 | |||
| 52 | $result = $name . $i; |
||
| 53 | } else { |
||
| 54 | $result = $name; |
||
| 55 | } |
||
| 56 | |||
| 57 | access_show_hidden_entities($hidden); |
||
| 58 | |||
| 59 | return $result; |
||
| 60 | } |
||
| 61 | |||
| 62 | public function getUserByAttributes($data) { |
||
| 63 | $user = false; |
||
| 64 | |||
| 65 | if ($data["guid"]) { |
||
| 66 | $user = get_entity($data["guid"]); |
||
| 67 | } elseif ($data["username"]) { |
||
| 68 | $user = get_user_by_username($data["username"]); |
||
| 69 | } elseif ($data["email"]) { |
||
| 70 | $users = get_user_by_email($data["email"]); |
||
| 71 | $user = $users[0]; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $user; |
||
| 75 | } |
||
| 76 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.