|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace TheCodingMachine\GraphQL\Controllers; |
| 5 | + |
| 6 | + |
| 7 | +use phpDocumentor\Reflection\Type; |
| 8 | +use phpDocumentor\Reflection\Types\Array_; |
| 9 | +use phpDocumentor\Reflection\Types\Mixed_; |
| 10 | +use Roave\BetterReflection\Reflection\ReflectionFunction; |
| 11 | +use Roave\BetterReflection\Reflection\ReflectionMethod; |
| 12 | +use Roave\BetterReflection\Reflection\ReflectionParameter; |
| 13 | + |
| 14 | +class TypeMappingException extends GraphQLException |
| 15 | +{ |
| 16 | + private $type; |
| 17 | + |
| 18 | + public static function createFromType(Type $type) |
| 19 | + { |
| 20 | + $e = new self("Don't know how to handle type ".(string) $type); |
| 21 | + $e->type = $type; |
| 22 | + return $e; |
| 23 | + } |
| 24 | + |
| 25 | + public static function wrapWithParamInfo(TypeMappingException $previous, ReflectionParameter $parameter): TypeMappingException |
| 26 | + { |
| 27 | + if ($previous->type instanceof Array_) { |
| 28 | + $message = sprintf('Parameter $%s in %s::%s is type-hinted to array. Please provide an additional @param in the PHPDoc block to further specify the type of the array. For instance: @param string[] $%s.', |
| 29 | + $parameter->getName(), |
| 30 | + $parameter->getDeclaringClass()->getName(), |
| 31 | + $parameter->getDeclaringFunction()->getName(), |
| 32 | + $parameter->getName()); |
| 33 | + } elseif ($previous->type instanceof Mixed_) { |
| 34 | + $message = sprintf('Parameter $%s in %s::%s is missing a type-hint (or type-hinted to "mixed"). Please provide a better type-hint. For instance: "string $%s".', |
| 35 | + $parameter->getName(), |
| 36 | + $parameter->getDeclaringClass()->getName(), |
| 37 | + $parameter->getDeclaringFunction()->getName(), |
| 38 | + $parameter->getName()); |
| 39 | + } else { |
| 40 | + throw new GraphQLException("Unexpected type in TypeMappingException"); |
| 41 | + } |
| 42 | + |
| 43 | + $e = new self($message, 0, $previous); |
| 44 | + $e->type = $previous->type; |
| 45 | + return $e; |
| 46 | + } |
| 47 | + |
| 48 | + public static function wrapWithReturnInfo(TypeMappingException $previous, ReflectionMethod $method): TypeMappingException |
| 49 | + { |
| 50 | + if ($previous->type instanceof Array_) { |
| 51 | + $message = sprintf('Return type in %s::%s is type-hinted to array. Please provide an additional @return in the PHPDoc block to further specify the type of the array. For instance: @return string[]', |
| 52 | + $method->getDeclaringClass()->getName(), |
| 53 | + $method->getName()); |
| 54 | + } elseif ($previous->type instanceof Mixed_) { |
| 55 | + $message = sprintf('Return type in %s::%s is missing a type-hint (or type-hinted to "mixed"). Please provide a better type-hint.', |
| 56 | + $method->getDeclaringClass()->getName(), |
| 57 | + $method->getName()); |
| 58 | + } else { |
| 59 | + throw new GraphQLException("Unexpected type in TypeMappingException"); |
| 60 | + } |
| 61 | + |
| 62 | + $e = new self($message, 0, $previous); |
| 63 | + $e->type = $previous->type; |
| 64 | + return $e; |
| 65 | + } |
| 66 | +} |
0 commit comments