Skip to content
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

Support string accessory types in BitwiseNot #1266

Merged
merged 4 commits into from May 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Analyser/MutatingScope.php
Expand Up @@ -687,8 +687,17 @@ private function resolveType(Expr $node): Type
if ($type instanceof ConstantStringType) {
return new ConstantStringType(~$type->getValue());
}
if ($type instanceof StringType) {
return new StringType();
if ($type->isString()->yes()) {
$accessories = [
new StringType(),
];
if ($type->isNonEmptyString()->yes()) {
$accessories[] = new AccessoryNonEmptyStringType();
}
// it is not useful to apply numeric and literal strings here.
// numeric string isn't certainly kept numeric: 3v4l.org/JERDB

return TypeCombinator::intersect(...$accessories);
}
if ($type instanceof IntegerType || $type instanceof FloatType) {
return new IntegerType(); //no const types here, result depends on PHP_INT_SIZE
Expand Down
4 changes: 3 additions & 1 deletion tests/PHPStan/Analyser/data/bitwise-not.php
Expand Up @@ -6,10 +6,12 @@

/**
* @param string|int $stringOrInt
* @param non-empty-string $nonEmptyString
*/
function foo(int $int, string $string, float $float, $stringOrInt) : void{
function foo(int $int, string $string, float $float, $stringOrInt, string $nonEmptyString) : void{
assertType('int', ~$int);
assertType('string', ~$string);
assertType('non-empty-string', ~$nonEmptyString);
assertType('int', ~$float);
assertType('int|string', ~$stringOrInt);
assertType("'" . (~"abc") . "'", ~"abc");
Expand Down