Skip to content

Request::getSession return type should not be null #26539

@soullivaneuh

Description

@soullivaneuh
Q A
Bug report? no
Feature request? no
BC Break report? no
RFC? yes
Symfony version 4.0

As we have a hasSession method:

* @return bool true when the Request contains a Session object, false otherwise
*/
public function hasSession()
{
return null !== $this->session;
}

IMHO, getSession should not be nullable for the return type:

* @return SessionInterface|null The session
*/
public function getSession()
{
$session = $this->session;
if (!$session instanceof SessionInterface && null !== $session) {
$this->setSession($session = $session());
}
return $session;
}

It should throw an exception instead. Plus, thanks to PHP 7 type-hinting, throwing an exception will not be necessary in the future.

This will reduce some tools alerts like this:

Calling method set() on possibly null value of type Symfony\Component\HttpFoundation\Session\SessionInterface|null.

WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    HttpFoundationRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions