Skip to content

Cannot access offset on mixed when using isset() #8388

@noemi-salaun

Description

@noemi-salaun

Bug report

On PHPStan level 9

When using isset($data['foo']) with $data being mixed, PHPStan complains about "Cannot access offset 'foo' on mixed.".

Also, when checking for value type, like is_string($json['Foo']['Bar']), the value type is not narrowed.

Code snippet that reproduces the problem

        public function sayHello(mixed $json): string
	{
		if (!isset($json['Foo']['Bar']) || !is_string($json['Foo']['Bar'])) {
			throw new \Exception('Bad JSON');
		}

		return $json['Foo']['Bar'];
	}

https://phpstan.org/r/9b18a65f-f28d-4374-8167-1e94703d9707

Expected output

PHPStan should not report an issue when trying to access offset inside isset, and after validating offset with isset, we should be able to assert the type

Did PHPStan help you today? Did it make you happy in any way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions