← All results

Conditional return type driven by a parameter type check

Group: Conditional types · Category: ecosystem · File: conditionals_param_return.php

When analyzers model parameter-conditional returns, a string argument yields string and an int argument yields int. References: - PHPStan phpdoc-types: Conditional return types // E<noverify>: NoVerify mis-parses $value in the conditional return and attributes the diagnostic here - Psalm type_syntax/conditional_types.md - Intelephense Type-System.md: Conditional Return Type

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.7Phan 6.0.7 php-ast version 1.1.3 PHP version used to run Phan: 8.5.9 Pass
conditionals_param_return.php:21: Saw unextractable annotation for comment '* @return ($value is string ? string : int) // E<phan>: Phan cannot extract conditional return annotations' [PhanUnextractableAnnotation]
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass reported Lv.5+
conditionals_param_return.php:36: Parameter #1 $value of function Conformance\Tests\ConditionalsParamReturn\takesString expects string, int given. [identifier=argument.type] [reported-from-level=5]
With strict-rules
conditionals_param_return.php:36: Parameter #1 $value of function Conformance\Tests\ConditionalsParamReturn\takesString expects string, int given. [identifier=argument.type]
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Pass
conditionals_param_return.php:36: Argument 1 of Conformance\Tests\ConditionalsParamReturn\takesString expects string, but int provided [InvalidScalarArgument]
mago 1.46.0mago 1.46.0 Pass
conditionals_param_return.php:36: Invalid argument type for argument #1 of `Conformance\Tests\ConditionalsParamReturn\takesString`: expected `string`, but found `int`. [invalid-argument]
mir 0.70.1mir 0.70.1 Pass
conditionals_param_return.php:36: InvalidArgument: Argument $value of takesString() expects 'string', got 'int' [MIR0201]
phpantom 0.9.0phpantom_lsp 0.9.0 Pass
conditionals_param_return.php:36: Argument 1 ($value) expects string, got int [type_mismatch_argument]
intelephense 1.18.5intelephense 1.18.5 Pass
conditionals_param_return.php:36: Expected type 'string'. Found 'int'. [P1006]
phpy 0.2.0phpy 0.2.0 Pass

No diagnostics reported.

qodana 262.8665.325Qodana 262.8665.325 Pass

No diagnostics reported.

noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Pass
conditionals_param_return.php:14: Class or interface named \Conformance\Tests\ConditionalsParamReturn\$value does not exist [undefinedClass]
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Pass
conditionals_param_return.php:36: argument 1 (returned from identify()) to takesString() cannot become string $value — proven TypeError (strict mode) [type.argument-mismatch]
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass
conditionals_param_return.php:36: Parameter #1 $value of function Conformance\Tests\ConditionalsParamReturn\takesString expects string, int given. [identifier=argument.type]
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Pass
conditionals_param_return.php:36: Argument 1 of Conformance\Tests\ConditionalsParamReturn\takesString expects string, but int provided [InvalidScalarArgument]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\ConditionalsParamReturn;

/**
 * Conditional return type driven by a parameter type check.
 *
 * When analyzers model parameter-conditional returns, a string argument
 * yields string and an int argument yields int.
 *
 * References:
 * - PHPStan phpdoc-types: Conditional return types // E<noverify>: NoVerify mis-parses `$value` in the conditional return and attributes the diagnostic here
 * - Psalm type_syntax/conditional_types.md
 * - Intelephense Type-System.md: Conditional Return Type
 */

/**
 * @param string|int $value
 * @return ($value is string ? string : int) // E<phan>: Phan cannot extract conditional return annotations
 */
function identify(string|int $value): string|int
{
    return $value;
}

function takesString(string $value): void
{
}

// Known string argument → return should be string.
takesString(identify('ok'));

// Known int argument → return should be int, not string.
takesString(identify(1)); // E?: int branch of conditional return should not satisfy string