21
21
22
22
namespace WikibaseSolutions \CypherDSL \Traits ;
23
23
24
+ use __PHP_Incomplete_Class ;
24
25
use InvalidArgumentException ;
25
26
use ReflectionClass ;
27
+ use ReflectionException ;
26
28
use TypeError ;
29
+ use function get_class ;
27
30
use function gettype ;
31
+ use function implode ;
28
32
use function is_array ;
33
+ use function is_bool ;
34
+ use function is_float ;
35
+ use function is_int ;
29
36
use function is_numeric ;
37
+ use function is_object ;
38
+ use function is_string ;
39
+ use function sprintf ;
30
40
use function strlen ;
31
41
use function trim ;
32
42
@@ -44,86 +54,70 @@ trait ErrorTrait
44
54
*
45
55
* @throws TypeError
46
56
*/
47
- private function assertClass (string $ varName , $ classNames , $ userInput ): void
57
+ private static function assertClass (string $ varName , $ classNames , $ userInput ): void
48
58
{
49
- if (!$ this ->isClass ($ classNames , $ userInput )) {
50
- throw new TypeError (
51
- $ this ->getTypeErrorText (
52
- $ varName ,
53
- $ classNames ,
54
- $ userInput
55
- )
56
- );
59
+ if (!self ::isClass ($ classNames , $ userInput )) {
60
+ throw self ::typeError ($ varName , $ classNames , $ userInput );
57
61
}
58
62
}
59
63
60
- private function assertClassOrType (string $ varName , $ classOrTypes , $ userInput ): void
64
+ private static function assertClassOrType (string $ varName , $ classOrTypes , $ userInput ): void
61
65
{
62
- if (!$ this -> isClass ($ classOrTypes , $ userInput ) && !$ this -> isType ($ classOrTypes , $ userInput )) {
63
- throw new TypeError ( $ this -> getTypeErrorText ( $ varName , $ classOrTypes , $ userInput) );
66
+ if (!self :: isClass ($ classOrTypes , $ userInput ) && !self :: isType ($ classOrTypes , $ userInput )) {
67
+ throw self :: typeError ( $ varName , $ classOrTypes , $ userInput );
64
68
}
65
69
}
66
70
67
- private function assertType (string $ varName , $ types , $ userInput ): void
71
+ private static function assertType (string $ varName , $ types , $ userInput ): void
68
72
{
69
- if (!$ this ->isType ($ types , $ userInput )) {
70
- throw new TypeError (
71
- $ this ->getTypeErrorText (
72
- $ varName ,
73
- $ types ,
74
- $ userInput
75
- )
76
- );
73
+ if (!self ::isType ($ types , $ userInput )) {
74
+ throw self ::typeError ($ varName , $ types , $ userInput );
77
75
}
78
76
}
79
77
80
78
/**
81
- * Give a nice error message about $userInput not being an object with one of the $classNames types.
79
+ * Get debug type method stolen from the symfony polyfill
82
80
*
83
- * @param string $varName The name of the variable to be used in the message (without trailing '$')
84
- * @param array $classNames The class names that should be mentioned in the message
85
- * @param mixed $userInput The input that has been given
86
- * @return string
81
+ * @see https://github.com/symfony/polyfill/blob/main/src/Php80/Php80.php
87
82
*/
88
- private function getTypeErrorText ( string $ varName , array $ classNames , $ userInput ): string
83
+ public static function getDebugType ( $ value ): string
89
84
{
90
- return sprintf (
91
- '$%s should be a %s object, %s given. ' ,
92
- $ varName ,
93
- implode (' or ' , $ classNames ),
94
- $ this ->getUserInputInfo ($ userInput )
95
- );
96
- }
85
+ switch (true ) {
86
+ case null === $ value : return 'null ' ;
87
+ case is_bool ($ value ): return 'bool ' ;
88
+ case is_string ($ value ): return 'string ' ;
89
+ case is_array ($ value ): return 'array ' ;
90
+ case is_int ($ value ): return 'int ' ;
91
+ case is_float ($ value ): return 'float ' ;
92
+ case is_object ($ value ): break ;
93
+ case $ value instanceof __PHP_Incomplete_Class: return '__PHP_Incomplete_Class ' ;
94
+ default :
95
+ if (null === $ type = @get_resource_type ($ value )) {
96
+ return 'unknown ' ;
97
+ }
98
+
99
+ if ('Unknown ' === $ type ) {
100
+ $ type = 'closed ' ;
101
+ }
102
+
103
+ return "resource ( $ type) " ;
104
+ }
97
105
98
- /**
99
- * Simple function to determine what $userInput is.
100
- *
101
- * @param mixed $userInput
102
- * @return string A description of $userInput
103
- */
104
- private function getUserInputInfo ($ userInput ): string
105
- {
106
- $ info = gettype ($ userInput );
106
+ $ class = get_class ($ value );
107
107
108
- if ($ info === 'object ' ) {
109
- if ((new ReflectionClass ($ userInput ))->isAnonymous ()) {
110
- $ info = 'anonymous class instance ' ;
111
- } else {
112
- $ info = get_class ($ userInput );
113
- }
114
- } elseif (is_scalar ($ userInput )) {
115
- $ info .= ' " ' . (string )$ userInput . '" ' ;
108
+ if (false === strpos ($ class , '@ ' )) {
109
+ return $ class ;
116
110
}
117
111
118
- return $ info ;
112
+ return ( get_parent_class ( $ class ) ?: key ( class_implements ( $ class )) ?: ' class ' ). ' @anonymous ' ;
119
113
}
120
114
121
115
/**
122
116
* @param $classNames
123
117
* @param $userInput
124
118
* @return bool
125
119
*/
126
- private function isClass ($ classNames , $ userInput ): bool
120
+ private static function isClass ($ classNames , $ userInput ): bool
127
121
{
128
122
if (!is_array ($ classNames )) {
129
123
$ classNames = [$ classNames ];
@@ -143,7 +137,7 @@ private function isClass($classNames, $userInput): bool
143
137
* @param $userInput
144
138
* @return bool
145
139
*/
146
- private function isType ($ types , $ userInput ): bool
140
+ private static function isType ($ types , $ userInput ): bool
147
141
{
148
142
if (!is_array ($ types )) {
149
143
$ types = [$ types ];
@@ -168,7 +162,7 @@ private function isType($types, $userInput): bool
168
162
*
169
163
* @return void
170
164
*/
171
- private static function validateName (string $ name ): void
165
+ private static function assertValidName (string $ name ): void
172
166
{
173
167
$ name = trim ($ name );
174
168
@@ -188,4 +182,20 @@ private static function validateName(string $name): void
188
182
throw new InvalidArgumentException ('A name cannot be longer than 65534 characters ' );
189
183
}
190
184
}
185
+
186
+ /**
187
+ * @param string $varName
188
+ * @param $classNames
189
+ * @param $userInput
190
+ * @return TypeError
191
+ */
192
+ private static function typeError (string $ varName , $ classNames , $ userInput ): TypeError
193
+ {
194
+ return new TypeError (sprintf (
195
+ '$%s should be a %s object, %s given. ' ,
196
+ $ varName ,
197
+ implode (' or ' , $ classNames ),
198
+ self ::getDebugType ($ userInput )
199
+ ));
200
+ }
191
201
}
0 commit comments