Skip to content

@template Tvalue in @param array<non-empty-string, Tvalue|string|null> is incorrectly interpreted #435

Description

@landure

Verified on latest main

  • I have confirmed this bug still occurs when built from the latest main branch.

PHPantom version / commit

phpantom_lsp 33718a6

Installation method

Built from source

Operating system

Linux x86_64

Editor

Zed

Bug description

@param array<non-empty-string, Tvalue|string|null> $data seems to be interpreted as @param Tvalue $data,
resulting in invalid detected return type: array<non-empty-string, array{bar: true, foo: ?string}|non-empty-string> instead of array<non-empty-string, bool|non-empty-string>.

Steps to reproduce

class Example  {

    /**
     * @param array<non-empty-string,string|float|bool|null>
     * @return array<non-empty-string,non-empty-string|float|bool>
     */
    public function filter(array $value): array
    {
        return $this->filterOutNullAndEmptyStrings($value);
    }

    /**
     * @template Tvalue of object|scalar
     *
     * @param array<non-empty-string, Tvalue|string|null> $data
     *
     * @return array<non-empty-string, Tvalue|non-empty-string>
     */
    private function filterOutNullAndEmptyStrings(array $data): array
    {
        return array_filter($data, static fn (mixed $value): bool => null !== $value && '' !== $value);
    }
}

Error output or panic trace

Return type array<non-empty-string, array{bar: true, foo: ?string}|non-empty-string> is incompatible with declared return type array<non-empty-string, non-empty-string|float|bool>
         🪪  type_mismatch_return

.phpantom.toml

[diagnostics]
unresolved-member-access = true
extra-arguments = true
workspace = true

[indexing]
strategy = "full"

[phpstan]
command="tools/phpstan/vendor/bin/phpstan"

[phpcs]
command="tools/phpcs/vendor/bin/phpcs"

[formatting]
php-cs-fixer="tools/php-cs-fixer/vendor/bin/php-cs-fixer"
phpcbf="tools/phpcs/vendor/bin/phpcbf"

Additional context

This is a complex use case, but psalm, mago, and phpstan accept it without raising issues.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions