1 | <?php |
||
12 | abstract class AbstractToken implements Token |
||
13 | { |
||
14 | /** @var Configuration */ |
||
15 | protected $configuration; |
||
16 | |||
17 | /** @var int */ |
||
18 | private $depth; |
||
19 | |||
20 | /** @var null|Token */ |
||
21 | private $parent; |
||
22 | |||
23 | /** @var string */ |
||
24 | private $type; |
||
25 | |||
26 | /** |
||
27 | * Constructor |
||
28 | */ |
||
29 | 65 | public function __construct($type, Configuration $configuration) |
|
44 | |||
45 | /** |
||
46 | * Required by the Token interface. |
||
47 | */ |
||
48 | 40 | public function getDepth() |
|
52 | |||
53 | /** |
||
54 | * Required by the Token interface. |
||
55 | */ |
||
56 | 20 | public function getParent() |
|
60 | |||
61 | /** |
||
62 | * Chainable setter for 'parent'. |
||
63 | */ |
||
64 | 39 | public function setParent(Token $parent = null) |
|
75 | |||
76 | public function hasAncestor(Element $element) |
||
89 | |||
90 | /** |
||
91 | * Required by the Token interface. |
||
92 | */ |
||
93 | 27 | public function getType() |
|
97 | |||
98 | 45 | public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger = null) |
|
119 | |||
120 | 5 | 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: