Skip to content

Commit

Permalink
BooleanType - implement getConstantScalarTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Jan 11, 2025
1 parent 5a87c64 commit 76740fd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/Type/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public function getConstantStrings(): array
return [];
}

public function getConstantScalarTypes(): array
{
return [new ConstantBooleanType(true), new ConstantBooleanType(false)];
}

public function getConstantScalarValues(): array
{
return [true, false];
}

public function describe(VerbosityLevel $level): string
{
return 'bool';
Expand Down
12 changes: 1 addition & 11 deletions src/Type/Php/MbStrlenFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -93,16 +92,7 @@ public function getTypeFromFunctionCall(
}

$argType = $scope->getType($args[0]->value);

if ($argType->isSuperTypeOf(new BooleanType())->yes()) {
$constantScalars = TypeCombinator::remove($argType, new BooleanType())->getConstantScalarTypes();
if (count($constantScalars) > 0) {
$constantScalars[] = new ConstantBooleanType(true);
$constantScalars[] = new ConstantBooleanType(false);
}
} else {
$constantScalars = $argType->getConstantScalarTypes();
}
$constantScalars = $argType->getConstantScalarTypes();

$lengths = [];
foreach ($constantScalars as $constantScalar) {
Expand Down
13 changes: 1 addition & 12 deletions src/Type/Php/StrlenFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
Expand Down Expand Up @@ -44,16 +42,7 @@ public function getTypeFromFunctionCall(
}

$argType = $scope->getType($args[0]->value);

if ($argType->isSuperTypeOf(new BooleanType())->yes()) {
$constantScalars = TypeCombinator::remove($argType, new BooleanType())->getConstantScalarTypes();
if (count($constantScalars) > 0) {
$constantScalars[] = new ConstantBooleanType(true);
$constantScalars[] = new ConstantBooleanType(false);
}
} else {
$constantScalars = $argType->getConstantScalarTypes();
}
$constantScalars = $argType->getConstantScalarTypes();

$lengths = [];
foreach ($constantScalars as $constantScalar) {
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10952b.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,14 @@ public function test(): void
mb_strlen($string) > 0 => assertType('non-empty-string', $string),
default => assertType("''", $string),
};

assertType('int<0, 1>', strlen($this->getBool()));
assertType('int<0, 1>', mb_strlen($this->getBool()));
}

public function getBool(): bool
{
return rand(0, 1) === 1;
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-11201.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ function returnsBool(): bool {
assertType("' 1'", $s);

$s = sprintf('%20s', returnsBool());
assertType("lowercase-string&non-falsy-string", $s);
assertType("' '|' 1'", $s);
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/nsrt/implode.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public function constArrays5($constArr) {
public function constArrays6($constArr) {
assertType("string", implode('', $constArr));
}

/** @param array{10: 1|2|bool, xy: 'a'|'b'|'c'} $constArr */
public function constArrays7($constArr) {
assertType("'1a'|'1b'|'1c'|'2a'|'2b'|'2c'|'a'|'b'|'c'", implode('', $constArr));
}
}

0 comments on commit 76740fd

Please sign in to comment.