← 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.11PHPStan - PHP Static Analysis Tool 2.2.11 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.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
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.47.4mago 1.47.4 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.72.1mir 0.72.1 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.07.22.0Phpactor 2026.07.22.0 Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpy 0.2.0phpy 0.2.0 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.6steins 0.1.6 (2026-08-25 revision c13b4c7) Not enforced

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
phpstan-strict 2.2.11PHPStan - PHP Static Analysis Tool 2.2.11 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-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 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