Skip to content

Commit 3abfc16

Browse files
committed
fixup! Improve abs() return type
1 parent 076ead6 commit 3abfc16

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/PHPStan/Analyser/nsrt/abs.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public function constantInteger(int $int): void
9292

9393
/** @var -1 $int */
9494
assertType('1', abs($int));
95+
96+
assertType('123', abs(123));
97+
98+
assertType('123', abs(-123));
9599
}
96100

97101
public function mixedIntegerUnion(int $int): void
@@ -119,6 +123,10 @@ public function constantFloat(float $float): void
119123

120124
/** @var -1.0 $float */
121125
assertType('1.0', abs($float));
126+
127+
assertType('123.4', abs(123.4));
128+
129+
assertType('123.4', abs(-123.4));
122130
}
123131

124132
public function string(string $string): void
@@ -134,6 +142,19 @@ public function string(string $string): void
134142

135143
/** @var '-1'|'-2.0'|'3.0'|'4' $string */
136144
assertType('1|2.0|3.0|4', abs($string));
145+
146+
/** @var literal-string $string */
147+
assertType('float|int<0, max>', abs($string));
148+
149+
assertType('123', abs('123'));
150+
151+
assertType('123', abs('-123'));
152+
153+
assertType('123.0', abs('123.0'));
154+
155+
assertType('123.0', abs('-123.0'));
156+
157+
assertType('float|int<0, max>', abs('foo'));
137158
}
138159

139160
public function mixedUnion(mixed $value): void
@@ -182,6 +203,13 @@ public function invalidType(mixed $nonInt): void
182203

183204
/** @var \DateTime $nonInt */
184205
assertType('float|int<0, max>', abs($nonInt));
206+
207+
/** @var null $nonInt */
208+
assertType('0', abs($nonInt));
209+
210+
assertType('float|int<0, max>', abs('foo'));
211+
212+
assertType('0', abs(null));
185213
}
186214

187215
}

0 commit comments

Comments
 (0)