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