| 1 | <?php |
||
| 17 | class Identical extends AbstractRule |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * {@inheritDoc} |
||
| 21 | */ |
||
| 22 | public function getRules(ValidatorInterface $validator, ElementInterface $element = null) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritDoc} |
||
| 35 | */ |
||
| 36 | public function getMessages(ValidatorInterface $validator) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Whether this rule supports certain validators |
||
| 43 | * |
||
| 44 | * @param ValidatorInterface $validator |
||
| 45 | * @return mixed |
||
| 46 | */ |
||
| 47 | public function canHandle(ValidatorInterface $validator) |
||
| 51 | } |
||
| 52 |
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: