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

Fix ImpossibleCheckTypeFunctionCallRule for is_subclass and is_a #3788

Open
wants to merge 3 commits into
base: 1.12.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
4 changes: 0 additions & 4 deletions src/Rules/Comparison/ImpossibleCheckTypeFunctionCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
use function strtolower;

/**
* @implements Rule<Node\Expr\FuncCall>
Expand Down Expand Up @@ -38,9 +37,6 @@ public function processNode(Node $node, Scope $scope): array
}

$functionName = (string) $node->name;
if (strtolower($functionName) === 'is_a') {
return [];
}
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node);
if ($isAlways === null) {
return [];
Expand Down
9 changes: 8 additions & 1 deletion src/Type/Php/IsAFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$allowStringType = isset($node->getArgs()[2]) ? $scope->getType($node->getArgs()[2]->value) : new ConstantBooleanType(false);
$allowString = !$allowStringType->equals(new ConstantBooleanType(false));

$resultType = $this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, true);

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
if ($classType->getConstantStrings() === [] && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
return new SpecifiedTypes([], []);
}

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, true),
$resultType,
$context,
false,
$scope,
Expand Down
9 changes: 8 additions & 1 deletion src/Type/Php/IsSubclassOfFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
return new SpecifiedTypes([], []);
}

$resultType = $this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, false);

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
if ($classType->getConstantStrings() === [] && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
return new SpecifiedTypes([], []);
}

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, false),
$resultType,
$context,
false,
$scope,
Expand Down
4 changes: 1 addition & 3 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,9 +1133,7 @@ public function dataCondition(): iterable
new Arg(new Variable('stringOrNull')),
new Arg(new Expr\ConstFetch(new Name('false'))),
]),
[
'$object' => 'object',
],
[],
[],
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,4 +1102,41 @@ public function testAlwaysTruePregMatch(): void
$this->analyse([__DIR__ . '/data/always-true-preg-match.php'], []);
}

public function testBug3979(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-3979.php'], []);
}

public function testBug8464(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-8464.php'], []);
}

public function testBug8954(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-8954.php'], []);
}

public function testBugPR3404(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-pr-3404.php'], [
[
'Call to function is_a() with arguments BugPR3404\Location, \'BugPR3404\\\\Location\' and true will always evaluate to true.',
21,
],
]);
}

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

namespace Bug3979;

class A { }
class B extends A { }
class C { }

/**
* @param mixed $value
* @param string $class_type
*/
function check_class($value, $class_type): bool
{
if (!is_string($value) || !class_exists($value) ||
($class_type && !is_subclass_of($value, $class_type)))
return false;
return true;
}

var_dump(check_class("B", "A")); // true
var_dump(check_class("C", "A")); // false

/**
* @param class-string $value
* @param string $class_type
*/
function check_class2($value, $class_type): bool
{
if (is_a($value, $class_type, true)) {
return true;
}
return false;
}

/**
* @param class-string|object $value
* @param string $class_type
*/
function check_class3($value, $class_type): bool
{
if (is_a($value, $class_type, true)) {
return true;
}
return false;
}

/**
* @param class-string|object $value
* @param string $class_type
*/
function check_class4($value, $class_type): bool
{
if (is_subclass_of($value, $class_type, true)) {
return true;
}
return false;
}

/**
* @param object $value
* @param string $class_type
*/
function check_class5($value, $class_type): bool
{
if (is_a($value, $class_type, true)) {
return true;
}
return false;
}

/**
* @param object $value
* @param string $class_type
*/
function check_class6($value, $class_type): bool
{
if (is_subclass_of($value, $class_type, true)) {
return true;
}
return false;
}

/**
* @param object $value
* @param class-string $class_type
*/
function check_class7($value, $class_type): bool
{
if (is_a($value, $class_type, true)) {
return true;
}
return false;
}

/**
* @param object $value
* @param class-string $class_type
*/
function check_class8($value, $class_type): bool
{
if (is_subclass_of($value, $class_type, true)) {
return true;
}
return false;
}

/**
* @param class-string $value
* @param class-string $class_type
*/
function check_class9($value, $class_type): bool
{
if (is_a($value, $class_type, true)) {
return true;
}
return false;
}

/**
* @param class-string $value
* @param class-string $class_type
*/
function check_class10($value, $class_type): bool
{
if (is_subclass_of($value, $class_type, true)) {
return true;
}
return false;
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8464.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php // lint >= 8.0

namespace Bug8464;

final class ObjectUtil
{
/**
* @param class-string $type
*/
public static function instanceOf(mixed $object, string $type): bool
{
return \is_object($object)
&& (
$object::class === $type ||
is_subclass_of($object, $type)
);
}
}
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8954.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Bug8954;

/**
* @template U
* @template V
*
* @param ?class-string<U> $class
* @param class-string<V> $expected
*
* @return ?class-string<V>
*/
function ensureSubclassOf(?string $class, string $expected): ?string {
if ($class === null) {
return $class;
}

if (!class_exists($class)) {
throw new \Exception("Class “{$class}” does not exist.");
}

if (!is_subclass_of($class, $expected)) {
throw new \Exception("Class “{$class}” is not a subclass of “{$expected}”.");
}

return $class;
}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-pr-3404.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 8.0

namespace BugPR3404;

interface Location
{

}

/** @return class-string<Location> */
function aaa(): string
{

}

function (Location $l): void {
if (is_a($l, aaa(), true)) {
// might not always be true. $l might be one subtype of Location, aaa() might return a name of a different subtype of Location
}

if (is_a($l, Location::class, true)) {
// always true
}
};
Loading