Skip to content

False positive: first class callable protected by method_exists still reports callable.nonNativeMethod error. #13596

@patrickcarlohickman

Description

@patrickcarlohickman

Bug report

When creating a first class callable, I'm getting the error "Creating callable from a non-native method" (callable.nonNativeMethod) even when the code is protected by a call to method_exists() or is_callable().

The idea is that a base class that is meant to be extended has a method that creates a callable out of a target method that an extending class may or may not define. The base class uses a method_exists() (or is_callable()) guard to ensure that the callable is only generated when the extending class actually defines the target method. However, PHPStan still reports the error.

https://3v4l.org/imBZu

class BaseClass
{
    public function getCallable() : ?Closure {
        // This works fine, but PHPStan reports callable.nonNativeMethod error.
        return method_exists($this, 'myCallable') ? $this->myCallable(...) : null;
    }
}

class ChildOne extends BaseClass
{
    //
}

class ChildTwo extends BaseClass
{
    public function myCallable() : string {
        return 'I exist on child two.';
    }
}

Code snippet that reproduces the problem

https://phpstan.org/r/7ddd69de-cc28-4038-963c-0f04bda2b00e

Expected output

Expect no errors when callable generation is guarded.

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

I love the extra peace of mind PHPStan brings. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions