Skip to content

Commit 43fca8d

Browse files
jlherrenondrejmirtes
authored andcommitted
Concat between numeric and empty string produces numeric string
1 parent 3c6df85 commit 43fca8d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Analyser/MutatingScope.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,14 @@ private function resolveType(Expr $node): Type
912912
return new ErrorType();
913913
}
914914

915+
if ($leftStringType instanceof ConstantStringType && $leftStringType->getValue() === '') {
916+
return $rightStringType;
917+
}
918+
919+
if ($rightStringType instanceof ConstantStringType && $rightStringType->getValue() === '') {
920+
return $leftStringType;
921+
}
922+
915923
if ($leftStringType instanceof ConstantStringType && $rightStringType instanceof ConstantStringType) {
916924
return $leftStringType->append($rightStringType);
917925
}

tests/PHPStan/Analyser/data/cast-to-numeric-string.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,40 @@ function foo(int $a, float $b, $numeric, $numeric2, $number, $positive, $negativ
2222
assertType('string&numeric', (string)$negative);
2323
assertType("'1'", (string)$constantInt);
2424
}
25+
26+
/**
27+
* @param int|float|numeric-string $numeric
28+
* @param numeric $numeric2
29+
* @param number $number
30+
* @param positive-int $positive
31+
* @param negative-int $negative
32+
* @param 1 $constantInt
33+
*/
34+
function concatEmptyString(int $a, float $b, $numeric, $numeric2, $number, $positive, $negative, $constantInt): void {
35+
assertType('string&numeric', '' . $a);
36+
assertType('string&numeric', '' . $b);
37+
assertType('string&numeric', '' . $numeric);
38+
assertType('string&numeric', '' . $numeric2);
39+
assertType('string&numeric', '' . $number);
40+
assertType('string&numeric', '' . $positive);
41+
assertType('string&numeric', '' . $negative);
42+
assertType("'1'", '' . $constantInt);
43+
44+
assertType('string&numeric', $a . '');
45+
assertType('string&numeric', $b . '');
46+
assertType('string&numeric', $numeric . '');
47+
assertType('string&numeric', $numeric2 . '');
48+
assertType('string&numeric', $number . '');
49+
assertType('string&numeric', $positive . '');
50+
assertType('string&numeric', $negative . '');
51+
assertType("'1'", $constantInt . '');
52+
}
53+
54+
function concatAssignEmptyString(int $i, float $f) {
55+
$i .= '';
56+
assertType('string&numeric', $i);
57+
58+
$s = '';
59+
$s .= $f;
60+
assertType('string&numeric', $s);
61+
}

0 commit comments

Comments
 (0)