1 | <?php |
||
23 | trait ComponentStateTrait |
||
24 | { |
||
25 | use RoutingAwareTrait; |
||
26 | |||
27 | /** |
||
28 | * @var \stdClass |
||
29 | */ |
||
30 | private $state; |
||
31 | |||
32 | /** |
||
33 | * @TODO concept |
||
34 | * @var NodeInterface[] |
||
35 | */ |
||
36 | private $nodes = []; |
||
37 | |||
38 | /** |
||
39 | * @return RenderEvent |
||
40 | */ |
||
41 | abstract protected function getRenderEvent(); |
||
42 | |||
43 | /** |
||
44 | * TODO concept |
||
45 | * @param DispatchEvent $event |
||
46 | */ |
||
47 | public function onDispatchState(DispatchEvent $event) |
||
54 | |||
55 | /** |
||
56 | * @TODO concept |
||
57 | * @param string $name Node name |
||
58 | * @param string|\Closure $method |
||
59 | * @return $this |
||
60 | */ |
||
61 | public function onStateChange($name, $method) |
||
86 | |||
87 | /** |
||
88 | * @return \stdClass |
||
89 | */ |
||
90 | public function getState() |
||
98 | |||
99 | /** |
||
100 | * @param \stdClass $state |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setState(\stdClass $state) |
||
108 | |||
109 | /** |
||
110 | * @param array|\stdClass $state |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function mergeState($state) |
||
118 | |||
119 | /** |
||
120 | * @param \stdClass $state |
||
121 | */ |
||
122 | public function initState(\stdClass $state) |
||
125 | |||
126 | /** |
||
127 | * @param string $name |
||
128 | * @return ComponentNode |
||
129 | */ |
||
130 | // protected function getNode($name) |
||
131 | // { |
||
132 | // if (empty($this->nodes[$name])) { |
||
133 | // $this->nodes[$name] = new ComponentNode($this->getRenderEvent()->getNode($name), $this); |
||
134 | // } |
||
135 | // return $this->nodes[$name]; |
||
136 | // } |
||
137 | |||
138 | /** |
||
139 | * @TODO concept |
||
140 | */ |
||
141 | public function __clone() |
||
145 | } |
||
146 |
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: