1 | <?php |
||
21 | class AllowedCommandValidator extends ConstraintValidator |
||
22 | { |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $allowedPrefixes; |
||
27 | |||
28 | /** |
||
29 | * Constructor class. |
||
30 | * |
||
31 | * @param array $allowedPrefixes List of allowed prefixes. |
||
32 | */ |
||
33 | public function __construct(array $allowedPrefixes = []) |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public function validate($property, Constraint $constraint) |
||
58 | } |
||
59 |
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: