Skip to content

Conditional return type in @method is not resolved #12205

Closed

Description

@alies-dev

A conditional return type on a class-level @method tag is not resolved at the call site when the condition refers to a parameter (conditionals on an already-bound class template do resolve). The template Psalm generates for the parameter stays in the inferred type:

<?php
/**
 * @method ($asText is true ? string : list<string>) words(int $nb = 3, bool $asText = false)
 */
final class FakeGenerator
{
    public function __call(string $name, array $arguments): mixed
    {
        return null;
    }
}

function test(FakeGenerator $g): void
{
    $a = $g->words(3, false);
    /** @psalm-trace $a */
    $b = $g->words(3, true);
    /** @psalm-trace $b */
}
Trace: $a: (TGeneratedFromParam1 is true ? string : list<string>)
Trace: $b: (TGeneratedFromParam1 is true ? string : list<string>)

Expected list<string> and string. The same conditional in @return on a real method resolves to exactly that, and PHPStan (2.2.x-dev) resolves this @method form too.

Reproduced on 6.x @ b2e7f20 and master @ fe996e2, with and without native parameter types in the tag.

Real-world case: FakerPHP/Faker#1075 proposes this annotation for words() on Faker\Generator.

Activity

  1. psalm-github-bot commented on Oct 7, 2026

    @psalm-github-bot

    Hey @alies-dev, can you reproduce the issue on https://psalm.dev? These will be used as phpunit tests when implementing the feature or fixing this bug.

  2. orklah commented on Nov 14, 2026

    @orklah
    Collaborator

    Fixed by #12206, now on master too: $g->words(3, false) infers list<string> and $g->words(3, true) infers string. Closing.

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

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