Thanks to visit codestin.com
Credit goes to github.com

Skip to content

should return X but returns Y false positive ? #11894

@momala454

Description

@momala454

Bug report

I want to have a method that trim an argument if it's a string, or return it as is if not.
An error Function hello() should return T but returns string. is reported.
If (on hello2), I tweak the return to tell it return string if the input is string, the error is not reported, but another one is reported if I call it
Return type of call to function hello2 contains unresolvable type.

<?php declare(strict_types = 1);

/** @template T of mixed
 * @param T $a
 * @return T
 */
function hello(mixed $a): mixed
{
    if (!is_string($a)) {
        return $a;
    }

	hello2($a);
	hello($a);

    return trim($a);
}

/** @template T of mixed
 * @param T $a
 * @return (T is string ? string : T)
 */
function hello2(mixed $a): mixed
{
    if (!is_string($a)) {
        return $a;
    }

    return trim($a);
}

Code snippet that reproduces the problem

https://phpstan.org/r/efe015eb-2eb6-44fe-a699-4034c576705c

Expected output

I'm not sure if I must do something different or if it's an error in phpstan

Did PHPStan help you today? Did it make you happy in any way?

yes thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions