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 int */ |
||
20 | private $line; |
||
21 | |||
22 | /** @var null|Token */ |
||
23 | private $parent; |
||
24 | |||
25 | /** @var int */ |
||
26 | private $position; |
||
27 | |||
28 | /** |
||
29 | * Constructor |
||
30 | */ |
||
31 | 202 | public function __construct(Configuration $configuration, int $line, int $position) |
|
39 | |||
40 | /** |
||
41 | * Required by the Token interface. |
||
42 | */ |
||
43 | 141 | public function getDepth() : int |
|
47 | |||
48 | /** |
||
49 | * Getter for 'line'. |
||
50 | */ |
||
51 | 92 | public function getLine() : int |
|
55 | |||
56 | /** |
||
57 | * Required by the Token interface. |
||
58 | */ |
||
59 | 80 | public function getParent() |
|
63 | |||
64 | /** |
||
65 | * Chainable setter for 'parent'. |
||
66 | */ |
||
67 | 140 | public function setParent(Token $parent = null) |
|
76 | |||
77 | 18 | public function hasAncestor(Element $element) : bool |
|
90 | |||
91 | /** |
||
92 | * Getter for 'position'. |
||
93 | */ |
||
94 | 92 | public function getPosition() : int |
|
98 | |||
99 | 150 | public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger) |
|
116 | |||
117 | abstract public function getType() : string; |
||
118 | |||
119 | 13 | public function __toString() |
|
124 | } |
||
125 |
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: