21
21
22
22
namespace WikibaseSolutions \CypherDSL \Traits ;
23
23
24
- use TypeError ;
25
24
use ReflectionClass ;
25
+ use TypeError ;
26
26
27
27
/**
28
28
* Convenience trait including simple assertions and error reporting functions
29
29
*/
30
- trait ErrorTrait {
30
+ trait ErrorTrait
31
+ {
31
32
32
33
/**
33
34
* Asserts that $userInput is an instance of one of the provided $classNames (polyfill for php 8.0 Union types)
34
35
*
35
- * @param string $varName The name of the userinput variable, to be used in the error message.
36
- * @param string|string[] $classNames The classnames that should be tested against
37
- * @param mixed $userInput The input that should be tested
36
+ * @param string $varName The name of the userinput variable, to be used in the error message.
37
+ * @param string|string[] $classNames The classnames that should be tested against
38
+ * @param mixed $userInput The input that should be tested
38
39
* @throws TypeError
39
40
*/
40
- private function assertClass (string $ varName , $ classNames , $ userInput ) : void {
41
+ private function assertClass (string $ varName , $ classNames , $ userInput ): void
42
+ {
41
43
if (!is_array ($ classNames )) {
42
44
$ classNames = [$ classNames ];
43
45
}
46
+
44
47
foreach ($ classNames as $ class ) {
45
48
if ($ userInput instanceof $ class )
46
49
return ;
47
50
}
51
+
48
52
throw new TypeError (
49
53
$ this ->getTypeErrorText (
50
54
$ varName ,
@@ -57,38 +61,41 @@ private function assertClass(string $varName, $classNames, $userInput) : void {
57
61
/**
58
62
* Give a nice error message about $userInput not being an object with one of the $classNames types.
59
63
*
60
- * @param string $varname The name of the variable to be used in the message (without trailing '$')
61
- * @param array $classNames The classnames that should be mentioned in the message
62
- * @param mixed $userInput The input that has been given.
64
+ * @param string $varName The name of the variable to be used in the message (without trailing '$')
65
+ * @param array $classNames The class names that should be mentioned in the message
66
+ * @param mixed $userInput The input that has been given
67
+ * @return string
63
68
*/
64
- private function getTypeErrorText (
65
- string $ varName ,
66
- array $ classNames ,
67
- $ userInput
68
- ) : string {
69
- return
70
- "\$$ varName should be a " .
71
- implode (' or ' , $ classNames ) . " object, " .
72
- $ this ->getUserInputInfo ($ userInput ) . " given. " ;
69
+ private function getTypeErrorText (string $ varName , array $ classNames , $ userInput ): string
70
+ {
71
+ return sprintf (
72
+ '$%s should be a %s object, %s given. ' ,
73
+ $ varName ,
74
+ implode (' or ' , $ classNames ),
75
+ $ this ->getUserInputInfo ($ userInput )
76
+ );
73
77
}
74
78
75
79
/**
76
80
* Simple function to determine what $userInput is.
77
81
*
78
82
* @param mixed $userInput
79
- * @return string A description of $userInput.
83
+ * @return string A description of $userInput
80
84
*/
81
- private function getUserInputInfo ($ userInput ) : string {
82
- $ info = gettype ( $ userInput );
83
- if ( $ info === 'object ' ) {
85
+ private function getUserInputInfo ($ userInput ): string
86
+ {
87
+ $ info = gettype ($ userInput );
88
+
89
+ if ($ info === 'object ' ) {
84
90
if ((new ReflectionClass ($ userInput ))->isAnonymous ()) {
85
91
$ info = 'anonymous class instance ' ;
86
92
} else {
87
- $ info = get_class ( $ userInput );
93
+ $ info = get_class ($ userInput );
88
94
}
89
95
} elseif (is_scalar ($ userInput )) {
90
- $ info .= ' " ' . (string ) $ userInput . '" ' ;
96
+ $ info .= ' " ' . (string )$ userInput . '" ' ;
91
97
}
98
+
92
99
return $ info ;
93
100
}
94
101
}
0 commit comments