← All results

Positional list destructuring of a string-keyed array

Group: Cross-analyzer regressions · Category: regression · File: regressions_list_destructure_string_key.php

[$a, $b] = $stringKeyed reads the positional integer keys 0 and 1, but a array<string, int> has no integer keys — the destructured variables are always undefined at runtime. Analyzers diverge on whether they detect this: Mago reports the string-key / undefined-index mismatch, while others accept the destructuring silently. Source lead: Mago analyzer test list_destructure_string_key_simple (crates/analyzer/tests/cases). This is the inverse direction from the narrowing cases — here Mago is the strict tool and the others miss the defect.

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
regressions_list_destructure_string_key.php:28: Attempting an array destructing assignment with a key of type int but the only key types of the right-hand side are of type string [PhanTypeMismatchArrayDestructuringKey]
phpstan 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass reported Lv.3+
regressions_list_destructure_string_key.php:28: Offset 0 does not exist on array<string, int>. [identifier=offsetAccess.notFound] [reported-from-level=3]
regressions_list_destructure_string_key.php:28: Offset 1 does not exist on array<string, int>. [identifier=offsetAccess.notFound] [reported-from-level=3]
regressions_list_destructure_string_key.php:30: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] [reported-from-level=10]
regressions_list_destructure_string_key.php:31: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString] [reported-from-level=10]
With strict-rules
regressions_list_destructure_string_key.php:28: Offset 0 does not exist on array<string, int>. [identifier=offsetAccess.notFound]
regressions_list_destructure_string_key.php:28: Offset 1 does not exist on array<string, int>. [identifier=offsetAccess.notFound]
regressions_list_destructure_string_key.php:30: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString]
regressions_list_destructure_string_key.php:31: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString]
psalm 6.16.1Psalm 6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac
next: 7.0.0-beta19
Fail

No diagnostics reported.

Expectation diff
Line 28: Expected 1 error(s)
mago 1.46.0mago 1.46.0 Pass
regressions_list_destructure_string_key.php:28: List-style destructuring of a value with non-integer keys (`array<string, int>`). [list-destructure-string-key]
regressions_list_destructure_string_key.php:28: Invalid array key type: `int(0)` is not a valid key for this array. [mismatched-array-index]
regressions_list_destructure_string_key.php:28: Invalid array key type: `int(1)` is not a valid key for this array. [mismatched-array-index]
mir 0.70.1mir 0.70.1 Fail

No diagnostics reported.

Expectation diff
Line 28: Expected 1 error(s)
phpantom 0.9.0phpantom_lsp 0.9.0 Fail

No diagnostics reported.

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

No diagnostics reported.

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

No diagnostics reported.

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

No diagnostics reported.

Expectation diff
Line 28: 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

No diagnostics reported.

Expectation diff
Line 28: Expected 1 error(s)
steins 0.1.5steins 0.1.5 (2026-08-12 revision 39b4cc1) Pass
regressions_list_destructure_string_key.php:28: offset 0 is outside the declared shape — stringKeyed() is array<string, int>, which cannot carry the key; reads null with "Undefined array key 0" [offset.undeclared]
regressions_list_destructure_string_key.php:28: offset 1 is outside the declared shape — stringKeyed() is array<string, int>, which cannot carry the key; reads null with "Undefined array key 1" [offset.undeclared]
phpstan-strict 2.2.8PHPStan - PHP Static Analysis Tool 2.2.8 Pass
regressions_list_destructure_string_key.php:28: Offset 0 does not exist on array<string, int>. [identifier=offsetAccess.notFound]
regressions_list_destructure_string_key.php:28: Offset 1 does not exist on array<string, int>. [identifier=offsetAccess.notFound]
regressions_list_destructure_string_key.php:30: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString]
regressions_list_destructure_string_key.php:31: Parameter #1 (mixed) of echo cannot be converted to string. [identifier=echo.nonString]
psalm-next 7.0.0-beta19Psalm 7.0.0-beta19@7e751c06a756fa64dc4c759c09fe4a173afcb433 Fail

No diagnostics reported.

Expectation diff
Line 28: Expected 1 error(s)

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\RegressionsListDestructureStringKey;

/**
 * Positional list destructuring of a string-keyed array.
 *
 * `[$a, $b] = $stringKeyed` reads the positional integer keys 0 and 1, but a
 * `array<string, int>` has no integer keys — the destructured variables are always
 * undefined at runtime. Analyzers diverge on whether they detect this: Mago reports the
 * string-key / undefined-index mismatch, while others accept the destructuring silently.
 *
 * Source lead: Mago analyzer test `list_destructure_string_key_simple`
 * (crates/analyzer/tests/cases). This is the inverse direction from the narrowing cases —
 * here Mago is the strict tool and the others miss the defect.
 */

/** @return array<string, int> */
function stringKeyed(): array
{
    return ['a' => 1, 'b' => 2];
}

function caller(): void
{
    [$a, $b] = stringKeyed(); // E: offsets 0 and 1 do not exist on array<string, int>, so the destructure cannot succeed

    echo $a; // E<phpstan>: $a is mixed after the failed destructure, not string-convertible // E<phpstan-strict>: same
    echo $b; // E<phpstan>: $b is mixed after the failed destructure, not string-convertible // E<phpstan-strict>: same
}