Conditions | 10 |
Paths | 25 |
Total Lines | 73 |
Code Lines | 50 |
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 |
||
50 | public function authenticate(EventInterface $e) |
||
51 | { |
||
52 | $e = $e->getTarget(); |
||
53 | if ($this->isSatisfied()) { |
||
54 | $storage = $this->getStorage()->read(); |
||
55 | $e->setIdentity($storage['identity']) |
||
56 | ->setCode(AuthenticationResult::SUCCESS) |
||
57 | ->setMessages(array('Authentication successful.')); |
||
58 | return; |
||
59 | } |
||
60 | |||
61 | $identity = $e->getRequest()->getPost()->get('identity'); |
||
62 | $credential = $e->getRequest()->getPost()->get('credential'); |
||
63 | $credential = $this->preProcessCredential($credential); |
||
64 | /** @var UserInterface|null $userObject */ |
||
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 | $e->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 | $e->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 | $bcrypt = new Bcrypt(); |
||
99 | $bcrypt->setCost($this->getOptions()->getPasswordCost()); |
||
100 | if (!$bcrypt->verify($credential, $userObject->getPassword())) { |
||
101 | // Password does not match |
||
102 | $e->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID) |
||
103 | ->setMessages(array('Supplied credential is invalid.')); |
||
104 | $this->setSatisfied(false); |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | // regen the id |
||
109 | $session = new SessionContainer($this->getStorage()->getNameSpace()); |
||
110 | $session->getManager()->regenerateId(); |
||
111 | |||
112 | // Success! |
||
113 | $e->setIdentity($userObject->getId()); |
||
114 | // Update user's password hash if the cost parameter has changed |
||
115 | $this->updateUserPasswordHash($userObject, $credential, $bcrypt); |
||
116 | $this->setSatisfied(true); |
||
117 | $storage = $this->getStorage()->read(); |
||
118 | $storage['identity'] = $e->getIdentity(); |
||
119 | $this->getStorage()->write($storage); |
||
120 | $e->setCode(AuthenticationResult::SUCCESS) |
||
121 | ->setMessages(array('Authentication successful.')); |
||
122 | } |
||
123 | |||
234 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.