Skip to content

Commit 2acf585

Browse files
committed
Fix DateTime::format('u') return type
1 parent db46c94 commit 2acf585

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/Type/Php/DateFunctionReturnTypeHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function buildReturnTypeFromFormat(string $formatString, bool $useMicrose
7676
return $this->buildNumericRangeType(0, 1, false);
7777
case 'u':
7878
return $useMicrosec
79-
? new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()])
79+
? new IntersectionType([new StringType(), new AccessoryNumericStringType()])
8080
: new ConstantStringType('000000');
8181
}
8282

tests/PHPStan/Analyser/nsrt/bug-10893.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
use function PHPStan\Testing\assertType;
66

77
/**
8-
* @param non-falsy-string $nonfalsy
8+
* @param numeric-string $numericString
99
*/
10-
function hasMicroseconds(\DateTimeInterface $value, string $nonfalsy): bool
10+
function hasMicroseconds(\DateTimeInterface $value, string $numericString): bool
1111
{
12-
assertType('non-falsy-string', $value->format('u'));
12+
assertType('numeric-string', $value->format('u'));
1313
assertType('int', (int)$value->format('u'));
1414
assertType('bool', (int)$value->format('u') !== 0);
15-
assertType('non-falsy-string', $nonfalsy);
16-
assertType('int', (int)$nonfalsy);
17-
assertType('bool', (int)$nonfalsy !== 0);
15+
assertType('numeric-string', $numericString);
16+
assertType('int', (int)$numericString);
17+
assertType('bool', (int)$numericString !== 0);
18+
19+
assertType('float', $value->format('u') * 1e-6);
1820

1921
return (int) $value->format('u') !== 0;
2022
}

tests/PHPStan/Analyser/nsrt/bug-6613.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
function (\DateTime $dt) {
88
assertType("'000000'", date('u'));
9-
assertType('non-falsy-string', date_format($dt, 'u'));
10-
assertType('non-falsy-string', $dt->format('u'));
9+
assertType('numeric-string', date_format($dt, 'u'));
10+
assertType('numeric-string', $dt->format('u'));
1111
};

0 commit comments

Comments
 (0)