|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Pest\PHPStan\Type\Pest; |
| 6 | + |
| 7 | +use Override; |
| 8 | +use PHPStan\Reflection\ClassConstantReflection; |
| 9 | +use PHPStan\Reflection\ClassMemberAccessAnswerer; |
| 10 | +use PHPStan\Reflection\ExtendedMethodReflection; |
| 11 | +use PHPStan\Reflection\ExtendedPropertyReflection; |
| 12 | +use PHPStan\Reflection\ReflectionProvider; |
| 13 | +use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection; |
| 14 | +use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection; |
| 15 | +use PHPStan\TrinaryLogic; |
| 16 | +use PHPStan\Type\ObjectType; |
| 17 | + |
| 18 | +final class PestTestCaseWithTraitsType extends ObjectType |
| 19 | +{ |
| 20 | + /** @param list<class-string> $traitNames */ |
| 21 | + public function __construct( |
| 22 | + string $className, |
| 23 | + private readonly array $traitNames, |
| 24 | + private readonly ReflectionProvider $reflectionProvider, |
| 25 | + ) { |
| 26 | + parent::__construct($className); |
| 27 | + } |
| 28 | + |
| 29 | + #[Override] |
| 30 | + public function hasMethod(string $methodName): TrinaryLogic |
| 31 | + { |
| 32 | + if (parent::hasMethod($methodName)->yes()) { |
| 33 | + return TrinaryLogic::createYes(); |
| 34 | + } |
| 35 | + |
| 36 | + if ($this->hasTraitMethod($methodName)) { |
| 37 | + return TrinaryLogic::createYes(); |
| 38 | + } |
| 39 | + |
| 40 | + return parent::hasMethod($methodName); |
| 41 | + } |
| 42 | + |
| 43 | + #[Override] |
| 44 | + public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection |
| 45 | + { |
| 46 | + if (parent::hasMethod($methodName)->yes()) { |
| 47 | + return parent::getMethod($methodName, $scope); |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($this->traitNames as $traitName) { |
| 51 | + $traitReflection = $this->reflectionProvider->getClass($traitName); |
| 52 | + |
| 53 | + if ($traitReflection->hasNativeMethod($methodName)) { |
| 54 | + return $traitReflection->getNativeMethod($methodName); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return parent::getMethod($methodName, $scope); |
| 59 | + } |
| 60 | + |
| 61 | + #[Override] |
| 62 | + public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection |
| 63 | + { |
| 64 | + if (parent::hasMethod($methodName)->yes()) { |
| 65 | + return parent::getUnresolvedMethodPrototype($methodName, $scope); |
| 66 | + } |
| 67 | + |
| 68 | + foreach ($this->traitNames as $traitName) { |
| 69 | + if ($this->reflectionProvider->getClass($traitName)->hasNativeMethod($methodName)) { |
| 70 | + return new ObjectType($traitName)->getUnresolvedMethodPrototype($methodName, $scope); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return parent::getUnresolvedMethodPrototype($methodName, $scope); |
| 75 | + } |
| 76 | + |
| 77 | + #[Override] |
| 78 | + public function hasProperty(string $propertyName): TrinaryLogic |
| 79 | + { |
| 80 | + if (parent::hasProperty($propertyName)->yes()) { |
| 81 | + return TrinaryLogic::createYes(); |
| 82 | + } |
| 83 | + |
| 84 | + if ($this->hasTraitProperty($propertyName)) { |
| 85 | + return TrinaryLogic::createYes(); |
| 86 | + } |
| 87 | + |
| 88 | + return parent::hasProperty($propertyName); |
| 89 | + } |
| 90 | + |
| 91 | + #[Override] |
| 92 | + public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection |
| 93 | + { |
| 94 | + if (parent::hasProperty($propertyName)->yes()) { |
| 95 | + return parent::getProperty($propertyName, $scope); |
| 96 | + } |
| 97 | + |
| 98 | + foreach ($this->traitNames as $traitName) { |
| 99 | + $traitReflection = $this->reflectionProvider->getClass($traitName); |
| 100 | + |
| 101 | + if ($traitReflection->hasNativeProperty($propertyName)) { |
| 102 | + return $traitReflection->getNativeProperty($propertyName); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return parent::getProperty($propertyName, $scope); |
| 107 | + } |
| 108 | + |
| 109 | + #[Override] |
| 110 | + public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 111 | + { |
| 112 | + if (parent::hasProperty($propertyName)->yes()) { |
| 113 | + return parent::getUnresolvedPropertyPrototype($propertyName, $scope); |
| 114 | + } |
| 115 | + |
| 116 | + foreach ($this->traitNames as $traitName) { |
| 117 | + if ($this->reflectionProvider->getClass($traitName)->hasNativeProperty($propertyName)) { |
| 118 | + return new ObjectType($traitName)->getUnresolvedPropertyPrototype($propertyName, $scope); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return parent::getUnresolvedPropertyPrototype($propertyName, $scope); |
| 123 | + } |
| 124 | + |
| 125 | + #[Override] |
| 126 | + public function hasConstant(string $constantName): TrinaryLogic |
| 127 | + { |
| 128 | + if (parent::hasConstant($constantName)->yes()) { |
| 129 | + return TrinaryLogic::createYes(); |
| 130 | + } |
| 131 | + |
| 132 | + if ($this->hasTraitConstant($constantName)) { |
| 133 | + return TrinaryLogic::createYes(); |
| 134 | + } |
| 135 | + |
| 136 | + return parent::hasConstant($constantName); |
| 137 | + } |
| 138 | + |
| 139 | + #[Override] |
| 140 | + public function getConstant(string $constantName): ClassConstantReflection |
| 141 | + { |
| 142 | + if (parent::hasConstant($constantName)->yes()) { |
| 143 | + return parent::getConstant($constantName); |
| 144 | + } |
| 145 | + |
| 146 | + foreach ($this->traitNames as $traitName) { |
| 147 | + $traitReflection = $this->reflectionProvider->getClass($traitName); |
| 148 | + |
| 149 | + if ($traitReflection->hasConstant($constantName)) { |
| 150 | + return $traitReflection->getConstant($constantName); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return parent::getConstant($constantName); |
| 155 | + } |
| 156 | + |
| 157 | + private function hasTraitMethod(string $methodName): bool |
| 158 | + { |
| 159 | + return array_any($this->traitNames, fn (string $traitName): bool => $this->reflectionProvider->getClass($traitName)->hasNativeMethod($methodName)); |
| 160 | + } |
| 161 | + |
| 162 | + private function hasTraitProperty(string $propertyName): bool |
| 163 | + { |
| 164 | + return array_any($this->traitNames, fn (string $traitName): bool => $this->reflectionProvider->getClass($traitName)->hasNativeProperty($propertyName)); |
| 165 | + } |
| 166 | + |
| 167 | + private function hasTraitConstant(string $constantName): bool |
| 168 | + { |
| 169 | + return array_any($this->traitNames, fn (string $traitName): bool => $this->reflectionProvider->getClass($traitName)->hasConstant($constantName)); |
| 170 | + } |
| 171 | +} |
0 commit comments