1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dgame\Object; |
4
|
|
|
|
5
|
|
|
use ReflectionMethod; |
6
|
|
|
use ReflectionProperty; |
7
|
|
|
use function Dgame\Conditional\debug; |
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
|
|
|
{ |
27
|
|
|
$this->facade = $facade; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ObjectFacade $facade |
32
|
|
|
* |
33
|
|
|
* @return Validator |
34
|
|
|
*/ |
35
|
|
|
public static function new(ObjectFacade $facade): self |
36
|
|
|
{ |
37
|
|
|
return new self($facade); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ReflectionProperty $property |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function isValidProperty(ReflectionProperty $property): bool |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
if (!$property->isPublic()) { |
48
|
|
|
debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Property %s is not public', $property->getName()); |
49
|
|
|
|
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($property->isStatic()) { |
54
|
|
|
debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Property %s is static', $property->getName()); |
55
|
|
|
|
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ReflectionMethod $method |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function isValidMethod(ReflectionMethod $method): bool |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if (!$method->isPublic()) { |
70
|
|
|
debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s is not public', $method->getName()); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($method->isStatic()) { |
76
|
|
|
debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s is static', $method->getName()); |
|
|
|
|
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ReflectionMethod $method |
86
|
|
|
* @param $value |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function isValidSetterMethod(ReflectionMethod $method, $value): bool |
91
|
|
|
{ |
92
|
|
|
if (!$this->isValidMethod($method)) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
View Code Duplication |
if ($value === null && $method->getNumberOfParameters() !== 0 && !$method->getParameters()[0]->allowsNull()) { |
|
|
|
|
97
|
|
|
debug(ObjectFacade::DEBUG_LABEL)->output('[Error] First parameter of method %s is not allowed to be null', $method->getName()); |
|
|
|
|
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($method->getNumberOfParameters() === 0) { |
103
|
|
|
debug(ObjectFacade::DEBUG_LABEL)->output('[Warning] Method %s does not accept any parameters', $method->getName()); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param ReflectionMethod $method |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function validateGetterMethod(ReflectionMethod $method): bool |
115
|
|
|
{ |
116
|
|
|
if (!$this->isValidMethod($method)) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$value = $method->invoke($this->facade->getObject()); |
121
|
|
View Code Duplication |
if ($value === null && $method->hasReturnType() && !$method->getReturnType()->allowsNull()) { |
|
|
|
|
122
|
|
|
debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s return value is not allowed to be null', $method->getName()); |
|
|
|
|
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.