1 | <?php |
||
19 | class NodeDefinitionBuilder implements DefinitionInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $nodeMapping = array(); |
||
25 | |||
26 | /** |
||
27 | * @var DefinitionInterface|NodeDefinitionContainerInterface|null |
||
28 | */ |
||
29 | protected $parent; |
||
30 | |||
31 | /** |
||
32 | * Constructor. |
||
33 | */ |
||
34 | public function __construct() |
||
46 | |||
47 | /** |
||
48 | * Set node class. |
||
49 | * |
||
50 | * @param string $type |
||
51 | * @param string $class |
||
52 | */ |
||
53 | public static function setNodeClass($type, $class) |
||
57 | |||
58 | /** |
||
59 | * Get node class by type. |
||
60 | * |
||
61 | * @param string $type |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public static function getNodeClass($type) |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function setParent($parent = null) |
||
102 | |||
103 | /** |
||
104 | * @return DefinitionInterface|NodeDefinitionContainerInterface|null |
||
105 | */ |
||
106 | public function end() |
||
110 | |||
111 | /** |
||
112 | * Append node to parent. |
||
113 | * |
||
114 | * @param NodeDefinitionInterface $node |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function append(NodeDefinitionInterface $node) |
||
133 | |||
134 | /** |
||
135 | * Create variable definition. |
||
136 | * |
||
137 | * @param string $name |
||
138 | * |
||
139 | * @return VariableDefinition |
||
140 | */ |
||
141 | public function variableNode($name) |
||
145 | |||
146 | /** |
||
147 | * Create scalar definition. |
||
148 | * |
||
149 | * @param string $name |
||
150 | * |
||
151 | * @return ScalarDefinition |
||
152 | */ |
||
153 | public function scalarNode($name) |
||
157 | |||
158 | /** |
||
159 | * Create boolean definition. |
||
160 | * |
||
161 | * @param string $name |
||
162 | * |
||
163 | * @return BooleanDefinition |
||
164 | */ |
||
165 | public function booleanNode($name) |
||
169 | |||
170 | /** |
||
171 | * Create integer definition. |
||
172 | * |
||
173 | * @param string $name |
||
174 | * |
||
175 | * @return IntegerDefinition |
||
176 | */ |
||
177 | public function integerNode($name) |
||
181 | |||
182 | /** |
||
183 | * Create float definition. |
||
184 | * |
||
185 | * @param string $name |
||
186 | * |
||
187 | * @return FloatDefinition |
||
188 | */ |
||
189 | public function floatNode($name) |
||
193 | |||
194 | /** |
||
195 | * Create enum definition. |
||
196 | * |
||
197 | * @param string $name |
||
198 | * @param array $values |
||
199 | * |
||
200 | * @return EnumDefinition |
||
201 | */ |
||
202 | public function enumNode($name, array $values) |
||
209 | |||
210 | /** |
||
211 | * Create array definition. |
||
212 | * |
||
213 | * @param string $name |
||
214 | * |
||
215 | * @return ArrayDefinition |
||
216 | */ |
||
217 | public function arrayNode($name) |
||
221 | |||
222 | /** |
||
223 | * Copy definition. |
||
224 | * |
||
225 | * @param string $name |
||
226 | * @param NodeDefinitionInterface $template |
||
227 | * |
||
228 | * @return NodeDefinitionInterface |
||
229 | */ |
||
230 | public function copyNode($name, NodeDefinitionInterface $template) |
||
238 | |||
239 | /** |
||
240 | * Build node by type. |
||
241 | * |
||
242 | * @param string $name |
||
243 | * @param string $type |
||
244 | * |
||
245 | * @return NodeDefinitionInterface |
||
246 | */ |
||
247 | public function node($name, $type) |
||
256 | } |
||
257 |
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: