1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Object; |
4
|
|
|
|
5
|
|
|
use Dgame\Type\TypeFactory; |
6
|
|
|
use ReflectionMethod; |
7
|
|
|
use ReflectionParameter; |
8
|
|
|
use Throwable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Validator |
12
|
|
|
* @package Dgame\Object |
13
|
|
|
*/ |
14
|
|
|
final class Validator |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ObjectFacade |
18
|
|
|
*/ |
19
|
|
|
private $facade; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Validator constructor. |
23
|
|
|
* |
24
|
|
|
* @param ObjectFacade $facade |
25
|
|
|
*/ |
26
|
4 |
|
public function __construct(ObjectFacade $facade) |
27
|
|
|
{ |
28
|
4 |
|
$this->facade = $facade; |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ObjectFacade $facade |
33
|
|
|
* |
34
|
|
|
* @return Validator |
|
|
|
|
35
|
|
|
*/ |
36
|
4 |
|
public static function new(ObjectFacade $facade): self |
37
|
|
|
{ |
38
|
4 |
|
return new self($facade); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ReflectionMethod $method |
43
|
|
|
* @param mixed $value |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
3 |
|
*/ |
47
|
|
|
public function isValidSetterMethod(ReflectionMethod $method, $value): bool |
48
|
3 |
|
{ |
49
|
|
|
if (!$method->isPublic()) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($method->getNumberOfParameters() === 0) { |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
2 |
|
|
57
|
|
|
if ($method->getNumberOfRequiredParameters() > 1) { |
58
|
2 |
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($value === null) { |
62
|
|
|
return $method->getParameters()[0]->allowsNull(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->isValidParameterValue($method->getParameters()[0], $value); |
66
|
|
|
} |
67
|
1 |
|
|
68
|
|
|
/** |
69
|
1 |
|
* @param ReflectionParameter $parameter |
70
|
|
|
* @param mixed $value |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
1 |
|
*/ |
74
|
1 |
|
public function isValidParameterValue(ReflectionParameter $parameter, $value): bool |
75
|
|
|
{ |
76
|
|
|
return !$parameter->hasType() || TypeFactory::reflection($parameter)->accept($value); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ReflectionMethod $method |
81
|
1 |
|
* |
82
|
1 |
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function isValidGetterMethod(ReflectionMethod $method): bool |
85
|
1 |
|
{ |
86
|
|
|
if (!$method->isPublic()) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$value = $method->invoke($this->facade->getObject()); |
92
|
|
|
} catch (Throwable $t) { |
93
|
|
|
return false; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
$returnType = $method->getReturnType(); |
97
|
|
|
|
98
|
|
|
return $value !== null || $returnType === null || $returnType->allowsNull(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ReflectionMethod $method |
103
|
|
|
* @param array ...$args |
104
|
2 |
|
* |
105
|
|
|
* @return bool |
106
|
2 |
|
*/ |
|
|
|
|
107
|
|
|
public function areValidMethodArguments(ReflectionMethod $method, ...$args): bool |
108
|
|
|
{ |
109
|
|
|
if (!$method->isPublic()) { |
110
|
2 |
|
return false; |
111
|
|
|
} |
112
|
2 |
|
|
113
|
|
|
if (count($args) < $method->getNumberOfRequiredParameters()) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->validateMethodArguments($method, ...$args); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param ReflectionMethod $method |
122
|
|
|
* @param array ...$args |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
|
|
|
|
126
|
|
|
private function validateMethodArguments(ReflectionMethod $method, ...$args): bool |
127
|
|
|
{ |
128
|
|
|
$parameters = $method->getParameters(); |
129
|
|
|
foreach ($args as $i => $arg) { |
130
|
|
|
if (!array_key_exists($i, $parameters)) { |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($arg === null && !$parameters[$i]->allowsNull()) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!$this->isValidParameterValue($parameters[$i], $arg)) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.