| Conditions | 5 |
| Paths | 8 |
| Total Lines | 31 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 35 | public function save(EntityManager $dbManager = null, $redirectOnDuplicateKey = false) |
||
| 36 | { |
||
| 37 | if ($dbManager !== null) { |
||
| 38 | $this->setEntityManager($dbManager); |
||
| 39 | } |
||
| 40 | |||
| 41 | try { |
||
| 42 | parent::save(); |
||
| 43 | } catch (\PDOException $e) { |
||
| 44 | |||
| 45 | if ($e->getCode() === "23000") { |
||
| 46 | |||
| 47 | if ($redirectOnDuplicateKey) { |
||
| 48 | |||
| 49 | /** @var AbstractControllerService $controller */ |
||
| 50 | $controller = ServiceLocator::instance()->get(AbstractControllerService::class); |
||
| 51 | $uri = $controller->getRequest()->getUri(); |
||
| 52 | |||
| 53 | $controller->getSessionManager()->setFlashMessage('db.duplicate.key', 'Eintrag bereits vorhanden!'); |
||
|
|
|||
| 54 | $controller->redirect($uri); |
||
| 55 | |||
| 56 | } |
||
| 57 | |||
| 58 | return false; |
||
| 59 | |||
| 60 | } |
||
| 61 | |||
| 62 | } |
||
| 63 | |||
| 64 | return $this; |
||
| 65 | } |
||
| 66 | |||
| 67 | } |
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: