Skip to content

Commit c0c9d1f

Browse files
committed
More precise constant variants of BooleanType
1 parent 5a87c64 commit c0c9d1f

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/Type/BooleanType.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ public function __construct()
4141
{
4242
}
4343

44+
public function getConstantScalarTypes(): array
45+
{
46+
return [new ConstantBooleanType(true), new ConstantBooleanType(false)];
47+
}
48+
4449
public function getConstantStrings(): array
4550
{
46-
return [];
51+
return [new ConstantStringType('1'), new ConstantStringType('0')];
52+
}
53+
54+
public function getConstantScalarValues(): array
55+
{
56+
return [true, false];
4757
}
4858

4959
public function describe(VerbosityLevel $level): string

src/Type/Php/StrlenFunctionReturnTypeExtension.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
8-
use PHPStan\Type\BooleanType;
9-
use PHPStan\Type\Constant\ConstantBooleanType;
108
use PHPStan\Type\Constant\ConstantIntegerType;
119
use PHPStan\Type\Constant\ConstantStringType;
1210
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
@@ -44,16 +42,7 @@ public function getTypeFromFunctionCall(
4442
}
4543

4644
$argType = $scope->getType($args[0]->value);
47-
48-
if ($argType->isSuperTypeOf(new BooleanType())->yes()) {
49-
$constantScalars = TypeCombinator::remove($argType, new BooleanType())->getConstantScalarTypes();
50-
if (count($constantScalars) > 0) {
51-
$constantScalars[] = new ConstantBooleanType(true);
52-
$constantScalars[] = new ConstantBooleanType(false);
53-
}
54-
} else {
55-
$constantScalars = $argType->getConstantScalarTypes();
56-
}
45+
$constantScalars = $argType->getConstantScalarTypes();
5746

5847
$lengths = [];
5948
foreach ($constantScalars as $constantScalar) {

tests/PHPStan/Analyser/nsrt/bug-11201.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ function returnsBool(): bool {
5353
assertType("' 1'", $s);
5454

5555
$s = sprintf('%20s', returnsBool());
56-
assertType("lowercase-string&non-falsy-string", $s);
56+
assertType("' '|' 1'", $s);

0 commit comments

Comments
 (0)