-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DatePeriod::getEndDate dynamic return type
- Loading branch information
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/Type/Php/DatePeriodGetEndDateMethodReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class DatePeriodGetEndDateMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return \DatePeriod::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'getEndDate'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
$argument = $methodCall->var->args[2] ?? null; | ||
|
||
// initialize with iso parameter | ||
if ($argument === null) { | ||
return new NullType(); | ||
} | ||
|
||
$dateTimeType = new ObjectType(\DateTimeInterface::class); | ||
$argumentType = $scope->getType($argument->value); | ||
|
||
// initialize with end parameter | ||
if ($dateTimeType->isSuperTypeOf($argumentType)->yes()) { | ||
return $dateTimeType; | ||
} | ||
|
||
// initialize with recurrences parameter | ||
if ((new IntegerType())->isSuperTypeOf($argumentType)->yes()) { | ||
return new NullType(); | ||
} | ||
|
||
return TypeCombinator::addNull($dateTimeType); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/PHPStan/Analyser/data/DatePeriodGetEndDateDynamicReturnType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
$start = new DateTime('2012-07-01'); | ||
$interval = new DateInterval('P7D'); | ||
$end = new DateTime('2012-07-31'); | ||
$recurrences = 4; | ||
$iso = 'R4/2012-07-01T00:00:00Z/P7D'; | ||
|
||
assertType('null', (new DatePeriod($start, $interval, $recurrences))->getEndDate()); | ||
assertType(\DateTimeInterface::class, (new DatePeriod($start, $interval, $end))->getEndDate()); | ||
assertType('null', (new DatePeriod($iso))->getEndDate()); |