1 | <?php |
||
22 | abstract class AbstractInnerHit implements BuilderInterface |
||
23 | { |
||
24 | use ParametersTrait; |
||
25 | use NameAwareTrait; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $path; |
||
31 | |||
32 | /** |
||
33 | * @var BuilderInterface |
||
34 | */ |
||
35 | private $query; |
||
36 | |||
37 | /** |
||
38 | * @var BuilderBag |
||
39 | */ |
||
40 | private $innerHits; |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | abstract public function toArray(); |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | abstract public function getType(); |
||
51 | |||
52 | /** |
||
53 | * Inner hits container init. |
||
54 | * |
||
55 | * @param string $name |
||
56 | * @param string $path |
||
57 | * @param BuilderInterface $query |
||
58 | */ |
||
59 | public function __construct($name, $path, BuilderInterface $query) |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getPath() |
||
73 | |||
74 | /** |
||
75 | * @param string $path |
||
76 | */ |
||
77 | public function setPath($path) |
||
81 | |||
82 | /** |
||
83 | * @return BuilderInterface |
||
84 | */ |
||
85 | public function getQuery() |
||
89 | |||
90 | /** |
||
91 | * @param BuilderInterface $query |
||
92 | */ |
||
93 | public function setQuery(BuilderInterface $query) |
||
97 | |||
98 | /** |
||
99 | * Adds a sub-innerHit. |
||
100 | * |
||
101 | * @param AbstractInnerHit $abstractInnerHit |
||
102 | */ |
||
103 | public function addInnerHit(AbstractInnerHit $abstractInnerHit) |
||
111 | |||
112 | /** |
||
113 | * Returns all sub inner hits. |
||
114 | * |
||
115 | * @return BuilderInterface[] |
||
116 | */ |
||
117 | public function getInnerHits() |
||
125 | |||
126 | /** |
||
127 | * Returns sub inner hit. |
||
128 | * @param string $name inner hit name to return. |
||
129 | * |
||
130 | * @return AbstractInnerHit|null |
||
131 | */ |
||
132 | public function getInnerHit($name) |
||
140 | |||
141 | /** |
||
142 | * Process all nested inner hits. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function collectNestedInnerHits() |
||
156 | } |
||
157 |
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: