← All results

Assertion-driven narrowing to non-empty list

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

References: - PHP assert() - analyzer support for assert-driven narrowing - python-typing narrowing inspiration

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
assertions_assert_non_empty_list.php:32: Invalid offset 0 of $values of array type array{} [PhanTypeInvalidDimOffset]
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass reported Lv.3+
assertions_assert_non_empty_list.php:32: Function Conformance\Tests\AssertionsAssertNonEmptyList\rejectsEmptyBranch() should return int but returns mixed. [identifier=return.type] [reported-from-level=10]
assertions_assert_non_empty_list.php:32: Offset 0 does not exist on array{}. [identifier=offsetAccess.notFound] [reported-from-level=3]
With strict-rules
assertions_assert_non_empty_list.php:32: Function Conformance\Tests\AssertionsAssertNonEmptyList\rejectsEmptyBranch() should return int but returns mixed. [identifier=return.type]
assertions_assert_non_empty_list.php:32: Offset 0 does not exist on array{}. [identifier=offsetAccess.notFound]
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Pass
assertions_assert_non_empty_list.php:32: Cannot access value on empty array variable $values [EmptyArrayAccess]
assertions_assert_non_empty_list.php:32: Could not infer a return type [MixedReturnStatement]
mago 1.46.0mago 1.46.0 Pass
assertions_assert_non_empty_list.php:32: Cannot access elements of an empty list `list<never>`. [impossible-array-access]
assertions_assert_non_empty_list.php:32: Invalid return type for function `Conformance\Tests\AssertionsAssertNonEmptyList\rejectsEmptyBranch`: expected `int`, but found `null`. [invalid-return-statement]
mir 0.70.1mir 0.70.1 Pass
assertions_assert_non_empty_list.php:32: NonExistentArrayOffset: Array offset '0' does not exist [MIR0301]
phpantom 0.9.0phpantom_lsp 0.9.0 Fail

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
intelephense 1.18.5intelephense 1.18.5 Fail

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
phpy 0.2.0phpy 0.2.0 Fail

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
qodana 262.8665.325Qodana 262.8665.325 Fail

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
noverify 0.5.5NoVerify, version 0.5.5: built on: 2025.04.22 14:36:46 OS: r-kuchinskas arm64 Commit: 4774b82f8adc53fbef38f95e97af4b953b4d1529 Fail
assertions_assert_non_empty_list.php:19: param $values miss matched with phpdoc type <<list<int>>> [funcParamTypeMissMatch]
assertions_assert_non_empty_list.php:29: param $values miss matched with phpdoc type <<list<int>>> [funcParamTypeMissMatch]
Expectation diff
Line 32: Expected 1 error(s)
Line 19: Unexpected errors ["param $values miss matched with phpdoc type <<list<int>>> [funcParamTypeMissMatch]"]
Line 29: Unexpected errors ["param $values miss matched with phpdoc type <<list<int>>> [funcParamTypeMissMatch]"]
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Pass
assertions_assert_non_empty_list.php:32: offset 0 provably missing — $values is [] on this path; reads null with "Undefined array key 0" [offset.missing]
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass
assertions_assert_non_empty_list.php:32: Function Conformance\Tests\AssertionsAssertNonEmptyList\rejectsEmptyBranch() should return int but returns mixed. [identifier=return.type]
assertions_assert_non_empty_list.php:32: Offset 0 does not exist on array{}. [identifier=offsetAccess.notFound]
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Pass
assertions_assert_non_empty_list.php:32: Cannot access value on empty array variable $values [EmptyArrayAccess]
assertions_assert_non_empty_list.php:32: Could not infer a return type [MixedReturnStatement]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\AssertionsAssertNonEmptyList;

/**
 * Assertion-driven narrowing to non-empty list.
 *
 * References:
 * - PHP assert()
 * - analyzer support for assert-driven narrowing
 * - python-typing narrowing inspiration
 */

/**
 * @param list<int> $values
 */
function takesAtLeastOne(array $values): int
{
    assert($values !== []);

    return $values[0];
}

/**
 * @param list<int> $values
 */
function rejectsEmptyBranch(array $values): int
{
    if ($values === []) {
        return $values[0]; // E: empty-list branch should not permit index 0
    }

    return $values[0];
}