← 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.10Phan 6.0.10 php-ast version 1.1.3 PHP version used to run Phan: 8.5.11 Pass
assertions_assert_non_empty_list.php:32: Invalid offset 0 of $values of array type array{} [PhanTypeInvalidDimOffset]
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 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.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
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.57.0mago 1.57.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.87.0mir 0.87.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.11.1phpantom_lsp 0.11.1 Pass
assertions_assert_non_empty_list.php:32: Return type null is incompatible with declared return type int [type_mismatch_return]
intelephense 1.18.5intelephense 1.18.5 Fail

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

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

No diagnostics reported.

Expectation diff
Line 32: Expected 1 error(s)
qodana 262.10968.76PhpStorm 262.10968.76 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.3.1PHPStan - PHP Static Analysis Tool 2.3.1 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-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 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];
}