Skip to content

Commit

Permalink
Do not use instanceof *Type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 18, 2023
1 parent 204dea9 commit 6aa86bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/Mockery/PhpDoc/TypeNodeResolverExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mockery/Type/MockDynamicNamedMockReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Mockery/Type/MockDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 9 additions & 12 deletions src/Mockery/Type/StubDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6aa86bd

Please sign in to comment.