Skip to content

Fix ImpossibleInstanceOfRule #4154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/Rules/Classes/ImpossibleInstanceOfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
use PHPStan\Parser\LastConditionVisitor;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
Expand All @@ -25,6 +28,7 @@ final class ImpossibleInstanceOfRule implements Rule
{

public function __construct(
private RuleLevelHelper $ruleLevelHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter]
Expand All @@ -42,11 +46,6 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$instanceofType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node) : $scope->getNativeType($node);
if (!$instanceofType instanceof ConstantBooleanType) {
return [];
}

if ($node->class instanceof Node\Name) {
$className = $scope->resolveName($node->class);
$classType = new ObjectType($className);
Expand All @@ -56,7 +55,13 @@ public function processNode(Node $node, Scope $scope): array
new StringType(),
new ObjectWithoutClassType(),
);
if (!$allowed->isSuperTypeOf($classType)->yes()) {
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->class,
'',
static fn (Type $type): bool => !$allowed->isSuperTypeOf($type)->yes(),
);
if (!$typeResult->getType() instanceof ErrorType && !$allowed->isSuperTypeOf($typeResult->getType())->yes()) {
return [
RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s results in an error.',
Expand All @@ -67,6 +72,11 @@ public function processNode(Node $node, Scope $scope): array
}
}

$instanceofType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node) : $scope->getNativeType($node);
if (!$instanceofType instanceof ConstantBooleanType) {
return [];
}

$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
Expand Down
28 changes: 26 additions & 2 deletions tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Rules\Classes;

use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhp;
Expand All @@ -19,7 +20,10 @@ class ImpossibleInstanceOfRuleTest extends RuleTestCase

protected function getRule(): Rule
{
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, false, false, false, true);

return new ImpossibleInstanceOfRule(
$ruleLevelHelper,
$this->treatPhpDocTypesAsCertain,
$this->reportAlwaysTrueInLastCondition,
true,
Expand Down Expand Up @@ -151,14 +155,14 @@ public function testInstanceof(): void
'Instanceof between ImpossibleInstanceOf\Bar and ImpossibleInstanceOf\BarGrandChild will always evaluate to false.',
322,
],
/*[
[
'Instanceof between mixed and int results in an error.',
353,
],
[
'Instanceof between mixed and ImpossibleInstanceOf\InvalidTypeTest|int results in an error.',
362,
],*/
],
[
'Instanceof between ImpossibleInstanceOf\Foo and ImpossibleInstanceOf\Foo will always evaluate to true.',
388,
Expand Down Expand Up @@ -496,6 +500,26 @@ public function testBug3632(): void
]);
}

public function testBug10036(): void
{
$this->treatPhpDocTypesAsCertain = true;

$this->analyse([__DIR__ . '/data/bug-10036.php'], [
[
'Instanceof between stdClass and string|null results in an error.',
11,
],
[
'Instanceof between stdClass and string|null results in an error.',
19,
],
[
'Instanceof between stdClass and array results in an error.',
39,
],
]);
}

#[RequiresPhp('>= 8.0')]
public function testNewIsAlwaysFinalClass(): void
{
Expand Down
41 changes: 41 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-10036.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Bug10036;

use stdClass;

class HelloWorld
{
public function sayHello(?string $class): void
{
var_dump(new stdClass instanceof $class);
}

/**
* @param string|null $class
*/
public function sayWorld($class): void
{
var_dump(new stdClass instanceof $class);
}

public function sayString(string $class): void
{
var_dump(new stdClass instanceof $class);
}

public function sayMixed(mixed $class): void
{
var_dump(new stdClass instanceof $class);
}

public function sayObject(object $class): void
{
var_dump(new stdClass instanceof $class);
}

public function sayArray(array $class): void
{
var_dump(new stdClass instanceof $class);
}
}
Loading