Skip to content

False unknown_member in elseif after instanceof branch #168

@alexchexes

Description

@alexchexes

PHPantom version

phpantom_lsp 0.8.0-dirty

Installation method

Pre-built binary from GitHub Releases

Operating system

Windows x86_64

Editor

VS Code

Bug description

PHPantom reports unknown_member inside an elseif branch guarded by is_callable([$value, 'getTime']), using the class type from the previous instanceof branch.

Expected: no diagnostic. The elseif branch only runs when $value is not the class checked in the previous if, and the method call is guarded by is_callable().

Actual: PHPantom reports Method 'getTime' not found on class 'Repro\KnownDateLike'.

Steps to reproduce

  1. Use any supported PHP version.
  2. Create a PHP file with:
<?php

namespace Repro;

final class KnownDateLike
{
    public function format(): string
    {
        return 'formatted';
    }
}

function formatDateLike(int|string|KnownDateLike|object|null $value): string
{
    if ($value === null) {
        return '';
    }

    if (is_object($value)) {
        if ($value instanceof KnownDateLike) {
            $value = $value->format();
        } elseif (is_callable([$value, 'getTime'])) {
            $value = (string) $value->getTime();
        }
    }

    return (string) $value;
}
  1. Run diagnostics.

PHPantom reports:

31  Method 'getTime' not found on class 'Repro\KnownDateLike'
    unknown_member

Error output or panic trace


.phpantom.toml

Additional context

Binary source

Originally observed in the VS Code extension v0.5.0. Reproduced with the extension-cached GitHub Releases binary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions