Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Type/Php/DateFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ public function buildReturnTypeFromFormat(string $formatString, bool $useMicrose
return $this->buildNumericRangeType(0, 1, false);
case 'u':
return $useMicrosec
? new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()])
? new IntersectionType([new StringType(), new AccessoryNonFalsyStringType(), new AccessoryNumericStringType()])
: new ConstantStringType('000000');
case 'v':
return $useMicrosec
? new IntersectionType([new StringType(), new AccessoryNonFalsyStringType(), new AccessoryNumericStringType()])
: new ConstantStringType('000');
}

$date = date($formatString);
Expand Down
20 changes: 14 additions & 6 deletions tests/PHPStan/Analyser/nsrt/bug-10893.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@
use function PHPStan\Testing\assertType;

/**
* @param non-falsy-string $nonfalsy
* @param non-falsy-string&numeric-string $str
*/
function hasMicroseconds(\DateTimeInterface $value, string $nonfalsy): bool
function hasMicroseconds(\DateTimeInterface $value, string $str): bool
{
assertType('non-falsy-string', $value->format('u'));
assertType('non-falsy-string&numeric-string', $str);
assertType('int', (int)$str);
assertType('bool', (int)$str !== 0);

assertType('non-falsy-string&numeric-string', $value->format('u'));
assertType('int', (int)$value->format('u'));
assertType('bool', (int)$value->format('u') !== 0);
assertType('non-falsy-string', $nonfalsy);
assertType('int', (int)$nonfalsy);
assertType('bool', (int)$nonfalsy !== 0);

assertType('non-falsy-string&numeric-string', $value->format('v'));
assertType('int', (int)$value->format('v'));
assertType('bool', (int)$value->format('v') !== 0);

assertType('float', $value->format('u') * 1e-6);
assertType('float', $value->format('v') * 1e-3);

return (int) $value->format('u') !== 0;
}
8 changes: 6 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-6613.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

function (\DateTime $dt) {
assertType("'000000'", date('u'));
assertType('non-falsy-string', date_format($dt, 'u'));
assertType('non-falsy-string', $dt->format('u'));
assertType('non-falsy-string&numeric-string', date_format($dt, 'u'));
assertType('non-falsy-string&numeric-string', $dt->format('u'));

assertType("'000'", date('v'));
assertType('non-falsy-string&numeric-string', date_format($dt, 'v'));
assertType('non-falsy-string&numeric-string', $dt->format('v'));
};
Loading