1 | <?php |
||
17 | class InArray extends AbstractRule |
||
18 | { |
||
19 | /** |
||
20 | * Get the validation rules |
||
21 | * |
||
22 | * @param \Zend\Validator\ValidatorInterface $validator |
||
23 | * @param \Zend\Form\ElementInterface $element |
||
24 | * |
||
25 | * @return array |
||
26 | */ |
||
27 | public function getRules(ValidatorInterface $validator, ElementInterface $element = null) |
||
37 | |||
38 | /** |
||
39 | * Get the validation message |
||
40 | * |
||
41 | * @param \Zend\Validator\ValidatorInterface $validator |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public function getMessages(ValidatorInterface $validator) |
||
52 | |||
53 | /** |
||
54 | * Whether this rule supports certain validators |
||
55 | * |
||
56 | * @param ValidatorInterface $validator |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function canHandle(ValidatorInterface $validator) |
||
63 | } |
||
64 |
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: