Skip to content
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

Classname in instanceof cannot be null #2769

Draft
wants to merge 3 commits into
base: 1.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion src/Rules/Classes/ImpossibleInstanceOfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$node->class instanceof Node\Name) {
Copy link
Contributor Author

@staabm staabm Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this needs to be BleedingEdge only? reworked into a level 8 rule instead

$classType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->class) : $scope->getNativeType($node->class);
if (TypeCombinator::containsNull($classType)) {
return [
RuleErrorBuilder::message(sprintf(
'Classname in instanceof cannot be null, %s given.',
$classType->describe(VerbosityLevel::typeOnly()),
))->build(),
];
}
}

$instanceofType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node) : $scope->getNativeType($node);
if (!$instanceofType instanceof ConstantBooleanType) {
return [];
Expand All @@ -45,7 +57,6 @@ public function processNode(Node $node, Scope $scope): array
$className = $scope->resolveName($node->class);
$classType = new ObjectType($className);
} else {
$classType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->class) : $scope->getNativeType($node->class);
$allowed = TypeCombinator::union(
new StringType(),
new ObjectWithoutClassType(),
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,32 @@ public function testReportAlwaysTrueInLastCondition(bool $reportAlwaysTrueInLast
$this->analyse([__DIR__ . '/data/impossible-instanceof-report-always-true-last-condition.php'], $expectedErrors);
}

public function testNullableClassname(): void
{
$this->checkAlwaysTrueInstanceOf = true;
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/bug-10036.php'], [
[
'Classname in instanceof cannot be null, string|null given.',
9,
],
]);
}

public function testNullablePhpdocClassname(): void
{
$this->checkAlwaysTrueInstanceOf = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-10036.php'], [
[
'Classname in instanceof cannot be null, string|null given.',
9,
],
[
'Classname in instanceof cannot be null, string|null given.',
17,
],
]);
}

}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-10036.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Bug10036;

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was not sure whether we want the rule error on mixed

}
}