Conditions | 6 |
Paths | 17 |
Total Lines | 51 |
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 |
||
35 | public function resolveCreatedByToUserId(StringLiteral $createdByIdentifier): ?StringLiteral |
||
36 | { |
||
37 | try { |
||
38 | // If the createdby is a UUID, return it immediately. |
||
39 | UUID::fromNative($createdByIdentifier->toNative()); |
||
40 | return $createdByIdentifier; |
||
41 | } catch (InvalidNativeArgumentException $exception) { |
||
42 | $this->logger->info( |
||
43 | 'The provided createdByIdentifier ' . $createdByIdentifier->toNative() . ' is not a UUID.', |
||
44 | [ |
||
45 | 'exception' => $exception, |
||
46 | ] |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | try { |
||
51 | // If the createdby is not a UUID, it might still be an Auth0 or social id. |
||
52 | $user = $this->users->getUserById($createdByIdentifier); |
||
53 | if ($user instanceof UserIdentityDetails) { |
||
54 | return $user->getUserId(); |
||
55 | } |
||
56 | |||
57 | // If no user was found with the createdby as id, check if it's an email and look up the user that way. |
||
58 | // Otherwise look it up as a username. |
||
59 | try { |
||
60 | $email = new EmailAddress($createdByIdentifier->toNative()); |
||
61 | $user = $this->users->getUserByEmail($email); |
||
62 | } catch (InvalidNativeArgumentException $e) { |
||
63 | $user = $this->users->getUserByNick($createdByIdentifier); |
||
64 | } |
||
65 | if ($user instanceof UserIdentityDetails) { |
||
66 | return $user->getUserId(); |
||
67 | } |
||
68 | } catch (Exception $e) { |
||
69 | $this->logger->error( |
||
70 | sprintf( |
||
71 | 'An unexpected error occurred while resolving user with identifier %s', |
||
72 | $createdByIdentifier |
||
73 | ), |
||
74 | [ |
||
75 | 'exception' => $e, |
||
76 | ] |
||
77 | ); |
||
78 | } |
||
79 | |||
80 | $this->logger->warning( |
||
81 | 'Unable to find user with identifier ' . $createdByIdentifier |
||
82 | ); |
||
83 | |||
84 | return null; |
||
85 | } |
||
86 | } |
||
87 |