Skip to content

Commit 8a84798

Browse files
committed
cleaned ErrorTrait
1 parent 3acb165 commit 8a84798

File tree

2 files changed

+65
-56
lines changed

2 files changed

+65
-56
lines changed

src/Traits/ErrorTrait.php

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Traits;
2323

24+
use __PHP_Incomplete_Class;
2425
use InvalidArgumentException;
2526
use ReflectionClass;
27+
use ReflectionException;
2628
use TypeError;
29+
use function get_class;
2730
use function gettype;
31+
use function implode;
2832
use function is_array;
33+
use function is_bool;
34+
use function is_float;
35+
use function is_int;
2936
use function is_numeric;
37+
use function is_object;
38+
use function is_string;
39+
use function sprintf;
3040
use function strlen;
3141
use function trim;
3242

@@ -44,86 +54,70 @@ trait ErrorTrait
4454
*
4555
* @throws TypeError
4656
*/
47-
private function assertClass(string $varName, $classNames, $userInput): void
57+
private static function assertClass(string $varName, $classNames, $userInput): void
4858
{
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);
5761
}
5862
}
5963

60-
private function assertClassOrType(string $varName, $classOrTypes, $userInput): void
64+
private static function assertClassOrType(string $varName, $classOrTypes, $userInput): void
6165
{
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);
6468
}
6569
}
6670

67-
private function assertType(string $varName, $types, $userInput): void
71+
private static function assertType(string $varName, $types, $userInput): void
6872
{
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);
7775
}
7876
}
7977

8078
/**
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
8280
*
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
8782
*/
88-
private function getTypeErrorText(string $varName, array $classNames, $userInput): string
83+
public static function getDebugType($value): string
8984
{
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+
}
97105

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);
107107

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;
116110
}
117111

118-
return $info;
112+
return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous';
119113
}
120114

121115
/**
122116
* @param $classNames
123117
* @param $userInput
124118
* @return bool
125119
*/
126-
private function isClass($classNames, $userInput): bool
120+
private static function isClass($classNames, $userInput): bool
127121
{
128122
if (!is_array($classNames)) {
129123
$classNames = [$classNames];
@@ -143,7 +137,7 @@ private function isClass($classNames, $userInput): bool
143137
* @param $userInput
144138
* @return bool
145139
*/
146-
private function isType($types, $userInput): bool
140+
private static function isType($types, $userInput): bool
147141
{
148142
if (!is_array($types)) {
149143
$types = [$types];
@@ -168,7 +162,7 @@ private function isType($types, $userInput): bool
168162
*
169163
* @return void
170164
*/
171-
private static function validateName(string $name): void
165+
private static function assertValidName(string $name): void
172166
{
173167
$name = trim($name);
174168

@@ -188,4 +182,20 @@ private static function validateName(string $name): void
188182
throw new InvalidArgumentException('A name cannot be longer than 65534 characters');
189183
}
190184
}
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+
}
191201
}

src/Traits/HasNameTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace WikibaseSolutions\CypherDSL\Traits;
44

5-
use InvalidArgumentException;
65
use function bin2hex;
76
use function ceil;
87
use function openssl_random_pseudo_bytes;

0 commit comments

Comments
 (0)