1 | <?php |
||
26 | class Node implements NodeInterface |
||
27 | { |
||
28 | use EntityTrait, IdentifiableEntityTrait; |
||
29 | |||
30 | /** |
||
31 | * Name of this item. |
||
32 | * |
||
33 | * @ODM\Field(type="string") |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $name; |
||
37 | |||
38 | /** |
||
39 | * Value of this item. |
||
40 | * |
||
41 | * Used in select form elements. |
||
42 | * |
||
43 | * @ODM\Field(type="string") |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $value; |
||
47 | |||
48 | /** |
||
49 | * Order priority. |
||
50 | * |
||
51 | * @ODM\Field(type="int") |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $priority = 0; |
||
55 | |||
56 | /** |
||
57 | * Child nodes. |
||
58 | * |
||
59 | * @ODM\ReferenceMany(discriminatorField="_entity", storeAs="dbRef", strategy="set", sort={"priority"="asc"}, cascade="all", orphanRemoval="true") |
||
60 | * @var Collection |
||
61 | */ |
||
62 | protected $children; |
||
63 | |||
64 | /** |
||
65 | * Parent node. |
||
66 | * |
||
67 | * @ODM\ReferenceOne(discriminatorField="_entity", storeAs="dbRef", nullable="true") |
||
68 | * @var |
||
69 | */ |
||
70 | protected $parent; |
||
71 | |||
72 | /** |
||
73 | * Creates a new Tree item. |
||
74 | * |
||
75 | * @param null|string $name |
||
76 | * @param null|string $value |
||
77 | * @param int $priority |
||
78 | */ |
||
79 | public function __construct($name = null, $value = null, $priority = 0) |
||
87 | |||
88 | /** |
||
89 | * Set the name. |
||
90 | * |
||
91 | * @param string $name |
||
92 | * |
||
93 | * @return self |
||
94 | * @throws \InvalidArgumentException if $name is empty. |
||
95 | */ |
||
96 | public function setName($name) |
||
106 | |||
107 | public function getName() |
||
111 | |||
112 | /** |
||
113 | * Set the value. |
||
114 | * |
||
115 | * Used in form selects. |
||
116 | * |
||
117 | * @param string $value |
||
118 | * |
||
119 | * @return self |
||
120 | * @throws \InvalidArgumentException if $value AND {@link name} are empty. |
||
121 | */ |
||
122 | public function setValue($value) |
||
135 | |||
136 | public function getValue() |
||
144 | |||
145 | public function setPriority($priority) |
||
151 | |||
152 | public function getPriority() |
||
156 | |||
157 | public function setChildren(Collection $children) |
||
168 | |||
169 | public function getChildren() |
||
177 | |||
178 | public function hasChildren() |
||
182 | |||
183 | public function addChild(NodeInterface $child) |
||
190 | |||
191 | public function removeChild(NodeInterface $child) |
||
197 | |||
198 | public function clearChildren() |
||
204 | |||
205 | public function setParent(NodeInterface $parent) |
||
211 | |||
212 | public function getParent() |
||
216 | } |
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: