← 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.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Enforced
conditionals_true_return.php:40: Argument 1 ($value) is asTime(true) of type float but \Conformance\Tests\ConditionalsTrueReturn\takesString() takes string defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/conditionals_true_return.php:29 [PhanTypeMismatchArgument]
conditionals_true_return.php:41: Argument 1 ($value) is asTime(false) of type string but \Conformance\Tests\ConditionalsTrueReturn\takesFloat() takes float defined at /Users/megurine/repo/php/php-typing-conformance/conformance/tests/conditionals_true_return.php:33 [PhanTypeMismatchArgument]
  • Recognition: spelling resolved
  • Enforcement: 2/2 of the expected violations reported
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 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.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
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.57.0mago 1.57.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.87.0mir 0.87.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.11.1phpantom_lsp 0.11.1 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.10.07.0Phpactor 2026.10.07.0 Not enforced

No diagnostics reported.

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

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported
qodana 262.10968.76PhpStorm 262.10968.76 Widened to string|float

No diagnostics reported.

  • Recognition: spelling resolved
  • Enforcement: 0/2 of the expected violations reported

Notes: The conditional is not evaluated: `asTime(true)` and `asTime(false)` are both inferred as `float|string`, the union of its branches, and that union passes either parameter because PhpStorm accepts a union wherever one of its members fits.

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.3.1PHPStan - PHP Static Analysis Tool 2.3.1 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-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 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