Skip to content

Fix phpstan/phpstan#13674: infer non-empty-list after isset($list[$i])#5161

Closed
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-d3lg0np
Closed

Fix phpstan/phpstan#13674: infer non-empty-list after isset($list[$i])#5161
phpstan-bot wants to merge 2 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-d3lg0np

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

This PR fixes phpstan/phpstan#13674

When isset($arr[$dim]) is used with a non-constant dimension (e.g. a variable $i), PHPStan now correctly narrows the array type to non-empty-array/non-empty-list.

Previously, isset($list[$i]) would narrow the dim type but not the array itself. Now, if both the PHPDoc type and native type are arrays, the array is narrowed to non-empty in the truthy branch.

The narrowing is guarded by checking that the native type is also an array, which prevents issues with template types in loop scope merging where the native type is mixed.

Examples

/** @param list<int> $list */
function foo(array $list, int $i): void {
    if (isset($list[$i])) {
        // Before: list<int>
        // After:  non-empty-list<int>
    }
}

/** @param array<string, int> $arr */
function bar(array $arr, string $key): void {
    if (isset($arr[$key])) {
        // Before: array<string, int>
        // After:  non-empty-array<string, int>
    }
}

@staabm staabm closed this Mar 9, 2026
@staabm staabm deleted the create-pull-request/patch-d3lg0np branch March 9, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants