-
Notifications
You must be signed in to change notification settings - Fork 460
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
1 parent
9c5ddc5
commit 84ea0e1
Showing
4 changed files
with
85 additions
and
0 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
44 changes: 44 additions & 0 deletions
44
src/Type/Php/DateIntervalConstructorThrowTypeExtension.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,44 @@ | ||
<?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\Type\DynamicStaticMethodThrowTypeExtension; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeUtils; | ||
|
||
class DateIntervalConstructorThrowTypeExtension implements DynamicStaticMethodThrowTypeExtension | ||
{ | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === '__construct' && $methodReflection->getDeclaringClass()->getName() === DateInterval::class; | ||
} | ||
|
||
public function getThrowTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type | ||
{ | ||
if (count($methodCall->args) === 0) { | ||
return $methodReflection->getThrowType(); | ||
} | ||
|
||
$arg = $methodCall->args[0]->value; | ||
$constantStrings = TypeUtils::getConstantStrings($scope->getType($arg)); | ||
if (count($constantStrings) === 0) { | ||
return $methodReflection->getThrowType(); | ||
} | ||
|
||
foreach ($constantStrings as $constantString) { | ||
try { | ||
new \DateInterval($constantString->getValue()); | ||
} catch (\Exception $e) { // phpcs:ignore | ||
return $methodReflection->getThrowType(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
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