← All results

Comparison narrowing of float (>, >=, <, <=)

Group: Assertions and narrowing · Category: ecosystem · File: assertions_float_comparison_narrowing.php

Integers are discrete, so $n > 5 on int can become int<6, max>. Floats are not: $n > 1.5 excludes 1.5 without a user-visible next value, while $n >= 1.5 still admits it. Honour is silence on $n === 1.5 after >= / <=, and an always-false report after > / <. The unguarded === 1.5 is the control: an unrestricted float may be 1.5. Source: phpstan/phpstan 2.3.6 (phpstan/phpstan-src#6937). References: - https://phpstan.org/writing-php-code/phpdoc-types#float-ranges - phpdoc_advanced_fallback_float_range for the float<min, max> spelling

Analyzer results

AnalyzerVersionResultDiagnostics
phan 6.0.11Phan 6.0.11 php-ast version 1.1.3 PHP version used to run Phan: 8.5.12 Pass

No diagnostics reported.

phpstan 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 Pass reported Lv.4+
assertions_float_comparison_narrowing.php:27: Strict comparison using === between float<1.5000000000000002, max> and 1.5 will always evaluate to false. [reported-from-level=4]
assertions_float_comparison_narrowing.php:41: Strict comparison using === between float<min, 1.4999999999999998> and 1.5 will always evaluate to false. [reported-from-level=4]
With strict-rules
assertions_float_comparison_narrowing.php:27: Strict comparison using === between float<1.5000000000000002, max> and 1.5 will always evaluate to false.
assertions_float_comparison_narrowing.php:41: Strict comparison using === between float<min, 1.4999999999999998> and 1.5 will always evaluate to false.
phpstan-strict 2.3.7PHPStan - PHP Static Analysis Tool 2.3.7 Pass
assertions_float_comparison_narrowing.php:27: Strict comparison using === between float<1.5000000000000002, max> and 1.5 will always evaluate to false.
assertions_float_comparison_narrowing.php:41: Strict comparison using === between float<min, 1.4999999999999998> and 1.5 will always evaluate to false.
psalm 6.21.4Psalm 6.21.4@72023213487628df0bc5b2e02d68cbac614b7ef5 Pass

No diagnostics reported.

psalm 7 7.21.0Psalm 7.21.0@1a665f0114bd40cf12ad57a1a82066549d92f1b6 Pass

No diagnostics reported.

mago 1.61.0mago 1.61.0 Pass

No diagnostics reported.

mir 0.101.0mir 0.101.0 Pass

No diagnostics reported.

phpantom 1.1.0phpantom_lsp 1.1.0 Pass

No diagnostics reported.

pun 0.4.3pun 0.4.3 Pass

No diagnostics reported.

intelephense 1.18.5intelephense 1.18.5 Pass

No diagnostics reported.

phpactor 2026.11.22.0Phpactor 2026.11.22.0 Pass

No diagnostics reported.

phpy 1.0.19566phpy 1.0.19566 Pass

No diagnostics reported.

qodana 263.7307.215PhpStorm 263.7307.215 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

No diagnostics reported.

steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Pass

No diagnostics reported.

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\AssertionsFloatComparisonNarrowing;

/**
 * Comparison narrowing of `float` (`>`, `>=`, `<`, `<=`).
 *
 * Integers are discrete, so `$n > 5` on `int` can become `int<6, max>`.
 * Floats are not: `$n > 1.5` excludes `1.5` without a user-visible next
 * value, while `$n >= 1.5` still admits it. Honour is silence on
 * `$n === 1.5` after `>=` / `<=`, and an always-false report after `>` / `<`.
 * The unguarded `=== 1.5` is the control: an unrestricted `float` may be
 * `1.5`.
 *
 * Source: phpstan/phpstan 2.3.6 (phpstan/phpstan-src#6937).
 *
 * References:
 * - https://phpstan.org/writing-php-code/phpdoc-types#float-ranges
 * - phpdoc_advanced_fallback_float_range for the `float<min, max>` spelling
 */

function afterGreaterThan(float $n): void
{
    if ($n > 1.5) {
        echo $n === 1.5 ? 'eq' : 'gt'; // E?: after > 1.5, === 1.5 is always false
    }
}

function afterGreaterEqual(float $n): void
{
    if ($n >= 1.5) {
        echo $n === 1.5 ? 'eq' : 'ge'; // Q: >= 1.5 still allows 1.5
    }
}

function afterLessThan(float $n): void
{
    if ($n < 1.5) {
        echo $n === 1.5 ? 'eq' : 'lt'; // E?: after < 1.5, === 1.5 is always false
    }
}

function afterLessEqual(float $n): void
{
    if ($n <= 1.5) {
        echo $n === 1.5 ? 'eq' : 'le'; // Q: <= 1.5 still allows 1.5
    }
}

function noGuard(float $n): void
{
    echo $n === 1.5 ? 'eq' : 'other'; // V: unrestricted float may be 1.5
}