← All results

Conditional return type driven by a boolean parameter (is true)

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

The PHPStan 1.6 form: a flag chooses between the two sides of the native union. Tools that expand the condition accept the matching branch and reject the other; tools that fall back to string|float // E<noverify>: NoVerify reads $asFloat in the condition as a class name reject both uses as a union, including the valid control. References: - PHPStan 1.6.0: Conditional return types (microtime example) - PHPStan phpdoc-types: Conditional return types - 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 Unrecognized
conditionals_true_return.php:22: Saw unextractable annotation for comment '* @return ($asFloat is true ? float : string)' [PhanUnextractableAnnotation]
  • Recognition: spelling not resolved — reported on declaration line(s) 22
  • Enforcement: 0/2 of the expected violations reported — incidental, since the spelling was not resolved
phpstan 2.2.15PHPStan - PHP Static Analysis Tool 2.2.15 Enforced reported Lv.5+
conditionals_true_return.php:40: Parameter #1 $value of function Conformance\Tests\ConditionalsTrueReturn\takesString expects string, float given. [reported-from-level=5]
conditionals_true_return.php:41: Parameter #1 $value of function Conformance\Tests\ConditionalsTrueReturn\takesFloat expects float, string given. [reported-from-level=5]
With strict-rules
conditionals_true_return.php:40: Parameter #1 $value of function Conformance\Tests\ConditionalsTrueReturn\takesString expects string, float given.
conditionals_true_return.php:41: Parameter #1 $value of function Conformance\Tests\ConditionalsTrueReturn\takesFloat expects float, string given.
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm 6.18.0Psalm 6.18.0@536dd6236b39a5115385b0307b42d6d1ad431a02
next: 7.0.0-beta22
Enforced
conditionals_true_return.php:40: Argument 1 of Conformance\Tests\ConditionalsTrueReturn\takesString expects string, but float provided [InvalidScalarArgument]
conditionals_true_return.php:41: Argument 1 of Conformance\Tests\ConditionalsTrueReturn\takesFloat expects float, but string provided [InvalidScalarArgument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
mago 1.50.0mago 1.50.0 Enforced
conditionals_true_return.php:40: Invalid argument type for argument #1 of `Conformance\Tests\ConditionalsTrueReturn\takesString`: expected `string`, but found `float`. [invalid-argument]
conditionals_true_return.php:41: Invalid argument type for argument #1 of `Conformance\Tests\ConditionalsTrueReturn\takesFloat`: expected `float`, but found `string`. [invalid-argument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
mir 0.78.0mir 0.78.0 Enforced
conditionals_true_return.php:40: InvalidArgument: Argument $value of takesString() expects 'string', got 'float' [MIR0201]
conditionals_true_return.php:41: InvalidArgument: Argument $value of takesFloat() expects 'float', got 'string' [MIR0201]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
phpantom 0.10.0phpantom_lsp 0.10.0 Enforced
conditionals_true_return.php:40: Argument 1 ($value) expects string, got float [type_mismatch_argument]
conditionals_true_return.php:41: Argument 1 ($value) expects float, got string [type_mismatch_argument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
intelephense 1.18.5intelephense 1.18.5 Enforced
conditionals_true_return.php:40: Expected type 'string'. Found 'float'. [P1006]
conditionals_true_return.php:41: Expected type 'float'. Found 'string'. [P1006]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
phpactor 2026.06.23.0Phpactor 2026.06.23.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpy 1.0.19274phpy 1.0.19274 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
qodana 262.9437.196Qodana 262.9437.196 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Partly enforced (1/3)
conditionals_true_return.php:12: Class or interface named \Conformance\Tests\ConditionalsTrueReturn\$asFloat does not exist [undefinedClass]
  • Recognition: spelling resolved
  • Enforcement: 1/3 of the expected violations reported
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Enforced
conditionals_true_return.php:40: argument 0.0 (returned from asTime()) to takesString() cannot become string $value — proven TypeError (strict mode) [type.argument-mismatch]
conditionals_true_return.php:41: argument "0" (returned from asTime()) to takesFloat() cannot become float $value — proven TypeError (strict mode) [type.argument-mismatch]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
phpstan-strict 2.2.15PHPStan - PHP Static Analysis Tool 2.2.15 Enforced
conditionals_true_return.php:40: Parameter #1 $value of function Conformance\Tests\ConditionalsTrueReturn\takesString expects string, float given.
conditionals_true_return.php:41: Parameter #1 $value of function Conformance\Tests\ConditionalsTrueReturn\takesFloat expects float, string given.
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
psalm-next 7.0.0-beta22Psalm 7.0.0-beta22@de27e5724ee5997a1e3baee4d394ca6f4e46eba0 Enforced
conditionals_true_return.php:40: Argument 1 of Conformance\Tests\ConditionalsTrueReturn\takesString expects string, but float provided [InvalidScalarArgument]
conditionals_true_return.php:41: Argument 1 of Conformance\Tests\ConditionalsTrueReturn\takesFloat expects float, but string provided [InvalidScalarArgument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\ConditionalsTrueReturn;

/**
 * Conditional return type driven by a boolean parameter (`is true`).
 *
 * The PHPStan 1.6 form: a flag chooses between the two sides of the
 * native union. Tools that expand the condition accept the matching
 * branch and reject the other; tools that fall back to `string|float` // E<noverify>: NoVerify reads $asFloat in the condition as a class name
 * reject both uses as a union, including the valid control.
 *
 * References:
 * - PHPStan 1.6.0: Conditional return types (`microtime` example)
 * - PHPStan phpdoc-types: Conditional return types
 * - Intelephense Type-System.md: Conditional Return Type
 */

/**
 * @return ($asFloat is true ? float : string)
 */
function asTime(bool $asFloat): string|float // T: ($asFloat is true ? float : string)
{
    return $asFloat ? 0.0 : '0';
}

function takesString(string $value): void
{
}

function takesFloat(float $value): void
{
}

takesString(asTime(false)); // V: false branch is string
takesFloat(asTime(true)); // V: true branch is float

takesString(asTime(true)); // E?: true branch is float, not string
takesFloat(asTime(false)); // E?: false branch is string, not float