Skip to content

Commit

Permalink
Support string accessory types in BitwiseNot
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 15, 2022
1 parent e46c6d3 commit 92420cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
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

0 comments on commit 92420cd

Please sign in to comment.