Skip to content

More precise constant variants of BooleanType #3781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, see https://3v4l.org/ipPIP

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));
}
}
Loading