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
afe33db
commit d22c593
Showing
6 changed files
with
132 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
15 changes: 15 additions & 0 deletions
15
src/Type/Php/DateTimeImmutableModifyReturnTypeExtension.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,15 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateTimeImmutable; | ||
|
||
class DateTimeImmutableModifyReturnTypeExtension extends DateTimeModifyReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return DateTimeImmutable::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateTime; | ||
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; | ||
|
||
class DateTimeModifyReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return DateTime::class; | ||
} | ||
|
||
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(); | ||
|
||
$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)); | ||
} | ||
|
||
} |