diff --git a/src/Mockery/PhpDoc/TypeNodeResolverExtension.php b/src/Mockery/PhpDoc/TypeNodeResolverExtension.php index 183d196..ca936aa 100644 --- a/src/Mockery/PhpDoc/TypeNodeResolverExtension.php +++ b/src/Mockery/PhpDoc/TypeNodeResolverExtension.php @@ -9,7 +9,6 @@ use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; -use PHPStan\Type\TypeWithClassName; use function count; class TypeNodeResolverExtension implements \PHPStan\PhpDoc\TypeNodeResolverExtension, TypeNodeResolverAwareExtension @@ -36,13 +35,14 @@ public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type $types = $this->typeNodeResolver->resolveMultiple($typeNode->types, $nameScope); foreach ($types as $type) { - if (!$type instanceof TypeWithClassName) { + $classNames = $type->getObjectClassNames(); + if (count($classNames) !== 1) { continue; } if ( count($types) === 2 - && $type->getClassName() === 'Mockery\\MockInterface' + && $classNames[0] === 'Mockery\\MockInterface' ) { return TypeCombinator::intersect(...$types); } diff --git a/src/Mockery/Type/MockDynamicNamedMockReturnTypeExtension.php b/src/Mockery/Type/MockDynamicNamedMockReturnTypeExtension.php index 67d8c04..ed51007 100644 --- a/src/Mockery/Type/MockDynamicNamedMockReturnTypeExtension.php +++ b/src/Mockery/Type/MockDynamicNamedMockReturnTypeExtension.php @@ -5,7 +5,6 @@ use PhpParser\Node\Expr\StaticCall; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; -use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; @@ -46,11 +45,12 @@ public function getTypeFromStaticMethodCall( $types = [$defaultReturnType]; foreach ($args as $arg) { $classType = $scope->getType($arg->value); - if (!$classType instanceof ConstantStringType) { + $constantStrings = $classType->getConstantStrings(); + if (count($constantStrings) !== 1) { continue; } - $value = $classType->getValue(); + $value = $constantStrings[0]->getValue(); if (substr($value, 0, 6) === 'alias:') { $value = substr($value, 6); } diff --git a/src/Mockery/Type/MockDynamicReturnTypeExtension.php b/src/Mockery/Type/MockDynamicReturnTypeExtension.php index ce4aa94..e2913e3 100644 --- a/src/Mockery/Type/MockDynamicReturnTypeExtension.php +++ b/src/Mockery/Type/MockDynamicReturnTypeExtension.php @@ -5,7 +5,6 @@ use PhpParser\Node\Expr\StaticCall; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; -use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; @@ -47,11 +46,12 @@ public function getTypeFromStaticMethodCall( $types = [$defaultReturnType]; foreach ($methodCall->getArgs() as $arg) { $classType = $scope->getType($arg->value); - if (!$classType instanceof ConstantStringType) { + $constantStrings = $classType->getConstantStrings(); + if (count($constantStrings) !== 1) { continue; } - $value = $classType->getValue(); + $value = $constantStrings[0]->getValue(); if (substr($value, 0, 6) === 'alias:') { $value = substr($value, 6); } diff --git a/src/Mockery/Type/StubDynamicReturnTypeExtension.php b/src/Mockery/Type/StubDynamicReturnTypeExtension.php index 5d7bbdb..250b797 100644 --- a/src/Mockery/Type/StubDynamicReturnTypeExtension.php +++ b/src/Mockery/Type/StubDynamicReturnTypeExtension.php @@ -5,12 +5,11 @@ use PhpParser\Node\Expr\MethodCall; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\DynamicMethodReturnTypeExtension; -use PHPStan\Type\IntersectionType; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; -use PHPStan\Type\TypeWithClassName; +use function array_filter; +use function array_values; use function count; class StubDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension @@ -38,16 +37,14 @@ public function isMethodSupported(MethodReflection $methodReflection): bool return $methodReflection->getName() === $this->stubMethodName; } - public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type { - $calledOnType = $scope->getType($methodCall->var); - $defaultType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); - if (!$calledOnType instanceof IntersectionType || count($calledOnType->getTypes()) !== 2) { - return $defaultType; - } - $mockedType = $calledOnType->getTypes()[1]; - if (!$mockedType instanceof TypeWithClassName) { - return $defaultType; + $calledOnType = $scope->getType($methodCall->var)->getObjectClassNames(); + $names = array_values(array_filter($calledOnType, static function (string $name) { + return $name !== 'Mockery\\MockInterface'; + })); + if (count($names) !== 1) { + return null; } return new ObjectType($this->stubInterfaceName);