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