← 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.15PHPStan - PHP Static Analysis Tool 2.2.15 Pass reported Lv.3+
assertions_assert_non_empty_list.php:32: Function Conformance\Tests\AssertionsAssertNonEmptyList\rejectsEmptyBranch() should return int but returns mixed. [reported-from-level=10]
assertions_assert_non_empty_list.php:32: Offset 0 does not exist on array{}. [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.
assertions_assert_non_empty_list.php:32: Offset 0 does not exist on array{}.
psalm 6.18.0Psalm 6.18.0@536dd6236b39a5115385b0307b42d6d1ad431a02
next: 7.0.0-beta22
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.50.0mago 1.50.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.78.0mir 0.78.0 Pass
assertions_assert_non_empty_list.php:32: NonExistentArrayOffset: Array offset '0' does not exist [MIR0301]
assertions_assert_non_empty_list.php:32: MixedReturnStatement: Cannot return a mixed type from function with declared return type 'int' [MIR1212]
phpantom 0.10.0phpantom_lsp 0.10.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)
phpactor 2026.06.23.0Phpactor 2026.06.23.0 Fail

No diagnostics reported.

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

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
qodana 262.9437.196Qodana 262.9437.196 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.8steins 0.1.8 (2026-09-21 revision 76306a9) 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.15PHPStan - PHP Static Analysis Tool 2.2.15 Pass
assertions_assert_non_empty_list.php:32: Function Conformance\Tests\AssertionsAssertNonEmptyList\rejectsEmptyBranch() should return int but returns mixed.
assertions_assert_non_empty_list.php:32: Offset 0 does not exist on array{}.
psalm-next 7.0.0-beta22Psalm 7.0.0-beta22@de27e5724ee5997a1e3baee4d394ca6f4e46eba0 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];
}