Skip to content

undefined property access not detected when class instantiated by static factory method #8845

@sneakyimp

Description

@sneakyimp

Bug report

I was hoping to use PHPStan to identify all dynamic property assignments, which have been deprecated in PHP 8.2. Sadly, it doesn't seem to catch dynamic property assignments when the class object is instantiated via a factory method.

Code snippet that reproduces the problem

If you define a class with a factory method in class.php:

<?php
class Foo {
    public static function factory(){
        return new self();
    }
}

And then you utilize that class in test.php

<?php
require_once 'class.php';
$foo = Foo::factory();
$foo->some_new_property = 42;
var_dump($foo);

I have tried this with the try tool here.

Expected output

PHPStan should detect that you are accessing an undefined property of the Foo class in test.php.

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

It looks promising so far. I've only just started tinkering with it today.

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