Skip to content

False type_mismatch_argument when @phpstan-type aliases are passed to compatible parameters #166

@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 type mismatches when values typed with @phpstan-type aliases are passed to compatible native parameter types.

Expected: no diagnostic. The aliases should be expanded before argument compatibility is checked.

Actual: PHPantom keeps the alias name as the argument type and reports type_mismatch_argument.

Steps to reproduce

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

/**
 * @phpstan-type Payload = array{
 *     name: string,
 *     phone: string,
 * }
 */
final class Api
{
    /** @var Payload */
    private array $payload;

    public function __construct()
    {
        $this->payload = [
            'name' => 'Alex',
            'phone' => '123',
        ];
    }

    public function submit(): array
    {
        return Sender::post('/lead', $this->payload);
    }
}

final class Sender
{
    public static function post(string $url, ?array $postData = null): array
    {
        return [];
    }
}

/**
 * @phpstan-type Field = 'id'|'name'
 */
final class Filter
{
    /**
     * @param Field $field
     */
    public function isDefined($field): bool
    {
        return property_exists($this, $field);
    }
}

/**
 * @phpstan-type ValueType = 'string'|'int'|'boolean'
 */
final class Query
{
    public function setValue(mixed $value, ?string $type = null): void
    {
    }
}

/**
 * @phpstan-import-type ValueType from Query
 */
final class Model
{
    /**
     * @param list<array{0:mixed,1:ValueType}> $values
     */
    public function save(array $values): void
    {
        $query = new Query();

        foreach ($values as [$value, $type]) {
            $query->setValue($value, $type);
        }
    }
}
  1. Run diagnostics.

PHPantom reports:

24  Argument 2 ($postData) expects ?array, got Payload
46  Argument 2 ($property) expects string, got Field
73  Argument 2 ($type) expects ?string, got ValueType
    type_mismatch_argument

Error output or panic trace


.phpantom.toml

Additional context

This looks like a type-alias expansion gap in argument type checking. PHPantom keeps the alias names (Payload, Field, ValueType) instead of expanding them before compatibility checks.

Examples:

  • Payload expands to array{name: string, phone: string}, compatible with ?array.
  • Field expands to 'id'|'name', compatible with string.
  • ValueType expands to 'string'|'int'|'boolean', compatible with ?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