Skip to content

Method_exists calls in traits should be ignored #13252

@czirkoszoltan

Description

@czirkoszoltan

Bug report

A trait may use method_exists($this, 'methodname'). Although the value of this call can be determined statically, it shouldn't be reported as an error for the class using the trait. Another class using the same trait may not have the same method.

trait T {
    public function func(): void {
        var_export(method_exists($this, 'method'));
    }
}

class A {
    use T;
    
    public function method(): void {}
}

class B {
    use T;
}

$a = new A();
$a->func();     // prints true

$b = new B();
$b->func();     // prints false
 ------ ------------------------------------------------------------------------------------------- 
  Line   test.php (in context of class A)                                                           
 ------ ------------------------------------------------------------------------------------------- 
  5      Call to function method_exists() with $this(A) and 'method' will always evaluate to true.  
         🪪 function.alreadyNarrowedType                                                            
 ------ ------------------------------------------------------------------------------------------- 

Code snippet that reproduces the problem

https://phpstan.org/r/78442e02-df6e-4a93-bfe3-9ec1fb692765

Expected output

No error reported.

Related

#7599 may be related.

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