-
Notifications
You must be signed in to change notification settings - Fork 95
Add dynamic return type to ResponseHeaderBag::getCookies #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Symfony; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeUtils; | ||
use Symfony\Component\HttpFoundation\Cookie; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
|
||
final class ResponseHeaderBagDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return ResponseHeaderBag::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'getCookies'; | ||
} | ||
|
||
public function getTypeFromMethodCall( | ||
MethodReflection $methodReflection, | ||
MethodCall $methodCall, | ||
Scope $scope | ||
): Type | ||
{ | ||
if (isset($methodCall->getArgs()[0])) { | ||
$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value)); | ||
|
||
if (count($argStrings) === 1 && $argStrings[0]->getValue() === ResponseHeaderBag::COOKIES_ARRAY) { | ||
|
||
return new ArrayType( | ||
new StringType(), | ||
new ArrayType( | ||
new StringType(), | ||
new ArrayType( | ||
new StringType(), | ||
new ObjectType(Cookie::class) | ||
) | ||
) | ||
); | ||
} | ||
} | ||
|
||
return new ArrayType(new IntegerType(), new ObjectType(Cookie::class)); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
use function PHPStan\Testing\assertType; | ||
|
||
$headerBag = new ResponseHeaderBag(); | ||
$headerBag->setCookie(Cookie::create('cookie_name')); | ||
|
||
assertType('array<int, Symfony\Component\HttpFoundation\Cookie>', $headerBag->getCookies()); | ||
assertType('array<string, array<string, array<string, Symfony\Component\HttpFoundation\Cookie>>>', $headerBag->getCookies(ResponseHeaderBag::COOKIES_ARRAY)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also test the FLAT scenario. |