Skip to content

Commit

Permalink
Merge branch '1.6.x' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 15, 2022
2 parents 0ecddcc + 92420cd commit fc839cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -623,8 +623,17 @@ public function getBitwiseNotType(Expr $expr, callable $getTypeCallback): 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 fc839cf

Please sign in to comment.