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 c5ace3a commit db436df
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
17 changes: 3 additions & 14 deletions src/Rules/PHPUnit/MockMethodCallRule.php
Expand Up @@ -6,9 +6,7 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
Expand Down Expand Up @@ -71,24 +69,15 @@ public function processNode(Node $node, Scope $scope): array
);
}

if (
!($type instanceof GenericObjectType)
|| $type->getClassName() !== InvocationMocker::class
|| count($type->getTypes()) <= 0
) {
continue;
}

$mockClass = $type->getTypes()[0];

if (!($mockClass instanceof ObjectType) || $mockClass->hasMethod($method)->yes()) {
$mockedClassObject = $type->getTemplateType(InvocationMocker::class, 'TMockedClass');
if ($mockedClassObject->hasMethod($method)->yes()) {
continue;
}

$errors[] = sprintf(
'Trying to mock an undefined method %s() on class %s.',
$method,
$mockClass->getClassName()
implode('|', $mockedClassObject->getObjectClassNames())
);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php
Expand Up @@ -18,7 +18,6 @@
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Type\Constant\ConstantStringType;
use ReflectionObject;
use function array_key_exists;
use function count;
Expand Down Expand Up @@ -125,14 +124,15 @@ private static function getExpressionResolvers(): array
if (self::$resolvers === null) {
self::$resolvers = [
'InstanceOf' => static function (Scope $scope, Arg $class, Arg $object): ?Instanceof_ {
$classType = $scope->getType($class->value);
if (!$classType instanceof ConstantStringType) {
$classType = $scope->getType($class->value)->getClassStringObjectType();
$classNames = $classType->getObjectClassNames();
if (count($classNames) !== 1) {
return null;
}

return new Instanceof_(
$object->value,
new Name($classType->getValue())
new Name($classNames[0])
);
},
'Same' => static function (Scope $scope, Arg $expected, Arg $actual): Identical {
Expand Down Expand Up @@ -205,12 +205,12 @@ private static function getExpressionResolvers(): array
return new FuncCall(new Name('is_scalar'), [$actual]);
},
'InternalType' => static function (Scope $scope, Arg $type, Arg $value): ?FuncCall {
$typeType = $scope->getType($type->value);
if (!$typeType instanceof ConstantStringType) {
$typeNames = $scope->getType($type->value)->getConstantStrings();
if (count($typeNames) !== 1) {
return null;
}

switch ($typeType->getValue()) {
switch ($typeNames[0]->getValue()) {
case 'numeric':
$functionName = 'is_numeric';
break;
Expand Down

0 comments on commit db436df

Please sign in to comment.