|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Pepakriz\PHPStanExceptionRules\Extension; |
| 4 | + |
| 5 | +use Pepakriz\PHPStanExceptionRules\DynamicConstructorThrowTypeExtension; |
| 6 | +use Pepakriz\PHPStanExceptionRules\UnsupportedClassException; |
| 7 | +use PhpParser\Node\Expr\New_; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Broker\Broker; |
| 11 | +use PHPStan\Broker\ClassNotFoundException; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\Type\Constant\ConstantStringType; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use PHPStan\Type\VoidType; |
| 17 | +use ReflectionClass; |
| 18 | +use ReflectionException; |
| 19 | +use ReflectionFunction; |
| 20 | +use ReflectionProperty; |
| 21 | +use ReflectionZendExtension; |
| 22 | +use function is_a; |
| 23 | + |
| 24 | +class ReflectionExtension implements DynamicConstructorThrowTypeExtension |
| 25 | +{ |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Broker |
| 29 | + */ |
| 30 | + private $broker; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + Broker $broker |
| 34 | + ) |
| 35 | + { |
| 36 | + $this->broker = $broker; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @throws UnsupportedClassException |
| 41 | + */ |
| 42 | + public function getThrowTypeFromConstructor(MethodReflection $methodReflection, New_ $newNode, Scope $scope): Type |
| 43 | + { |
| 44 | + $className = $methodReflection->getDeclaringClass()->getName(); |
| 45 | + |
| 46 | + if (is_a($className, ReflectionClass::class, true)) { |
| 47 | + return $this->resolveReflectionClass($newNode, $scope); |
| 48 | + } |
| 49 | + |
| 50 | + if (is_a($className, ReflectionProperty::class, true)) { |
| 51 | + return $this->resolveReflectionProperty($newNode, $scope); |
| 52 | + } |
| 53 | + |
| 54 | + if (is_a($className, ReflectionFunction::class, true)) { |
| 55 | + return $this->resolveReflectionFunction($newNode, $scope); |
| 56 | + } |
| 57 | + |
| 58 | + if (is_a($className, ReflectionZendExtension::class, true)) { |
| 59 | + return $this->resolveReflectionClass($newNode, $scope); |
| 60 | + } |
| 61 | + |
| 62 | + throw new UnsupportedClassException(); |
| 63 | + } |
| 64 | + |
| 65 | + private function resolveReflectionClass(New_ $newNode, Scope $scope): Type |
| 66 | + { |
| 67 | + $reflectionExceptionType = new ObjectType(ReflectionException::class); |
| 68 | + $valueType = $scope->getType($newNode->args[0]->value); |
| 69 | + if (!$valueType instanceof ConstantStringType) { |
| 70 | + return $reflectionExceptionType; |
| 71 | + } |
| 72 | + |
| 73 | + if (!$this->broker->hasClass($valueType->getValue())) { |
| 74 | + return $reflectionExceptionType; |
| 75 | + } |
| 76 | + |
| 77 | + return new VoidType(); |
| 78 | + } |
| 79 | + |
| 80 | + private function resolveReflectionFunction(New_ $newNode, Scope $scope): Type |
| 81 | + { |
| 82 | + $reflectionExceptionType = new ObjectType(ReflectionException::class); |
| 83 | + $valueType = $scope->getType($newNode->args[0]->value); |
| 84 | + if (!$valueType instanceof ConstantStringType) { |
| 85 | + return $reflectionExceptionType; |
| 86 | + } |
| 87 | + |
| 88 | + if (!$this->broker->hasFunction(new Name($valueType->getValue()), $scope)) { |
| 89 | + return $reflectionExceptionType; |
| 90 | + } |
| 91 | + |
| 92 | + return new VoidType(); |
| 93 | + } |
| 94 | + |
| 95 | + private function resolveReflectionProperty(New_ $newNode, Scope $scope): Type |
| 96 | + { |
| 97 | + $reflectionExceptionType = new ObjectType(ReflectionException::class); |
| 98 | + $valueType = $scope->getType($newNode->args[0]->value); |
| 99 | + if (!$valueType instanceof ConstantStringType) { |
| 100 | + return $reflectionExceptionType; |
| 101 | + } |
| 102 | + |
| 103 | + $propertyType = $scope->getType($newNode->args[1]->value); |
| 104 | + if (!$propertyType instanceof ConstantStringType) { |
| 105 | + return $reflectionExceptionType; |
| 106 | + } |
| 107 | + |
| 108 | + try { |
| 109 | + if (!$this->broker->getClass($valueType->getValue())->hasProperty($propertyType->getValue())) { |
| 110 | + return $reflectionExceptionType; |
| 111 | + } |
| 112 | + } catch (ClassNotFoundException $e) { |
| 113 | + return $reflectionExceptionType; |
| 114 | + } |
| 115 | + |
| 116 | + return new VoidType(); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments