1 | <?php |
||
11 | abstract class AbstractToken implements Token |
||
12 | { |
||
13 | /** @var Configuration */ |
||
14 | protected $configuration; |
||
15 | |||
16 | /** @var int */ |
||
17 | private $depth; |
||
18 | |||
19 | /** @var null|Token */ |
||
20 | private $parent; |
||
21 | |||
22 | /** @var string */ |
||
23 | private $type; |
||
24 | |||
25 | /** |
||
26 | * Constructor |
||
27 | */ |
||
28 | 67 | public function __construct($type, Configuration $configuration) |
|
29 | { |
||
30 | 1 | if ($type !== Token::CDATA |
|
31 | 67 | && $type !== Token::COMMENT |
|
32 | 67 | && $type !== Token::DOCTYPE |
|
33 | 67 | && $type !== Token::ELEMENT |
|
34 | 67 | && $type !== Token::TEXT) { |
|
35 | 1 | throw new \InvalidArgumentException('Invalid type: ' . $type); |
|
36 | } |
||
37 | |||
38 | 66 | $this->configuration = $configuration; |
|
39 | 66 | $this->depth = 0; |
|
40 | 66 | $this->parent = null; |
|
41 | 66 | $this->type = $type; |
|
42 | 66 | } |
|
43 | |||
44 | /** |
||
45 | * Required by the Token interface. |
||
46 | */ |
||
47 | 42 | public function getDepth() |
|
51 | |||
52 | /** |
||
53 | * Required by the Token interface. |
||
54 | */ |
||
55 | 22 | public function getParent() |
|
59 | |||
60 | /** |
||
61 | * Chainable setter for 'parent'. |
||
62 | */ |
||
63 | 41 | public function setParent(Token $parent = null) |
|
74 | |||
75 | public function hasAncestor(Element $element) |
||
88 | |||
89 | /** |
||
90 | * Required by the Token interface. |
||
91 | */ |
||
92 | 29 | public function getType() |
|
96 | |||
97 | 47 | public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger) |
|
115 | |||
116 | 5 | public function __toString() |
|
120 | } |
||
121 |
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: