Skip to content

False type_mismatch_argument after reassigning variable from its array offset #169

@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 keeps an old array|false type after a variable is reassigned from one of its own array offsets.

Expected: after $value = $value[0], later uses of $value should not still be treated as the previous array|false result.

Actual: PHPantom reports type_mismatch_argument when $value is passed to string parameters.

Steps to reproduce

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

function stripFlat(string $value): string
{
    $value = preg_split('/(, flat \d+.*)/u', $value, 0, PREG_SPLIT_DELIM_CAPTURE);
    $value = $value[0];

    $value = preg_split('/(, room \d+.*)/u', $value, 0, PREG_SPLIT_DELIM_CAPTURE);
    $value = $value[0];

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

PHPantom reports:

8   Argument 2 ($subject) expects string, got array|false
11  Argument 1 ($string) expects string, got array|false

Error output or panic trace


.phpantom.toml

Additional context

Using a separate $parts variable avoids this false positive:

$parts = preg_split('/.../', $value, 0, PREG_SPLIT_DELIM_CAPTURE);
$value = $parts[0];

The issue appears when the same variable is reused for both the array result and the extracted string.

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