2121
2222namespace WikibaseSolutions \CypherDSL \Traits ;
2323
24- use TypeError ;
2524use ReflectionClass ;
25+ use TypeError ;
2626
2727/**
2828 * Convenience trait including simple assertions and error reporting functions
2929 */
30- trait ErrorTrait {
30+ trait ErrorTrait
31+ {
3132
3233 /**
3334 * Asserts that $userInput is an instance of one of the provided $classNames (polyfill for php 8.0 Union types)
3435 *
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
3839 * @throws TypeError
3940 */
40- private function assertClass (string $ varName , $ classNames , $ userInput ) : void {
41+ private function assertClass (string $ varName , $ classNames , $ userInput ): void
42+ {
4143 if (!is_array ($ classNames )) {
4244 $ classNames = [$ classNames ];
4345 }
46+
4447 foreach ($ classNames as $ class ) {
4548 if ($ userInput instanceof $ class )
4649 return ;
4750 }
51+
4852 throw new TypeError (
4953 $ this ->getTypeErrorText (
5054 $ varName ,
@@ -57,38 +61,41 @@ private function assertClass(string $varName, $classNames, $userInput) : void {
5761 /**
5862 * Give a nice error message about $userInput not being an object with one of the $classNames types.
5963 *
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
6368 */
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+ );
7377 }
7478
7579 /**
7680 * Simple function to determine what $userInput is.
7781 *
7882 * @param mixed $userInput
79- * @return string A description of $userInput.
83+ * @return string A description of $userInput
8084 */
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 ' ) {
8490 if ((new ReflectionClass ($ userInput ))->isAnonymous ()) {
8591 $ info = 'anonymous class instance ' ;
8692 } else {
87- $ info = get_class ( $ userInput );
93+ $ info = get_class ($ userInput );
8894 }
8995 } elseif (is_scalar ($ userInput )) {
90- $ info .= ' " ' . (string ) $ userInput . '" ' ;
96+ $ info .= ' " ' . (string )$ userInput . '" ' ;
9197 }
98+
9299 return $ info ;
93100 }
94101}
0 commit comments