1 | <?php |
||
21 | class VariableBuilder extends BaseBuilder |
||
22 | { |
||
23 | /** |
||
24 | * @var string[] |
||
25 | */ |
||
26 | private const VARIABLE_DEFINITIONS = [ |
||
27 | 'ConstantDefinition', |
||
28 | 'VariableDefinition', |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * @param RuleInterface $rule |
||
33 | * @return bool |
||
34 | */ |
||
35 | public function match(RuleInterface $rule): bool |
||
39 | |||
40 | /** |
||
41 | * @param ContextInterface $ctx |
||
42 | * @param RuleInterface $rule |
||
43 | * @return \Generator|\Closure |
||
44 | */ |
||
45 | public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator |
||
66 | |||
67 | /** |
||
68 | * @param RuleInterface $rule |
||
69 | * @return iterable|string[] |
||
70 | */ |
||
71 | private function getVariableNames(RuleInterface $rule): iterable |
||
77 | |||
78 | /** |
||
79 | * @param RuleInterface $rule |
||
80 | * @return RuleInterface |
||
81 | */ |
||
82 | private function getValueNode(RuleInterface $rule): RuleInterface |
||
86 | |||
87 | /** |
||
88 | * @param RuleInterface $rule |
||
89 | * @return bool |
||
90 | */ |
||
91 | private function isConstant(RuleInterface $rule): bool |
||
95 | } |
||
96 |
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: