|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Symfony; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PhpParser\Node\Identifier; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | +use PHPStan\Type\ArrayType; |
| 12 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 13 | +use PHPStan\Type\IntegerType; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\StringType; |
| 16 | +use PHPStan\Type\Type; |
| 17 | +use Symfony\Component\HttpFoundation\Cookie; |
| 18 | +use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
| 19 | + |
| 20 | +final class ResponseHeaderBagDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 21 | +{ |
| 22 | + |
| 23 | + public function getClass(): string |
| 24 | + { |
| 25 | + return ResponseHeaderBag::class; |
| 26 | + } |
| 27 | + |
| 28 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 29 | + { |
| 30 | + return $methodReflection->getName() === 'getCookies'; |
| 31 | + } |
| 32 | + |
| 33 | + public function getTypeFromMethodCall( |
| 34 | + MethodReflection $methodReflection, |
| 35 | + MethodCall $methodCall, |
| 36 | + Scope $scope |
| 37 | + ): Type |
| 38 | + { |
| 39 | + if (isset($methodCall->getArgs()[0])) { |
| 40 | + $node = $methodCall->getArgs()[0]->value; |
| 41 | + |
| 42 | + if ( |
| 43 | + $node instanceof ClassConstFetch && |
| 44 | + $node->class instanceof Name && |
| 45 | + $node->name instanceof Identifier && |
| 46 | + $node->class->toString() === ResponseHeaderBag::class && |
| 47 | + $node->name->name === 'COOKIES_ARRAY' |
| 48 | + ) { |
| 49 | + return new ArrayType( |
| 50 | + new StringType(), |
| 51 | + new ArrayType( |
| 52 | + new StringType(), |
| 53 | + new ArrayType( |
| 54 | + new StringType(), |
| 55 | + new ObjectType(Cookie::class) |
| 56 | + ) |
| 57 | + ) |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return new ArrayType(new IntegerType(), new ObjectType(Cookie::class)); |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments