1 | <?php |
||
17 | class BuilderBag |
||
18 | { |
||
19 | /** |
||
20 | * @var BuilderInterface[] |
||
21 | */ |
||
22 | private $bag = []; |
||
23 | |||
24 | /** |
||
25 | * @param BuilderInterface[] $builders |
||
26 | */ |
||
27 | public function __construct($builders = []) |
||
33 | |||
34 | /** |
||
35 | * Adds a builder. |
||
36 | * |
||
37 | * @param BuilderInterface $builder |
||
38 | * |
||
39 | * @return string |
||
40 | */ |
||
41 | public function add(BuilderInterface $builder) |
||
53 | |||
54 | /** |
||
55 | * Checks if builder exists by a specific name. |
||
56 | * |
||
57 | * @param string $name Builder name. |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | public function has($name) |
||
65 | |||
66 | /** |
||
67 | * Removes a builder by name. |
||
68 | * |
||
69 | * @param string $name Builder name. |
||
70 | */ |
||
71 | public function remove($name) |
||
75 | |||
76 | /** |
||
77 | * Clears contained builders. |
||
78 | */ |
||
79 | public function clear() |
||
83 | |||
84 | /** |
||
85 | * Returns a builder by name. |
||
86 | * |
||
87 | * @param string $name Builder name. |
||
88 | * |
||
89 | * @return BuilderInterface |
||
90 | */ |
||
91 | public function get($name) |
||
95 | |||
96 | /** |
||
97 | * Returns all builders contained. |
||
98 | * |
||
99 | * @param string|null $type Builder type. |
||
100 | * |
||
101 | * @return BuilderInterface[] |
||
102 | */ |
||
103 | public function all($type = null) |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function toArray() |
||
126 | } |
||
127 |
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: