|
1 | 1 | <?php |
2 | 2 | namespace Gt\Promise\Chain; |
3 | 3 |
|
| 4 | +use Closure; |
4 | 5 | use ReflectionFunction; |
| 6 | +use ReflectionIntersectionType; |
| 7 | +use ReflectionNamedType; |
| 8 | +use ReflectionUnionType; |
5 | 9 | use Throwable; |
6 | 10 | use TypeError; |
7 | 11 |
|
@@ -37,20 +41,91 @@ public function callOnRejected(Throwable $reason) { |
37 | 41 | } |
38 | 42 |
|
39 | 43 | return call_user_func($this->onRejected, $reason); |
40 | | -// try { |
41 | | -// } |
42 | | -// catch(TypeError $error) { |
43 | | -// $reflection = new ReflectionFunction($this->onRejected); |
44 | | -// $param = $reflection->getParameters()[0] ?? null; |
45 | | -// if($param) { |
46 | | -// $paramType = (string)$param->getType(); |
47 | | -// |
48 | | -// if(!str_contains($error->getMessage(), "must be of type $paramType")) { |
49 | | -// throw $error; |
50 | | -// } |
51 | | -// } |
52 | | -// |
53 | | -// return $reason; |
54 | | -// } |
| 44 | + } |
| 45 | + |
| 46 | + public function checkResolutionCallbackType(mixed $resolvedValue):void { |
| 47 | + if(isset($this->onResolved)) { |
| 48 | + $this->checkType($resolvedValue, $this->onResolved); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public function checkRejectionCallbackType(Throwable $rejection):void { |
| 53 | + if(isset($this->onRejected)) { |
| 54 | + $this->checkType($rejection, $this->onRejected); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 60 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
| 61 | + */ |
| 62 | + // phpcs:ignore |
| 63 | + private function checkType(mixed $value, callable $callable):void { |
| 64 | + if(!$callable instanceof Closure) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $refFunction = new ReflectionFunction($callable); |
| 69 | + $refParameterList = $refFunction->getParameters(); |
| 70 | + if(!isset($refParameterList[0])) { |
| 71 | + return; |
| 72 | + } |
| 73 | + $refParameter = $refParameterList[0]; |
| 74 | + $nullable = $refParameter->allowsNull(); |
| 75 | + |
| 76 | + if(is_null($value)) { |
| 77 | + if(!$nullable) { |
| 78 | + throw new ChainFunctionTypeError("Then function's parameter is not nullable"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + $allowedTypes = []; |
| 83 | + $refType = $refParameter->getType(); |
| 84 | + |
| 85 | + if($refType instanceof ReflectionUnionType || $refType instanceof ReflectionIntersectionType) { |
| 86 | + foreach($refType->getTypes() as $refSubType) { |
| 87 | + array_push($allowedTypes, $refSubType->getName()); |
| 88 | + } |
| 89 | + } |
| 90 | + else { |
| 91 | + /** @var ?ReflectionNamedType $refType */ |
| 92 | + array_push($allowedTypes, $refType?->getName()); |
| 93 | + } |
| 94 | + |
| 95 | + $valueType = is_object($value) |
| 96 | + ? get_class($value) |
| 97 | + : gettype($value); |
| 98 | + foreach($allowedTypes as $allowedType) { |
| 99 | + $allowedType = match($allowedType) { |
| 100 | + "int" => "integer", |
| 101 | + "float" => "double", |
| 102 | + default => $allowedType, |
| 103 | + }; |
| 104 | + if(is_null($allowedType) || $allowedType === "mixed") { |
| 105 | +// A typeless property is defined - allow anything! |
| 106 | + return; |
| 107 | + } |
| 108 | + if($allowedType === $valueType) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if(is_a($allowedType, $valueType)) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + if($allowedType === "string") { |
| 118 | + if($valueType === "double" || $valueType === "integer") { |
| 119 | + return; |
| 120 | + } |
| 121 | + } |
| 122 | + if($allowedType === "double") { |
| 123 | + if(is_numeric($value)) { |
| 124 | + return; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + throw new ChainFunctionTypeError("Value $value is not compatible with chainable parameter"); |
55 | 130 | } |
56 | 131 | } |
0 commit comments