Skip to content

Request > Return Session instead of SessionInterface #233

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/Type/Symfony/RequestGetSessionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

final class RequestGetSessionReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return 'Symfony\Component\HttpFoundation\Request';
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'getSession';
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
return new ObjectType('Symfony\Component\HttpFoundation\Session\Session');
}

}
6 changes: 3 additions & 3 deletions tests/Type/Symfony/data/request_get_session.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php declare(strict_types = 1);

use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use function PHPStan\Testing\assertType;

/** @var \Symfony\Component\HttpFoundation\Request $request */
$request = doRequest();

$session1 = $request->getSession();
assertType(SessionInterface::class, $request->getSession());
assertType(Session::class, $request->getSession());

if ($request->hasSession()) {
assertType(SessionInterface::class, $request->getSession());
assertType(Session::class, $request->getSession());
}
6 changes: 3 additions & 3 deletions tests/Type/Symfony/data/request_get_session_null.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php declare(strict_types = 1);

use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use function PHPStan\Testing\assertType;

/** @var \Symfony\Component\HttpFoundation\Request $request */
$request = doRequest();

$session1 = $request->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());
}