Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DatePeriod; | ||
use DateTimeInterface; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | ||
use PHPStan\Type\Generic\GenericObjectType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
|
||
class DatePeriodConstructorReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return DatePeriod::class; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === '__construct'; | ||
} | ||
|
||
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type | ||
{ | ||
$thirdArgType = null; | ||
if (isset($methodCall->getArgs()[2])) { | ||
$thirdArgType = $scope->getType($methodCall->getArgs()[2]->value); | ||
} | ||
|
||
if (!$thirdArgType instanceof Type) { | ||
return new GenericObjectType(DatePeriod::class, [ | ||
new NullType(), | ||
new IntegerType(), | ||
]); | ||
} | ||
|
||
if ((new ObjectType(DateTimeInterface::class))->isSuperTypeOf($thirdArgType)->yes()) { | ||
return new GenericObjectType(DatePeriod::class, [ | ||
new ObjectType(DateTimeInterface::class), | ||
new NullType(), | ||
]); | ||
} | ||
|
||
if ((new IntegerType())->isSuperTypeOf($thirdArgType)->yes()) { | ||
return new GenericObjectType(DatePeriod::class, [ | ||
new NullType(), | ||
new IntegerType(), | ||
]); | ||
} | ||
|
||
return new ObjectType(DatePeriod::class); | ||
} | ||
|
||
} |
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
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,35 @@ | ||
<?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'; | ||
|
||
$datePeriodList = []; | ||
|
||
$datePeriod = new DatePeriod($start, $interval, $end); | ||
assertType(\DatePeriod::class . '<DateTimeInterface, null>', $datePeriod); | ||
assertType(\DateTimeInterface::class, $datePeriod->getEndDate()); | ||
assertType('null', $datePeriod->getRecurrences()); | ||
$datePeriodList[] = $datePeriod; | ||
|
||
$datePeriod = new DatePeriod($start, $interval, $recurrences); | ||
assertType(\DatePeriod::class . '<null, int>', $datePeriod); | ||
assertType('null', $datePeriod->getEndDate()); | ||
assertType('int', $datePeriod->getRecurrences()); | ||
$datePeriodList[] = $datePeriod; | ||
|
||
$datePeriod = new DatePeriod($iso); | ||
assertType(\DatePeriod::class . '<null, int>', $datePeriod); | ||
assertType('null', $datePeriod->getEndDate()); | ||
assertType('int', $datePeriod->getRecurrences()); | ||
$datePeriodList[] = $datePeriod; | ||
|
||
/** @var DatePeriod $datePeriod */ | ||
$datePeriod = $datePeriodList[random_int(0, 2)]; | ||
assertType(\DatePeriod::class, $datePeriod); | ||
assertType(\DateTimeInterface::class . '|null', $datePeriod->getEndDate()); | ||
assertType('int|null', $datePeriod->getRecurrences()); |