New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DateTimeModifyReturnTypeExtension #1267
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the scenario for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's similar too DateTimeConstructorThrowTypeExtension. I would say it's to handle a phpdoc like
|
||
} | ||
|
||
if ($hasFalse && !$hasDateTime) { | ||
return new ConstantBooleanType(false); | ||
} | ||
if ($hasDateTime && !$hasFalse) { | ||
return new StaticType($methodReflection->getDeclaringClass()); | ||
} | ||
|
||
return $defaultReturnType; | ||
} | ||
|
||
} |
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)); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will crash if someone calls
->modify()
without args.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done