1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\Reflection\Validation\Inheritance; |
11
|
|
|
|
12
|
|
|
use Railt\SDL\Contracts\Behavior\AllowsTypeIndication; |
13
|
|
|
use Railt\SDL\Contracts\Definitions\ScalarDefinition; |
14
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
15
|
|
|
use Railt\SDL\Exceptions\TypeConflictException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ScalarValidator |
19
|
|
|
*/ |
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 |
28
|
|
|
{ |
29
|
813 |
|
return $parent instanceof AllowsTypeIndication && |
30
|
813 |
|
$parent->getTypeDefinition() instanceof ScalarDefinition; |
31
|
|
|
} |
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 |
40
|
|
|
{ |
41
|
795 |
|
if ($child->getTypeDefinition() instanceof ScalarDefinition) { |
|
|
|
|
42
|
793 |
|
$this->validateScalarCompatibility($child, $parent); |
43
|
|
|
} else { |
44
|
2 |
|
$this->throwIncompatibleTypes($child, $parent); |
45
|
|
|
} |
46
|
523 |
|
} |
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 |
55
|
|
|
{ |
56
|
793 |
|
$this->isPostCondition($parent) |
|
|
|
|
57
|
793 |
|
? $this->validateDirectScalarCompatibility($child, $parent) |
58
|
350 |
|
: $this->validateInverseScalarCompatibility($child, $parent) |
59
|
|
|
; |
60
|
523 |
|
} |
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 |
69
|
|
|
{ |
70
|
793 |
|
$parentType = $parent->getTypeDefinition(); |
|
|
|
|
71
|
|
|
|
72
|
793 |
|
if (! ($child->getTypeDefinition() instanceof $parentType)) { |
|
|
|
|
73
|
270 |
|
$this->throwScalarIncompatibilityException($child, $parent); |
74
|
|
|
} |
75
|
523 |
|
} |
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 |
99
|
|
|
{ |
100
|
450 |
|
[$childType, $parentType] = [$child->getTypeDefinition(), $parent->getTypeDefinition()]; |
|
|
|
|
101
|
|
|
|
102
|
450 |
|
$error = \vsprintf('Scalar %s of %s does not compatible with %s of %s', [ |
103
|
450 |
|
$childType, $child, |
104
|
450 |
|
$parentType, $parent, |
|
|
|
|
105
|
|
|
]); |
106
|
|
|
|
107
|
450 |
|
throw new TypeConflictException($error, $this->getCallStack()); |
108
|
|
|
} |
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: