From 479dd044daf88c43d7e7df76a9bf6281376e227c Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Tue, 11 Jan 2022 11:30:00 +0100 Subject: [PATCH] Request > Return `Session` instead of `SessionInterface` Symfony 6 removed the `Session` and `FlashBagInterface` services. That means that the only way to get the FlashBag (to add flashes) it to use `$request->getSession()->getFlashBag()`. But the problem is, `getFlashBag` is not defined on the `SessionInterface`: ``` Call to an undefined method Symfony\Component\HttpFoundation\Session\SessionInterface::getFlashBag(). ``` To make everyone's life easier, let's change the return type to `Sesssion` instead. This is what's returned in a default Symfony application. --- extension.neon | 5 +++ .../RequestGetSessionReturnTypeExtension.php | 34 +++++++++++++++++++ .../Type/Symfony/data/request_get_session.php | 6 ++-- .../Symfony/data/request_get_session_null.php | 6 ++-- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/Type/Symfony/RequestGetSessionReturnTypeExtension.php diff --git a/extension.neon b/extension.neon index 23008e5d..2ae543e5 100644 --- a/extension.neon +++ b/extension.neon @@ -118,6 +118,11 @@ services: factory: PHPStan\Type\Symfony\RequestDynamicReturnTypeExtension tags: [phpstan.broker.dynamicMethodReturnTypeExtension] + # Request::getSession() return type + - + factory: PHPStan\Type\Symfony\RequestGetSessionReturnTypeExtension + tags: [phpstan.broker.dynamicMethodReturnTypeExtension] + # Request::getSession() type specification - factory: PHPStan\Type\Symfony\RequestTypeSpecifyingExtension diff --git a/src/Type/Symfony/RequestGetSessionReturnTypeExtension.php b/src/Type/Symfony/RequestGetSessionReturnTypeExtension.php new file mode 100644 index 00000000..9b257aa6 --- /dev/null +++ b/src/Type/Symfony/RequestGetSessionReturnTypeExtension.php @@ -0,0 +1,34 @@ +getName() === 'getSession'; + } + + public function getTypeFromMethodCall( + MethodReflection $methodReflection, + MethodCall $methodCall, + Scope $scope + ): Type + { + return new ObjectType('Symfony\Component\HttpFoundation\Session\Session'); + } + +} diff --git a/tests/Type/Symfony/data/request_get_session.php b/tests/Type/Symfony/data/request_get_session.php index 9a0335e5..17ef88a6 100644 --- a/tests/Type/Symfony/data/request_get_session.php +++ b/tests/Type/Symfony/data/request_get_session.php @@ -1,14 +1,14 @@ getSession(); -assertType(SessionInterface::class, $request->getSession()); +assertType(Session::class, $request->getSession()); if ($request->hasSession()) { - assertType(SessionInterface::class, $request->getSession()); + assertType(Session::class, $request->getSession()); } diff --git a/tests/Type/Symfony/data/request_get_session_null.php b/tests/Type/Symfony/data/request_get_session_null.php index 9c37979d..be0fd0d1 100644 --- a/tests/Type/Symfony/data/request_get_session_null.php +++ b/tests/Type/Symfony/data/request_get_session_null.php @@ -1,14 +1,14 @@ getSession(); -assertType(SessionInterface::class . '|null', $request->getSession()); +assertType(Session::class . '|null', $request->getSession()); if ($request->hasSession()) { - assertType(SessionInterface::class, $request->getSession()); + assertType(Session::class, $request->getSession()); }