Skip to content

False positives when using "method_exists" condition on interfaces #323

@Wirone

Description

@Wirone

When variable is typed as interface and we use concrete implementation in code, sometimes there are valid calls to methods that doesn't exist in interface. For example when using Symfony\Component\HttpFoundation\Session\SessionInterface and want to use getFlashBag method, this will trigger error in PHPStan (level 2):

/** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
$session = $request->getSession();
if ($session && method_exists($session, 'getFlashBag')) {
    $session->getFlashBag()->add('success', 'False positive detected!');
}

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