|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\PHPUnit; |
| 4 | + |
| 5 | +use PhpParser\Node\Arg; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PhpParser\Node\Expr\StaticCall; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\MethodReflection; |
| 10 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 11 | +use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
| 12 | +use PHPStan\Type\ObjectType; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use PHPStan\Type\TypeCombinator; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\MockObject\Stub; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use function count; |
| 19 | + |
| 20 | +class MockForIntersectionDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension, DynamicStaticMethodReturnTypeExtension |
| 21 | +{ |
| 22 | + |
| 23 | + public function getClass(): string |
| 24 | + { |
| 25 | + return TestCase::class; |
| 26 | + } |
| 27 | + |
| 28 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 29 | + { |
| 30 | + return $methodReflection->getName() === 'createMockForIntersectionOfInterfaces'; |
| 31 | + } |
| 32 | + |
| 33 | + public function isStaticMethodSupported(MethodReflection $methodReflection): bool |
| 34 | + { |
| 35 | + return $methodReflection->getName() === 'createStubForIntersectionOfInterfaces'; |
| 36 | + } |
| 37 | + |
| 38 | + public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type |
| 39 | + { |
| 40 | + return $this->getTypeFromCall($methodReflection, $methodCall->getArgs(), $scope); |
| 41 | + } |
| 42 | + |
| 43 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type |
| 44 | + { |
| 45 | + return $this->getTypeFromCall($methodReflection, $methodCall->getArgs(), $scope); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param array<Arg> $args |
| 50 | + */ |
| 51 | + private function getTypeFromCall(MethodReflection $methodReflection, array $args, Scope $scope): ?Type |
| 52 | + { |
| 53 | + if (!isset($args[0])) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + $interfaces = $scope->getType($args[0]->value); |
| 58 | + $constantArrays = $interfaces->getConstantArrays(); |
| 59 | + if (count($constantArrays) !== 1) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + $constantArray = $constantArrays[0]; |
| 64 | + if (count($constantArray->getOptionalKeys()) > 0) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + $result = []; |
| 69 | + if ($methodReflection->getName() === 'createMockForIntersectionOfInterfaces') { |
| 70 | + $result[] = new ObjectType(MockObject::class); |
| 71 | + } else { |
| 72 | + $result[] = new ObjectType(Stub::class); |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($constantArray->getValueTypes() as $valueType) { |
| 76 | + if (!$valueType->isClassString()->yes()) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + $values = $valueType->getConstantScalarValues(); |
| 81 | + if (count($values) !== 1) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + $result[] = new ObjectType((string) $values[0]); |
| 86 | + } |
| 87 | + |
| 88 | + return TypeCombinator::intersect(...$result); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments