Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
ondrejmirtes merged 2 commits into phpstan:master from franmomu:response_bag_get_cookies
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions extension.neon
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,8 @@ services:
-
factory: PHPStan\Type\Symfony\CommandGetHelperDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

# ResponseHeaderBag::getCookies() return type
-
factory: PHPStan\Type\Symfony\ResponseHeaderBagDynamicReturnTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
57 changes: 57 additions & 0 deletions src/Type/Symfony/ResponseHeaderBagDynamicReturnTypeExtension.php
View file Open in desktop
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) {
Copy link
Member

@ondrejmirtes ondrejmirtes Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry that asking for runtime value of ResponseHeaderBag::COOKIES_ARRAY might fail here in some projects. Can you please check for ClassConstFetch and check that the user fetches the right constant here? Thanks.

franmomu reacted with thumbs up emoji
Copy link
Contributor Author

@franmomu franmomu Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the guidance, not sure if this is the way you meant, let me know otherwise.

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));
}

}
1 change: 1 addition & 0 deletions tests/Type/Symfony/ExtensionTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/envelope_all.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/header_bag_get.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/response_header_bag_get_cookies.php');

if (class_exists('Symfony\Component\HttpFoundation\InputBag')) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/input_bag.php');
Expand Down
11 changes: 11 additions & 0 deletions tests/Type/Symfony/data/response_header_bag_get_cookies.php
View file Open in desktop
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));
Copy link
Member

@ondrejmirtes ondrejmirtes Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test the FLAT scenario.

franmomu reacted with thumbs up emoji

AltStyle によって変換されたページ (->オリジナル) /