Skip to content

Commit 10eb37a

Browse files
Solve bug 3979
1 parent eeb46c0 commit 10eb37a

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/Rules/Comparison/ImpossibleCheckTypeFunctionCallRule.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Parser\LastConditionVisitor;
88
use PHPStan\Rules\Rule;
99
use PHPStan\Rules\RuleErrorBuilder;
10+
use function in_array;
1011
use function sprintf;
1112
use function strtolower;
1213

@@ -37,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array
3738
}
3839

3940
$functionName = (string) $node->name;
40-
if (strtolower($functionName) === 'is_a') {
41+
if (in_array(strtolower($functionName), ['is_a', 'is_subclass_of'], true)) {
4142
return [];
4243
}
4344
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node);

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

+8-10
Original file line numberDiff line numberDiff line change
@@ -492,16 +492,7 @@ public function testBug6305(): void
492492
{
493493
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
494494
$this->treatPhpDocTypesAsCertain = true;
495-
$this->analyse([__DIR__ . '/data/bug-6305.php'], [
496-
[
497-
'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\A\' will always evaluate to true.',
498-
11,
499-
],
500-
[
501-
'Call to function is_subclass_of() with Bug6305\B and \'Bug6305\\\B\' will always evaluate to false.',
502-
14,
503-
],
504-
]);
495+
$this->analyse([__DIR__ . '/data/bug-6305.php'], []);
505496
}
506497

507498
public function testBug6698(): void
@@ -1101,4 +1092,11 @@ public function testAlwaysTruePregMatch(): void
11011092
$this->analyse([__DIR__ . '/data/always-true-preg-match.php'], []);
11021093
}
11031094

1095+
public function testBug3979(): void
1096+
{
1097+
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
1098+
$this->treatPhpDocTypesAsCertain = true;
1099+
$this->analyse([__DIR__ . '/data/bug-3979.php'], []);
1100+
}
1101+
11041102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Bug3979;
4+
5+
class A { }
6+
class B extends A { }
7+
class C { }
8+
9+
/**
10+
* @param mixed $value
11+
* @param string $class_type
12+
*/
13+
function check_class($value, $class_type): bool
14+
{
15+
if (!is_string($value) || !class_exists($value) ||
16+
($class_type && !is_subclass_of($value, $class_type)))
17+
return false;
18+
return true;
19+
}
20+
21+
var_dump(check_class("B", "A")); // true
22+
var_dump(check_class("C", "A")); // false

0 commit comments

Comments
 (0)