1 | <?php |
||
38 | class FormObjectFactory implements SingletonInterface |
||
39 | { |
||
40 | use ExtendedSelfInstantiateTrait; |
||
41 | |||
42 | /** |
||
43 | * @var FormObject[] |
||
44 | */ |
||
45 | protected $instances = []; |
||
46 | |||
47 | /** |
||
48 | * @var FormObjectStatic[] |
||
49 | */ |
||
50 | protected $static = []; |
||
51 | |||
52 | /** |
||
53 | * @var FormObjectProxy[] |
||
54 | */ |
||
55 | protected $proxy = []; |
||
56 | |||
57 | /** |
||
58 | * @param FormInterface $form |
||
59 | * @param string $name |
||
60 | * @return FormObject |
||
61 | */ |
||
62 | public function getInstanceWithFormInstance(FormInterface $form, $name = 'defaultName') |
||
73 | |||
74 | /** |
||
75 | * Will create an instance of `FormObject` based on a class that implements |
||
76 | * the interface `FormInterface`. |
||
77 | * |
||
78 | * @param string $className |
||
79 | * @param string $name |
||
80 | * @return FormObject |
||
81 | */ |
||
82 | public function getInstanceWithClassName($className, $name) |
||
89 | |||
90 | /** |
||
91 | * Returns the proxy object for the given form object and form instance. |
||
92 | * |
||
93 | * Please use with caution, as this is a very low level function! |
||
94 | * |
||
95 | * @param FormInterface $form |
||
96 | * @return FormObjectProxy |
||
97 | */ |
||
98 | public function getProxy(FormInterface $form) |
||
108 | |||
109 | /** |
||
110 | * @param string $className |
||
111 | * @return FormObjectStatic |
||
112 | * @throws ClassNotFoundException |
||
113 | * @throws InvalidArgumentTypeException |
||
114 | */ |
||
115 | protected function getStaticInstance($className) |
||
146 | |||
147 | /** |
||
148 | * Adds the given form configuration to the global FormZ configuration |
||
149 | * object. |
||
150 | * |
||
151 | * @param FormObjectStatic $static |
||
152 | */ |
||
153 | public function addToGlobalConfiguration(FormObjectStatic $static) |
||
159 | |||
160 | /** |
||
161 | * @param string $className |
||
162 | * @return string |
||
163 | */ |
||
164 | protected function getCacheIdentifier($className) |
||
170 | |||
171 | /** |
||
172 | * Wrapper for unit tests. |
||
173 | * |
||
174 | * @param string $className |
||
175 | * @return FormObjectStatic |
||
176 | */ |
||
177 | protected function buildStaticInstance($className) |
||
184 | |||
185 | /** |
||
186 | * Wrapper for unit tests. |
||
187 | * |
||
188 | * @param FormInterface $form |
||
189 | * @return FormObjectProxy |
||
190 | */ |
||
191 | protected function getNewProxyInstance(FormInterface $form) |
||
200 | |||
201 | /** |
||
202 | * @return Configuration|ConfigurationObjectInterface |
||
203 | */ |
||
204 | protected function getGlobalConfiguration() |
||
208 | } |
||
209 |
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: