Conditions | 11 |
Paths | 33 |
Total Lines | 71 |
Code Lines | 48 |
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 |
||
52 | public function authenticate(AuthenticationEvent $event) |
||
53 | { |
||
54 | if ($this->isSatisfied()) { |
||
55 | $storage = $this->getStorage()->read(); |
||
56 | $event->setIdentity($storage['identity']) |
||
57 | ->setCode(AuthenticationResult::SUCCESS) |
||
58 | ->setMessages(array('Authentication successful.')); |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | $identity = $event->getRequest()->getPost()->get('identity'); |
||
|
|||
63 | $credential = $event->getRequest()->getPost()->get('credential'); |
||
64 | $credential = $this->preProcessCredential($credential); |
||
65 | $userObject = null; |
||
66 | |||
67 | // Cycle through the configured identity sources and test each |
||
68 | $fields = $this->getOptions()->getAuthIdentityFields(); |
||
69 | while (!is_object($userObject) && count($fields) > 0) { |
||
70 | $mode = array_shift($fields); |
||
71 | switch ($mode) { |
||
72 | case 'username': |
||
73 | $userObject = $this->getMapper()->findByUsername($identity); |
||
74 | break; |
||
75 | case 'email': |
||
76 | $userObject = $this->getMapper()->findByEmail($identity); |
||
77 | break; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | if (!$userObject) { |
||
82 | $event->setCode(AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND) |
||
83 | ->setMessages(array('A record with the supplied identity could not be found.')); |
||
84 | $this->setSatisfied(false); |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | if ($this->getOptions()->getEnableUserState()) { |
||
89 | // Don't allow user to login if state is not in allowed list |
||
90 | if (!in_array($userObject->getState(), $this->getOptions()->getAllowedLoginStates())) { |
||
91 | $event->setCode(AuthenticationResult::FAILURE_UNCATEGORIZED) |
||
92 | ->setMessages(array('A record with the supplied identity is not active.')); |
||
93 | $this->setSatisfied(false); |
||
94 | return false; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | $cryptoService = $this->getHydrator()->getCryptoService(); |
||
99 | if (!$cryptoService->verify($credential, $userObject->getPassword())) { |
||
100 | // Password does not match |
||
101 | $event->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID) |
||
102 | ->setMessages(array('Supplied credential is invalid.')); |
||
103 | $this->setSatisfied(false); |
||
104 | return false; |
||
105 | } elseif ($cryptoService instanceof Bcrypt) { |
||
106 | // Update user's password hash if the cost parameter has changed |
||
107 | $this->updateUserPasswordHash($userObject, $credential, $cryptoService); |
||
108 | } |
||
109 | |||
110 | // regen the id |
||
111 | SessionContainer::getDefaultManager()->regenerateId(); |
||
112 | |||
113 | // Success! |
||
114 | $event->setIdentity($userObject->getId()); |
||
115 | |||
116 | $this->setSatisfied(true); |
||
117 | $storage = $this->getStorage()->read(); |
||
118 | $storage['identity'] = $event->getIdentity(); |
||
119 | $this->getStorage()->write($storage); |
||
120 | $event->setCode(AuthenticationResult::SUCCESS) |
||
121 | ->setMessages(array('Authentication successful.')); |
||
122 | } |
||
123 | |||
258 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: