Skip to content

Return type for json_decode based on second parameter #1874

@luxemate

Description

@luxemate

Summary of a problem or a feature request

json_decode has second parameter to determine if result is an array or \stdClass. It would be nice to catch cases when there's no assoc: true, and code is trying to access the result of json_decode as an array and vice versa.

Code snippet that reproduces the problem

No error for:

<?php declare(strict_types = 1);

$data = json_decode('{"foo": "bar"}');

$foo = $data['foo'];

https://phpstan.org/r/936624ae-ac3f-4f70-afad-e6480a19a6e6

Expected output

Cannot use object of type stdClass as array

https://3v4l.org/KCXIa

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