Skip to content

Commit ca044c4

Browse files
committed
Fixed assert() bug
1 parent 05942ca commit ca044c4

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Analyser\TypeSpecifierContext;
1212
use PHPStan\Reflection\ReflectionProvider;
1313
use PHPStan\Type\Constant\ConstantArrayType;
14+
use PHPStan\Type\Constant\ConstantBooleanType;
1415
use PHPStan\Type\Constant\ConstantStringType;
1516
use PHPStan\Type\MixedType;
1617
use PHPStan\Type\NeverType;
@@ -63,7 +64,12 @@ public function findSpecifiedType(
6364
if ($node->name instanceof \PhpParser\Node\Name) {
6465
$functionName = strtolower((string) $node->name);
6566
if ($functionName === 'assert') {
66-
return $this->findSpecifiedType($scope, $node->args[0]->value);
67+
$assertValue = $scope->getType($node->args[0]->value)->toBoolean();
68+
if (!$assertValue instanceof ConstantBooleanType) {
69+
return null;
70+
}
71+
72+
return $assertValue->getValue();
6773
}
6874
if (in_array($functionName, [
6975
'class_exists',

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,6 @@ public function testImpossibleCheckTypeFunctionCall(): void
234234
'Call to function property_exists() with CheckTypeFunctionCall\Bug2221 and \'foo\' will always evaluate to true.',
235235
782,
236236
],
237-
[
238-
'Call to function assert() with bool will always evaluate to true.',
239-
786,
240-
],
241237
[
242238
'Call to function property_exists() with CheckTypeFunctionCall\Bug2221 and \'foo\' will always evaluate to true.',
243239
786,
@@ -382,4 +378,11 @@ public function testBug2550(): void
382378
$this->analyse([__DIR__ . '/data/bug-2550.php'], []);
383379
}
384380

381+
public function testBug3994(): void
382+
{
383+
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
384+
$this->treatPhpDocTypesAsCertain = true;
385+
$this->analyse([__DIR__ . '/data/bug-3994.php'], []);
386+
}
387+
385388
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bug3994;
4+
5+
class HelloWorld
6+
{
7+
8+
public function split(string $str): void
9+
{
10+
$parts = explode(".", $str);
11+
assert(count($parts) === 4);
12+
}
13+
14+
}

0 commit comments

Comments
 (0)