Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement DateIntervalDynamicReturnTypeExtension
- Loading branch information
1 parent
94ca7b2
commit 7b4b767
Showing
3 changed files
with
51 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
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,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateInterval; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
|
||
class DateIntervalDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension | ||
{ | ||
public function getClass(): string | ||
{ | ||
return DateInterval::class; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'createFromDateString'; | ||
} | ||
|
||
public function getTypeFromStaticMethodCall( MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope ): ?\PHPStan\Type\Type | ||
{ | ||
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs( | ||
$scope, | ||
$methodCall->getArgs(), | ||
$methodReflection->getVariants(), | ||
)->getReturnType(); | ||
|
||
$dateTimeString = $scope->getType($methodCall->getArgs()[0]->value); | ||
|
||
if (!($dateTimeString instanceof ConstantStringType)) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$isValid = DateInterval::createFromDateString($dateTimeString->getValue()) !== false; | ||
|
||
return $isValid ? new ObjectType(DateInterval::class) : new ConstantBooleanType(false); | ||
} | ||
} |