Skip to content

Internal error "Invalid argument provided to method hasMany" when using a dynamic value #17

@chriscpty

Description

@chriscpty

In our project, we have an abstract model class where each implementation's relation to another abstract model class is different:

class AbstractClassA extends ActiveRecord {
    public const CLASS_B_CLASS = AbstractClassB::class;

    public function getClassB() {
        return $this->hasMany(static::CLASS_B_CLASS, /* condition */);
    }
}

class ClassA1 extends AbstractClassA {
    public const CLASS_B_CLASS = ClassB1::class;
}
class ClassA2 extends AbstractClassA {
    public const CLASS_B_CLASS = ClassB2::class;
}

class AbstractClassB extends ActiveRecord {
    public const CLASS_A_CLASS = AbstractClassA::class;

    public function getClassA() {
        return $this->hasOne(static::CLASS_A_CLASS, /* condition */);
    }
}
class ClassB1 extends AbstractClassB {
    public const CLASS_A_CLASS = ClassA1::class;
}
class ClassB2 extends AbstractClassB {
    public const CLASS_A_CLASS = ClassA2::class;
}

This throws a PHPStan error with the message

 Internal error: Invalid argument provided to method hasOne                                                                                    
 Hint: You should use ::class instead of ::className() while analysing file 

preventing any other PHPStan inspections from being applied.

Maybe you could detect if a constant passed into hasMany or hasOne is (always) a class string, and then use that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions