Skip to content

Dynamic calls to new are not properly checked #12273

@danrot

Description

@danrot

Bug report

I have code looking something like that:

<?php declare(strict_types = 1);

$map = [
	'datetime' => \DateTime::class,
	'stdclass' => \stdClass::class,
];

$settings = json_decode('{"class": "datetim"}');

new ($map[$settings->class])();

What is weird in this example, is that PHPStan does not show any error. I am not saying it should automatically recognize the type of the json_decode call, but $settings->class is of type mixed, and it is not said that $map[$settings->class] returns a proper class string.

Code snippet that reproduces the problem

https://phpstan.org/r/dd0caadd-0d1a-43c7-b6bc-480923a25b9f

Expected output

I would have expected that PHPStan warns me in line 13 of the example, since it is not guaranteed that this will be working.

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

All the time :-)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions