Conditions | 4 |
Paths | 6 |
Total Lines | 51 |
Code Lines | 31 |
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 |
||
69 | public function loadUserByOAuthUserResponse(UserResponseInterface $response) |
||
70 | { |
||
71 | $username = $response->getUsername(); |
||
72 | $user = $this->userManager->findUserBy([$this->getProperty($response) => $username]); |
||
73 | |||
74 | // Email is mandatory, we fill it up with random thing first |
||
75 | $email = uniqid('badger'); |
||
76 | if (null !== $response->getEmail()) { |
||
77 | $email = $response->getEmail(); |
||
78 | } |
||
79 | |||
80 | // When the user is registering |
||
81 | if (null === $user) { |
||
82 | $service = $response->getResourceOwner()->getName(); |
||
83 | $setter = 'set' . ucfirst($service); |
||
84 | $setter_id = $setter . 'Id'; |
||
85 | $setter_token = $setter . 'AccessToken'; |
||
86 | |||
87 | // Create new user here |
||
88 | $user = $this->userManager->createUser(); |
||
89 | $user->$setter_id($username); |
||
90 | $user->$setter_token($response->getAccessToken()); |
||
91 | |||
92 | $user->setUsername($response->getNickname()); |
||
93 | $user->setEmail($email); |
||
94 | $user->setPassword($username); // TODO: change |
||
95 | $user->setProfilePicture($response->getProfilePicture()); |
||
96 | $user->setEnabled(true); |
||
97 | $user->setDateRegistered(new \DateTime()); |
||
98 | $user->setNuts(0); |
||
99 | |||
100 | $tag = $this->tagRepository->findOneBy(['isDefault' => true]); |
||
101 | if (null !== $tag) { |
||
102 | $user->addTag($tag); |
||
103 | } |
||
104 | |||
105 | $this->userManager->updateUser($user); |
||
106 | |||
107 | return $user; |
||
108 | } |
||
109 | |||
110 | // If user exists - go with the HWIOAuth way |
||
111 | $user = parent::loadUserByOAuthUserResponse($response); |
||
112 | $serviceName = $response->getResourceOwner()->getName(); |
||
113 | $setter = 'set' . ucfirst($serviceName) . 'AccessToken'; |
||
114 | |||
115 | // Update access token |
||
116 | $user->$setter($response->getAccessToken()); |
||
117 | |||
118 | return $user; |
||
119 | } |
||
120 | } |
||
121 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.