Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DateTimeZoneConstructorThrowTypeExtension
- Loading branch information
1 parent
75cef53
commit 0710109
Showing
4 changed files
with
69 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
50 changes: 50 additions & 0 deletions
50
src/Type/Php/DateTimeZoneConstructorThrowTypeExtension.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,50 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use DateTimeZone; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\DynamicStaticMethodThrowTypeExtension; | ||
use PHPStan\Type\NeverType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\TypeUtils; | ||
use function count; | ||
|
||
class DateTimeZoneConstructorThrowTypeExtension implements DynamicStaticMethodThrowTypeExtension | ||
{ | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === '__construct' && $methodReflection->getDeclaringClass()->getName() === DateTimeZone::class; | ||
} | ||
|
||
public function getThrowTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type | ||
{ | ||
if (count($methodCall->getArgs()) === 0) { | ||
return null; | ||
} | ||
|
||
$valueType = $scope->getType($methodCall->getArgs()[0]->value); | ||
$constantStrings = TypeUtils::getConstantStrings($valueType); | ||
|
||
foreach ($constantStrings as $constantString) { | ||
try { | ||
new DateTimeZone($constantString->getValue()); | ||
} catch (\Exception $e) { // phpcs:ignore | ||
return $methodReflection->getThrowType(); | ||
} | ||
|
||
$valueType = TypeCombinator::remove($valueType, $constantString); | ||
} | ||
|
||
if (!$valueType instanceof NeverType) { | ||
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