Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
91 changes: 91 additions & 0 deletions src/Type/Php/DateIntervalFormatDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use DateInterval;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function is_numeric;
use function strtolower;
use function strtoupper;

#[AutowiredService]
final class DateIntervalFormatDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return DateInterval::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'format';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$arguments = $methodCall->getArgs();

if (!isset($arguments[0])) {
return null;
}

$arg = $scope->getType($arguments[0]->value);

$constantStrings = $arg->getConstantStrings();
if (count($constantStrings) === 0) {
if ($arg->isNonEmptyString()->yes()) {
return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeCombinator::intersect() instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally use directly IntersectionType for string type + accessories because it doesn't require any extra manipulation while intersect does lot of different things useless in this case

}

return null;
}

// The worst case scenario for the non-falsy-string check is that every number is 0.
$dateInterval = new DateInterval('P0D');

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

$accessories = [];
if (is_numeric($value)) {
$accessories[] = new AccessoryNumericStringType();
}
if ($value !== '0' && $value !== '') {
$accessories[] = new AccessoryNonFalsyStringType();
} elseif ($value !== '') {
$accessories[] = new AccessoryNonEmptyStringType();
}
if (strtolower($value) === $value) {
$accessories[] = new AccessoryLowercaseStringType();
}
if (strtoupper($value) === $value) {
$accessories[] = new AccessoryUppercaseStringType();
}

if (count($accessories) === 0) {
return null;
}

$possibleReturnTypes[] = new IntersectionType([new StringType(), ...$accessories]);
}

return TypeCombinator::union(...$possibleReturnTypes);
}

}
40 changes: 40 additions & 0 deletions tests/PHPStan/Analyser/nsrt/date-interval-format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace DateIntervalFormat;

use DateInterval;

use function PHPStan\Testing\assertType;

class Foo
{
/**
* @param string $string
* @param non-empty-string $nonEmptyString
* @param '%Y'|'%D' $unionString1
* @param '%Y'|'%y' $unionString2
*
* @return void
*/
public function test(
DateInterval $dateInterval,
string $string,
string $nonEmptyString,
string $unionString1,
string $unionString2,
): void {
assertType('string', $dateInterval->format($string));
assertType('non-empty-string', $dateInterval->format($nonEmptyString));

assertType('lowercase-string&non-falsy-string&numeric-string&uppercase-string', $dateInterval->format('%Y')); // '00'
assertType('lowercase-string&non-empty-string&numeric-string&uppercase-string', $dateInterval->format('%y')); // '0'
assertType('lowercase-string&non-falsy-string&numeric-string&uppercase-string', $dateInterval->format($unionString1));
assertType('lowercase-string&non-empty-string&numeric-string&uppercase-string', $dateInterval->format($unionString2));

assertType('non-falsy-string&uppercase-string', $dateInterval->format('%Y DAYS'));
assertType('non-falsy-string&uppercase-string', $dateInterval->format($unionString1. ' DAYS'));

assertType('lowercase-string&non-falsy-string', $dateInterval->format('%Y days'));
assertType('lowercase-string&non-falsy-string', $dateInterval->format($unionString1. ' days'));
}
}
Loading