1 | <?php |
||
20 | class ScalarValidator extends BaseInheritanceValidator |
||
21 | { |
||
22 | /** |
||
23 | * @param AllowsTypeIndication|TypeDefinition $child |
||
24 | * @param AllowsTypeIndication|TypeDefinition $parent |
||
25 | * @return bool |
||
26 | */ |
||
27 | 813 | public function match(TypeDefinition $child, TypeDefinition $parent): bool |
|
32 | |||
33 | /** |
||
34 | * @param AllowsTypeIndication|TypeDefinition $child |
||
35 | * @param AllowsTypeIndication|TypeDefinition $parent |
||
36 | * @return void |
||
37 | * @throws TypeConflictException |
||
38 | */ |
||
39 | 795 | public function validate(TypeDefinition $child, TypeDefinition $parent): void |
|
47 | |||
48 | /** |
||
49 | * @param AllowsTypeIndication|TypeDefinition $child |
||
50 | * @param AllowsTypeIndication|TypeDefinition $parent |
||
51 | * @return void |
||
52 | * @throws TypeConflictException |
||
53 | */ |
||
54 | 793 | private function validateScalarCompatibility(TypeDefinition $child, TypeDefinition $parent): void |
|
61 | |||
62 | /** |
||
63 | * @param AllowsTypeIndication|TypeDefinition $child |
||
64 | * @param AllowsTypeIndication|TypeDefinition $parent |
||
65 | * @return void |
||
66 | * @throws TypeConflictException |
||
67 | */ |
||
68 | 793 | private function validateDirectScalarCompatibility(TypeDefinition $child, TypeDefinition $parent): void |
|
76 | |||
77 | /** |
||
78 | * @param AllowsTypeIndication|TypeDefinition $child |
||
79 | * @param AllowsTypeIndication|TypeDefinition $parent |
||
80 | * @return void |
||
81 | * @throws TypeConflictException |
||
82 | */ |
||
83 | 350 | private function validateInverseScalarCompatibility(TypeDefinition $child, TypeDefinition $parent): void |
|
84 | { |
||
85 | 350 | $childType = $child->getTypeDefinition(); |
|
86 | |||
87 | 350 | if (! ($parent->getTypeDefinition() instanceof $childType)) { |
|
88 | 180 | $this->throwScalarIncompatibilityException($child, $parent); |
|
89 | } |
||
90 | 170 | } |
|
91 | |||
92 | /** |
||
93 | * @param AllowsTypeIndication|TypeDefinition $child |
||
94 | * @param AllowsTypeIndication|TypeDefinition $parent |
||
95 | * @return void |
||
96 | * @throws TypeConflictException |
||
97 | */ |
||
98 | 450 | private function throwScalarIncompatibilityException(TypeDefinition $child, TypeDefinition $parent): void |
|
109 | } |
||
110 |
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: