|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\Common\Type; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use LM\Common\Enum\Scalar; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait for checking types. |
|
12
|
|
|
* |
|
13
|
|
|
* @todo Make it implement an interface. |
|
14
|
|
|
* @todo Rely on an implementation (Scalar). |
|
15
|
|
|
*/ |
|
16
|
|
|
class Type |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string The type identifier (class name, scalar type, interface…) */ |
|
19
|
|
|
private $type; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(string $type) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->type = $type; |
|
24
|
|
|
if ($this->isArrayType() |
|
25
|
|
|
|| ($this->isStringType()) |
|
26
|
|
|
|| ($this->isIntegerType()) |
|
27
|
|
|
|| ($this->isBoolType()) |
|
28
|
|
|
|| ($this->isClassOrInterfaceName())) { |
|
29
|
|
|
} else { |
|
30
|
|
|
throw new InvalidArgumentException(); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return bool Whether the type is the array scalar type. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function isArrayType(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return Scalar::_ARRAY === $this->type; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return bool Whether the type is the boolean scalar type. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function isBoolType(): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return Scalar::_BOOL === $this->type; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return bool Whether the type is the FQCN of a class or of an interface. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function isClassOrInterfaceName(): bool |
|
54
|
|
|
{ |
|
55
|
|
|
return class_exists($this->type) || interface_exists($this->type); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return bool Whether the type is the scalar integer type. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function isIntegerType(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return Scalar::_INT === $this->type; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return bool Whether the type is the string type. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function isStringType(): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return Scalar::_STR === $this->type; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Throws an exception if the value is not of the expected type. |
|
76
|
|
|
* @param mixed $value The value to check. |
|
77
|
|
|
* @throws InvalidArgumentException if the value is not of the specified |
|
78
|
|
|
* type. |
|
79
|
|
|
* @todo Use is_a or instanceof instead? |
|
80
|
|
|
* @todo Long switch-like statement. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function check($value): void |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->isArrayType($this->type)) { |
|
|
|
|
|
|
85
|
|
|
if (!is_array($value)) { |
|
86
|
|
|
throw new InvalidArgumentException(); |
|
87
|
|
|
} |
|
88
|
|
|
} elseif ($this->isStringType($this->type)) { |
|
|
|
|
|
|
89
|
|
|
if (!is_string($value)) { |
|
90
|
|
|
throw new InvalidArgumentException(); |
|
91
|
|
|
} |
|
92
|
|
|
} elseif ($this->isIntegerType($this->type)) { |
|
|
|
|
|
|
93
|
|
|
if (!is_int($value)) { |
|
94
|
|
|
throw new InvalidArgumentException(); |
|
95
|
|
|
} |
|
96
|
|
|
} elseif ($this->isBoolType($this->type)) { |
|
|
|
|
|
|
97
|
|
|
if (!is_bool($value)) { |
|
98
|
|
|
throw new InvalidArgumentException(); |
|
99
|
|
|
} |
|
100
|
|
|
} elseif ($this->isClassOrInterfaceName($this->type)) { |
|
|
|
|
|
|
101
|
|
|
if (!is_object($value) || !is_a($value, $this->type)) { |
|
102
|
|
|
throw new InvalidArgumentException(); |
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
|
|
throw new InvalidArgumentException(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.