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