Skip to content

Commit 6aa86bd

Browse files
committed
Do not use instanceof *Type
1 parent 204dea9 commit 6aa86bd

4 files changed

+18
-21
lines changed

src/Mockery/PhpDoc/TypeNodeResolverExtension.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
1010
use PHPStan\Type\Type;
1111
use PHPStan\Type\TypeCombinator;
12-
use PHPStan\Type\TypeWithClassName;
1312
use function count;
1413

1514
class TypeNodeResolverExtension implements \PHPStan\PhpDoc\TypeNodeResolverExtension, TypeNodeResolverAwareExtension
@@ -36,13 +35,14 @@ public function resolve(TypeNode $typeNode, NameScope $nameScope): ?Type
3635

3736
$types = $this->typeNodeResolver->resolveMultiple($typeNode->types, $nameScope);
3837
foreach ($types as $type) {
39-
if (!$type instanceof TypeWithClassName) {
38+
$classNames = $type->getObjectClassNames();
39+
if (count($classNames) !== 1) {
4040
continue;
4141
}
4242

4343
if (
4444
count($types) === 2
45-
&& $type->getClassName() === 'Mockery\\MockInterface'
45+
&& $classNames[0] === 'Mockery\\MockInterface'
4646
) {
4747
return TypeCombinator::intersect(...$types);
4848
}

src/Mockery/Type/MockDynamicNamedMockReturnTypeExtension.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\StaticCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Type\Constant\ConstantStringType;
98
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
109
use PHPStan\Type\ObjectType;
1110
use PHPStan\Type\Type;
@@ -46,11 +45,12 @@ public function getTypeFromStaticMethodCall(
4645
$types = [$defaultReturnType];
4746
foreach ($args as $arg) {
4847
$classType = $scope->getType($arg->value);
49-
if (!$classType instanceof ConstantStringType) {
48+
$constantStrings = $classType->getConstantStrings();
49+
if (count($constantStrings) !== 1) {
5050
continue;
5151
}
5252

53-
$value = $classType->getValue();
53+
$value = $constantStrings[0]->getValue();
5454
if (substr($value, 0, 6) === 'alias:') {
5555
$value = substr($value, 6);
5656
}

src/Mockery/Type/MockDynamicReturnTypeExtension.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\StaticCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Type\Constant\ConstantStringType;
98
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
109
use PHPStan\Type\ObjectType;
1110
use PHPStan\Type\Type;
@@ -47,11 +46,12 @@ public function getTypeFromStaticMethodCall(
4746
$types = [$defaultReturnType];
4847
foreach ($methodCall->getArgs() as $arg) {
4948
$classType = $scope->getType($arg->value);
50-
if (!$classType instanceof ConstantStringType) {
49+
$constantStrings = $classType->getConstantStrings();
50+
if (count($constantStrings) !== 1) {
5151
continue;
5252
}
5353

54-
$value = $classType->getValue();
54+
$value = $constantStrings[0]->getValue();
5555
if (substr($value, 0, 6) === 'alias:') {
5656
$value = substr($value, 6);
5757
}

src/Mockery/Type/StubDynamicReturnTypeExtension.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Reflection\ParametersAcceptorSelector;
98
use PHPStan\Type\DynamicMethodReturnTypeExtension;
10-
use PHPStan\Type\IntersectionType;
119
use PHPStan\Type\ObjectType;
1210
use PHPStan\Type\Type;
13-
use PHPStan\Type\TypeWithClassName;
11+
use function array_filter;
12+
use function array_values;
1413
use function count;
1514

1615
class StubDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -38,16 +37,14 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3837
return $methodReflection->getName() === $this->stubMethodName;
3938
}
4039

41-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
40+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
4241
{
43-
$calledOnType = $scope->getType($methodCall->var);
44-
$defaultType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
45-
if (!$calledOnType instanceof IntersectionType || count($calledOnType->getTypes()) !== 2) {
46-
return $defaultType;
47-
}
48-
$mockedType = $calledOnType->getTypes()[1];
49-
if (!$mockedType instanceof TypeWithClassName) {
50-
return $defaultType;
42+
$calledOnType = $scope->getType($methodCall->var)->getObjectClassNames();
43+
$names = array_values(array_filter($calledOnType, static function (string $name) {
44+
return $name !== 'Mockery\\MockInterface';
45+
}));
46+
if (count($names) !== 1) {
47+
return null;
5148
}
5249

5350
return new ObjectType($this->stubInterfaceName);

0 commit comments

Comments
 (0)