Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/Type/Php/DateIntervalFormatDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,20 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

// The worst case scenario for the non-falsy-string check is that every number is 0.
// `%a` format gives `(unknown)` and removes numeric and uppercase accessory but then
// we'll have to manually check for the non-falsy one.
$dateInterval = new DateInterval('P0D');

$possibleReturnTypes = [];
foreach ($constantStrings as $string) {
$value = $dateInterval->format($string->getValue());
$formatString = $string->getValue();
$value = $dateInterval->format($formatString);

$accessories = [];
if (is_numeric($value)) {
$accessories[] = new AccessoryNumericStringType();
}
if ($value !== '0' && $value !== '') {
if ($value !== '0' && $value !== '' && $formatString !== '%a') {
$accessories[] = new AccessoryNonFalsyStringType();
} elseif ($value !== '') {
$accessories[] = new AccessoryNonEmptyStringType();
Expand Down
6 changes: 4 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-1452.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

$dateInterval = (new \DateTimeImmutable('now -60 minutes'))->diff(new \DateTimeImmutable('now'));

// Could be lowercase-string&non-falsy-string&numeric-string&uppercase-string
assertType('lowercase-string&non-falsy-string', $dateInterval->format('%a'));
assertType(
'lowercase-string&non-empty-string',
$dateInterval->format('%a')
);
Loading