Conditions | 4 |
Paths | 6 |
Total Lines | 21 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 4 |
CRAP Score | 9.3088 |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
30 | 3 | public function invoke(MethodInvocation $invocation) |
|
31 | { |
||
32 | /* @var Transactional $transactional */ |
||
33 | 3 | $transactional = $invocation->getMethod()->getAnnotation(Transactional::class); |
|
|
|||
34 | 3 | if (count($transactional->value) > 0) { |
|
35 | 3 | return (new PropTransaction)($invocation, $transactional); |
|
36 | } |
||
37 | if (! $this->pdo instanceof ExtendedPdoInterface) { |
||
38 | return $invocation->proceed(); |
||
39 | } |
||
40 | try { |
||
41 | $this->pdo->beginTransaction(); |
||
42 | $result = $invocation->proceed(); |
||
43 | $this->pdo->commit(); |
||
44 | } catch (\PDOException $e) { |
||
45 | $this->pdo->rollBack(); |
||
46 | throw new RollbackException($e->getMessage(), 0, $e); |
||
47 | } |
||
48 | |||
49 | return $result; |
||
50 | } |
||
51 | } |
||
52 |
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 sub-classes 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 parent class: