1 | <?php |
||
17 | class StringLength extends AbstractRule |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritDoc} |
||
21 | */ |
||
22 | public function getRules(ValidatorInterface $validator, ElementInterface $element = null) |
||
34 | |||
35 | /** |
||
36 | * {@inheritDoc} |
||
37 | */ |
||
38 | public function getMessages(ValidatorInterface $validator) |
||
50 | |||
51 | /** |
||
52 | * Whether this rule supports certain validators |
||
53 | * |
||
54 | * @param ValidatorInterface $validator |
||
55 | * @return mixed |
||
56 | */ |
||
57 | public function canHandle(ValidatorInterface $validator) |
||
61 | } |
||
62 |
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: