1 | <?php declare(strict_types=1); |
||
17 | class Author extends BaseAuthor |
||
18 | { |
||
19 | const NODE_NAME = 'author'; |
||
20 | |||
21 | /** |
||
22 | * @param NodeInterface $node |
||
23 | * @param \DOMElement $element |
||
24 | * @return mixed |
||
25 | */ |
||
26 | 3 | public function setProperty(NodeInterface $node, \DOMElement $element) : void |
|
27 | { |
||
28 | 3 | if ($node instanceof ItemInterface) { |
|
29 | 3 | $author = $node->newAuthor(); |
|
30 | 3 | $author->setName($this->getChildValue($element, 'name')); |
|
31 | 3 | $author->setUri($this->getChildValue($element, 'uri')); |
|
32 | 3 | $author->setEmail($this->getChildValue($element, 'email')); |
|
33 | 3 | $node->setAuthor($author); |
|
34 | } |
||
35 | 3 | } |
|
36 | |||
37 | /** |
||
38 | * @inheritDoc |
||
39 | */ |
||
40 | 1 | protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
|
49 | } |
||
50 |
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: