| Conditions | 5 |
| Paths | 4 |
| Total Lines | 25 |
| Code Lines | 15 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 45 | public function loginFormAction() |
||
| 46 | { |
||
| 47 | if (!$this->getRequest()->isPost()) { |
||
|
|
|||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | $username = $this->getRequest()->getPost('username'); |
||
| 52 | $password = $this->getRequest()->getPost('password'); |
||
| 53 | |||
| 54 | if (empty($username) || empty($password)) { |
||
| 55 | $view = new ViewModel(); |
||
| 56 | $view->errorMessage = 'User name or pass cannot be empty.'; |
||
| 57 | return $view; |
||
| 58 | } |
||
| 59 | |||
| 60 | $result = $this->auth->login($username, $password); |
||
| 61 | |||
| 62 | if (!$result->isValid()) { |
||
| 63 | $view = new ViewModel(); |
||
| 64 | $view->errorMessage = $result->getMessages()[0]; |
||
| 65 | return $view; |
||
| 66 | } |
||
| 67 | |||
| 68 | return $this->redirect->toUrl($this->redirectToUrl); |
||
| 69 | } |
||
| 70 | |||
| 77 |
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: