Skip to content

Commit 76740fd

Browse files
authored
BooleanType - implement getConstantScalarTypes
1 parent 5a87c64 commit 76740fd

6 files changed

+27
-24
lines changed

src/Type/BooleanType.php

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public function getConstantStrings(): array
4646
return [];
4747
}
4848

49+
public function getConstantScalarTypes(): array
50+
{
51+
return [new ConstantBooleanType(true), new ConstantBooleanType(false)];
52+
}
53+
54+
public function getConstantScalarValues(): array
55+
{
56+
return [true, false];
57+
}
58+
4959
public function describe(VerbosityLevel $level): string
5060
{
5161
return 'bool';

src/Type/Php/MbStrlenFunctionReturnTypeExtension.php

+1-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPStan\Reflection\FunctionReflection;
99
use PHPStan\Reflection\ParametersAcceptorSelector;
1010
use PHPStan\ShouldNotHappenException;
11-
use PHPStan\Type\BooleanType;
1211
use PHPStan\Type\Constant\ConstantBooleanType;
1312
use PHPStan\Type\Constant\ConstantIntegerType;
1413
use PHPStan\Type\Constant\ConstantStringType;
@@ -93,16 +92,7 @@ public function getTypeFromFunctionCall(
9392
}
9493

9594
$argType = $scope->getType($args[0]->value);
96-
97-
if ($argType->isSuperTypeOf(new BooleanType())->yes()) {
98-
$constantScalars = TypeCombinator::remove($argType, new BooleanType())->getConstantScalarTypes();
99-
if (count($constantScalars) > 0) {
100-
$constantScalars[] = new ConstantBooleanType(true);
101-
$constantScalars[] = new ConstantBooleanType(false);
102-
}
103-
} else {
104-
$constantScalars = $argType->getConstantScalarTypes();
105-
}
95+
$constantScalars = $argType->getConstantScalarTypes();
10696

10797
$lengths = [];
10898
foreach ($constantScalars as $constantScalar) {

src/Type/Php/StrlenFunctionReturnTypeExtension.php

+1-12
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-10952b.php

+9
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,14 @@ public function test(): void
3737
mb_strlen($string) > 0 => assertType('non-empty-string', $string),
3838
default => assertType("''", $string),
3939
};
40+
41+
assertType('int<0, 1>', strlen($this->getBool()));
42+
assertType('int<0, 1>', mb_strlen($this->getBool()));
4043
}
44+
45+
public function getBool(): bool
46+
{
47+
return rand(0, 1) === 1;
48+
}
49+
4150
}

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

+1-1
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);

tests/PHPStan/Analyser/nsrt/implode.php

+5
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ public function constArrays5($constArr) {
5151
public function constArrays6($constArr) {
5252
assertType("string", implode('', $constArr));
5353
}
54+
55+
/** @param array{10: 1|2|bool, xy: 'a'|'b'|'c'} $constArr */
56+
public function constArrays7($constArr) {
57+
assertType("'1a'|'1b'|'1c'|'2a'|'2b'|'2c'|'a'|'b'|'c'", implode('', $constArr));
58+
}
5459
}

0 commit comments

Comments
 (0)