-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathRequestTypeSpecifyingExtension.php
57 lines (45 loc) · 1.81 KB
/
RequestTypeSpecifyingExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php declare(strict_types = 1);
namespace PHPStan\Type\Symfony;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\TypeCombinator;
final class RequestTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
private const REQUEST_CLASS = 'Symfony\Component\HttpFoundation\Request';
private const HAS_METHOD_NAME = 'hasSession';
private const GET_METHOD_NAME = 'getSession';
private TypeSpecifier $typeSpecifier;
public function getClass(): string
{
return self::REQUEST_CLASS;
}
public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool
{
return $methodReflection->getName() === self::HAS_METHOD_NAME && !$context->null();
}
public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$methodVariants = $methodReflection->getDeclaringClass()->getNativeMethod(self::GET_METHOD_NAME)->getVariants();
$returnType = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $methodVariants)->getReturnType();
if (!TypeCombinator::containsNull($returnType)) {
return new SpecifiedTypes();
}
return $this->typeSpecifier->create(
new MethodCall($node->var, self::GET_METHOD_NAME),
TypeCombinator::removeNull($returnType),
$context,
$scope,
);
}
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}
}