Conditions | 4 |
Paths | 4 |
Total Lines | 19 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Tests | 11 |
CRAP Score | 4 |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
39 | 4 | public function simplify(AbstractNode $node) { |
|
40 | 4 | if(!$this->isSimplifierFor($node)) { |
|
41 | 1 | throw new InvalidArgumentException('FirstNodeSimplifier can only simplify FirstNode'); |
|
42 | } |
||
43 | |||
44 | 3 | $nodeSimplifier = $this->simplifierFactory->newNodeSimplifier(); |
|
45 | 3 | $operand = $nodeSimplifier->simplify($node->getOperand()); |
|
|
|||
46 | |||
47 | 3 | if($operand instanceof ResourceListNode) { |
|
48 | 2 | if($operand->isEmpty()) { |
|
49 | 1 | return $operand; |
|
50 | } else { |
||
51 | 1 | $resources = $operand->toArray(); |
|
52 | 1 | return new ResourceListNode(array(reset($resources))); |
|
53 | } |
||
54 | } else { |
||
55 | 1 | return new FirstNode($operand); |
|
56 | } |
||
57 | } |
||
58 | } |
||
59 |
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: