1 | <?php |
||
20 | trait TraversalTrait{ |
||
21 | |||
22 | /** |
||
23 | * @link http://php.net/manual/class.domnode.php#domnode.props.ownerdocument |
||
24 | * |
||
25 | * @var \chillerlan\PrototypeDOM\Document |
||
26 | */ |
||
27 | public $ownerDocument; |
||
28 | |||
29 | /** |
||
30 | * @param string $property |
||
31 | * @param int $maxLength |
||
32 | * @param int $nodeType |
||
33 | * |
||
34 | * @return \chillerlan\PrototypeDOM\NodeList |
||
35 | */ |
||
36 | 2 | public function recursivelyCollect(string $property, int $maxLength = -1, int $nodeType = XML_ELEMENT_NODE):NodeList{ |
|
40 | |||
41 | /** |
||
42 | * @param $selector |
||
43 | * @param $index |
||
44 | * @param string $property |
||
45 | * @param int $nodeType |
||
46 | * |
||
47 | * @return \DOMNode|null |
||
48 | */ |
||
49 | 5 | public function _recursivelyFind($selector, $index, string $property, int $nodeType = XML_ELEMENT_NODE){ |
|
59 | |||
60 | /** |
||
61 | * @param bool $xml |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | 3 | public function inspect($xml = false):string{ |
|
68 | |||
69 | /** |
||
70 | * @param string|array $selectors |
||
71 | * |
||
72 | * @return \chillerlan\PrototypeDOM\NodeList |
||
73 | */ |
||
74 | 4 | public function select($selectors = null):NodeList{ |
|
77 | |||
78 | /** |
||
79 | * @param string $selector |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | 2 | public function match(string $selector):bool{ |
|
87 | |||
88 | /** |
||
89 | * @param null $expression |
||
90 | * @param int $index |
||
91 | * |
||
92 | * @return \DOMNode|null |
||
93 | */ |
||
94 | 2 | public function down($expression = null, int $index = 0){ |
|
107 | |||
108 | /** |
||
109 | * @param string|null $expression |
||
110 | * @param int|null $index |
||
111 | * |
||
112 | * @return \DOMNode|null |
||
113 | */ |
||
114 | 2 | public function up($expression = null, int $index = null){ |
|
117 | |||
118 | /** |
||
119 | * @param string|null $expression |
||
120 | * @param int|null $index |
||
121 | * |
||
122 | * @return \DOMNode|null |
||
123 | */ |
||
124 | 1 | public function previous($expression = null, int $index = null){ |
|
127 | |||
128 | /** |
||
129 | * @param string|null $expression |
||
130 | * @param int|null $index |
||
131 | * |
||
132 | * @return \DOMNode|null |
||
133 | */ |
||
134 | 3 | public function next($expression = null, int $index = null){ |
|
137 | |||
138 | /** |
||
139 | * @param int $nodeType |
||
140 | * |
||
141 | * @return \chillerlan\PrototypeDOM\NodeList |
||
142 | */ |
||
143 | 1 | public function childElements(int $nodeType = XML_ELEMENT_NODE):NodeList{ |
|
160 | |||
161 | /** |
||
162 | * @param \DOMNode $ancestor |
||
163 | * |
||
164 | * @return bool |
||
165 | */ |
||
166 | 1 | public function descendantOf(DOMNode $ancestor):bool{ |
|
169 | |||
170 | /** |
||
171 | * @return \chillerlan\PrototypeDOM\NodeList |
||
172 | */ |
||
173 | 2 | public function ancestors():NodeList{ |
|
176 | |||
177 | /** |
||
178 | * @return \chillerlan\PrototypeDOM\NodeList |
||
179 | */ |
||
180 | 1 | public function siblings():NodeList{ |
|
183 | |||
184 | /** |
||
185 | * @return \chillerlan\PrototypeDOM\NodeList |
||
186 | */ |
||
187 | 4 | public function descendants():NodeList{ |
|
190 | |||
191 | /** |
||
192 | * @return \DOMNode|null |
||
193 | */ |
||
194 | 3 | public function firstDescendant(){ |
|
197 | |||
198 | /** |
||
199 | * @return \chillerlan\PrototypeDOM\NodeList |
||
200 | */ |
||
201 | 1 | public function previousSiblings():NodeList{ |
|
204 | |||
205 | /** |
||
206 | * @return \chillerlan\PrototypeDOM\NodeList |
||
207 | */ |
||
208 | 1 | public function nextSiblings():NodeList{ |
|
211 | |||
212 | } |
||
213 |
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 sub-classes 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 parent class: