Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateTimeModifyReturnTypeExtension
- Loading branch information
1 parent
e40474b
commit a8a37ed
Showing
5 changed files
with
135 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
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,79 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateTime; | ||
use DateTimeInterface; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\NeverType; | ||
use PHPStan\Type\StaticType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\TypeUtils; | ||
use function count; | ||
|
||
class DateTimeModifyReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
/** @var class-string<DateTimeInterface> */ | ||
private $dateTimeClass; | ||
|
||
/** @param class-string<DateTimeInterface> $dateTimeClass */ | ||
public function __construct(string $dateTimeClass = DateTime::class) | ||
{ | ||
$this->dateTimeClass = $dateTimeClass; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return $this->dateTimeClass; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'modify'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
$defaultReturnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); | ||
if (count($methodCall->getArgs()) < 1) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$valueType = $scope->getType($methodCall->getArgs()[0]->value); | ||
$constantStrings = TypeUtils::getConstantStrings($valueType); | ||
|
||
$hasFalse = false; | ||
$hasDateTime = false; | ||
|
||
foreach ($constantStrings as $constantString) { | ||
if (@(new DateTime())->modify($constantString->getValue()) === false) { | ||
$hasFalse = true; | ||
} else { | ||
$hasDateTime = true; | ||
} | ||
|
||
$valueType = TypeCombinator::remove($valueType, $constantString); | ||
} | ||
|
||
if (!$valueType instanceof NeverType) { | ||
return $defaultReturnType; | ||
} | ||
|
||
if ($hasFalse && !$hasDateTime) { | ||
return new ConstantBooleanType(false); | ||
} | ||
if ($hasDateTime && !$hasFalse) { | ||
return new StaticType($methodReflection->getDeclaringClass()); | ||
} | ||
|
||
return $defaultReturnType; | ||
} | ||
|
||
} |
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,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace DateTimeModifyReturnTypes; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
|
||
class Foo | ||
{ | ||
public function modify(DateTime $datetime, DateTimeImmutable $dateTimeImmutable, string $modify): void { | ||
assertType('DateTime|false', $datetime->modify($modify)); | ||
assertType('DateTimeImmutable|false', $dateTimeImmutable->modify($modify)); | ||
} | ||
|
||
/** | ||
* @param '+1 day'|'+2 day' $modify | ||
*/ | ||
public function modifyWithValidConstant(DateTime $datetime, DateTimeImmutable $dateTimeImmutable, string $modify): void { | ||
assertType('DateTime', $datetime->modify($modify)); | ||
assertType('DateTimeImmutable', $dateTimeImmutable->modify($modify)); | ||
} | ||
|
||
/** | ||
* @param 'kewk'|'koko' $modify | ||
*/ | ||
public function modifyWithInvalidConstant(DateTime $datetime, DateTimeImmutable $dateTimeImmutable, string $modify): void { | ||
assertType('false', $datetime->modify($modify)); | ||
assertType('false', $dateTimeImmutable->modify($modify)); | ||
} | ||
|
||
/** | ||
* @param '+1 day'|'koko' $modify | ||
*/ | ||
public function modifyWithBothConstant(DateTime $datetime, DateTimeImmutable $dateTimeImmutable, string $modify): void { | ||
assertType('DateTime|false', $datetime->modify($modify)); | ||
assertType('DateTimeImmutable|false', $dateTimeImmutable->modify($modify)); | ||
} | ||
|
||
} |