Skip to content

Include previous check on null safe operator property on object #6991

@alexander-schranz

Description

@alexander-schranz

Feature request

Currently phpstan would throw an error when trying access a property of an object, which was previously checked via a null safe operator on another property of it.

I'm using something like this code from Symfony Request:

$currentRequest = $this->requestStack->getCurrentRequest();
/** @var string|null $currentRouteName */
$currentRouteName = $currentRequest?->attributes->get('_route');
if (!$currentRouteName) {
    return [];
}

// later ...

// in the next line $currentRequest can not be null because of the `if (!$currentRouteName) {` check
$routeAttributes = $currentRequest->attributes->get('_route_params', []); 

A simpler example:
https://phpstan.org/r/af51a08d-960d-4001-a1a7-21f991ea83b2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions