← All results

Keyed destructuring from an array shape

Group: Arrays and shapes · Category: phpdoc · File: arrays_keyed_destructuring.php

Keyed destructuring, ['user' => $user] = $shape, takes each target's type from the matching key of the shape. The positional form is covered by arrays_list_shape_destructuring; this is the string-keyed counterpart. References: - https://wiki.php.net/rfc/list_keys

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
arrays_keyed_destructuring.php:47: Call to undeclared method \Conformance\Tests\ArraysKeyedDestructuring\User::total [PhanUndeclaredMethod]
arrays_keyed_destructuring.php:49: Call to undeclared method \Conformance\Tests\ArraysKeyedDestructuring\Order::name [PhanUndeclaredMethod]
arrays_keyed_destructuring.php:55: Call to undeclared method \Conformance\Tests\ArraysKeyedDestructuring\User::missing [PhanUndeclaredMethod]
phpstan 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass reported Lv.2+
arrays_keyed_destructuring.php:47: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\User::total(). [reported-from-level=2]
arrays_keyed_destructuring.php:47: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
arrays_keyed_destructuring.php:49: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\Order::name(). [reported-from-level=2]
arrays_keyed_destructuring.php:49: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
arrays_keyed_destructuring.php:55: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\User::missing(). [reported-from-level=2]
arrays_keyed_destructuring.php:55: Parameter #1 (mixed) of echo cannot be converted to string. [reported-from-level=10]
With strict-rules
arrays_keyed_destructuring.php:47: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\User::total().
arrays_keyed_destructuring.php:47: Parameter #1 (mixed) of echo cannot be converted to string.
arrays_keyed_destructuring.php:49: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\Order::name().
arrays_keyed_destructuring.php:49: Parameter #1 (mixed) of echo cannot be converted to string.
arrays_keyed_destructuring.php:55: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\User::missing().
arrays_keyed_destructuring.php:55: Parameter #1 (mixed) of echo cannot be converted to string.
psalm 6.20.0Psalm 6.20.0@804f03c0c183d149fcd4b8c7c0c9b3fbf836d34c
next: 7.0.0-rc2
Pass
arrays_keyed_destructuring.php:47: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
arrays_keyed_destructuring.php:47: Method Conformance\Tests\ArraysKeyedDestructuring\User::total does not exist [UndefinedMethod]
arrays_keyed_destructuring.php:49: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
arrays_keyed_destructuring.php:49: Method Conformance\Tests\ArraysKeyedDestructuring\Order::name does not exist [UndefinedMethod]
arrays_keyed_destructuring.php:55: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
arrays_keyed_destructuring.php:55: Method Conformance\Tests\ArraysKeyedDestructuring\User::missing does not exist [UndefinedMethod]
mago 1.57.0mago 1.57.0 Pass
arrays_keyed_destructuring.php:47: Method `total` does not exist on type `Conformance\Tests\ArraysKeyedDestructuring\User`. [non-existent-method]
arrays_keyed_destructuring.php:47: The first value for `echo` is too general. [mixed-argument]
arrays_keyed_destructuring.php:49: Method `name` does not exist on type `Conformance\Tests\ArraysKeyedDestructuring\Order`. [non-existent-method]
arrays_keyed_destructuring.php:49: The first value for `echo` is too general. [mixed-argument]
arrays_keyed_destructuring.php:55: Method `missing` does not exist on type `Conformance\Tests\ArraysKeyedDestructuring\User`. [non-existent-method]
arrays_keyed_destructuring.php:55: The first value for `echo` is too general. [mixed-argument]
mir 0.87.0mir 0.87.0 Pass
arrays_keyed_destructuring.php:47: UndefinedMethod: Method Conformance\Tests\ArraysKeyedDestructuring\User::total() does not exist [MIR0004]
arrays_keyed_destructuring.php:49: UndefinedMethod: Method Conformance\Tests\ArraysKeyedDestructuring\Order::name() does not exist [MIR0004]
arrays_keyed_destructuring.php:55: UndefinedMethod: Method Conformance\Tests\ArraysKeyedDestructuring\User::missing() does not exist [MIR0004]
phpantom 0.11.1phpantom_lsp 0.11.1 Pass
arrays_keyed_destructuring.php:47: Method 'total' not found on class 'Conformance\Tests\ArraysKeyedDestructuring\User' [unknown_member]
arrays_keyed_destructuring.php:49: Method 'name' not found on class 'Conformance\Tests\ArraysKeyedDestructuring\Order' [unknown_member]
arrays_keyed_destructuring.php:55: Method 'missing' not found on class 'Conformance\Tests\ArraysKeyedDestructuring\User' [unknown_member]
intelephense 1.18.5intelephense 1.18.5 Pass
arrays_keyed_destructuring.php:47: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
arrays_keyed_destructuring.php:47: Undefined method 'total'. [P1013]
arrays_keyed_destructuring.php:49: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
arrays_keyed_destructuring.php:49: Undefined method 'name'. [P1013]
arrays_keyed_destructuring.php:55: Expected type 'string|Stringable|null'. Found 'mixed'. [P1006]
arrays_keyed_destructuring.php:55: Undefined method 'missing'. [P1013]
phpactor 2026.10.07.0Phpactor 2026.10.07.0 Fail

No diagnostics reported.

Expectation diff
Line 47: Expected 1 error(s)
Line 49: Expected 1 error(s)
Line 55: Expected 1 error(s)
phpy 1.0.19409phpy 1.0.19409 Pass
arrays_keyed_destructuring.php:47: Call to unknown method: Conformance\Tests\ArraysKeyedDestructuring\User::total()
arrays_keyed_destructuring.php:49: Call to unknown method: Conformance\Tests\ArraysKeyedDestructuring\Order::name()
arrays_keyed_destructuring.php:55: Call to unknown method: Conformance\Tests\ArraysKeyedDestructuring\User::missing()
qodana 262.10968.76PhpStorm 262.10968.76 Fail

No diagnostics reported.

Expectation diff
Line 47: Expected 1 error(s)
Line 49: Expected 1 error(s)
Line 55: 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 Pass
arrays_keyed_destructuring.php:47: Call to undefined method {\Conformance\Tests\ArraysKeyedDestructuring\User}->total() [undefinedMethod]
arrays_keyed_destructuring.php:49: Call to undefined method {\Conformance\Tests\ArraysKeyedDestructuring\Order}->name() [undefinedMethod]
arrays_keyed_destructuring.php:55: Call to undefined method {\Conformance\Tests\ArraysKeyedDestructuring\User}->missing() [undefinedMethod]
steins 0.1.8steins 0.1.8 (2026-09-21 revision 76306a9) Fail

No diagnostics reported.

Expectation diff
Line 47: Expected 1 error(s)
Line 49: Expected 1 error(s)
Line 55: Expected 1 error(s)
phpstan-strict 2.3.1PHPStan - PHP Static Analysis Tool 2.3.1 Pass
arrays_keyed_destructuring.php:47: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\User::total().
arrays_keyed_destructuring.php:47: Parameter #1 (mixed) of echo cannot be converted to string.
arrays_keyed_destructuring.php:49: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\Order::name().
arrays_keyed_destructuring.php:49: Parameter #1 (mixed) of echo cannot be converted to string.
arrays_keyed_destructuring.php:55: Call to an undefined method Conformance\Tests\ArraysKeyedDestructuring\User::missing().
arrays_keyed_destructuring.php:55: Parameter #1 (mixed) of echo cannot be converted to string.
psalm-next 7.0.0-rc2Psalm 7.0.0-rc2@20339e3fe5b8d03bcd8061030eb2a83ad50e5783 Pass
arrays_keyed_destructuring.php:47: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
arrays_keyed_destructuring.php:47: Method Conformance\Tests\ArraysKeyedDestructuring\User::total does not exist [UndefinedMethod]
arrays_keyed_destructuring.php:49: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
arrays_keyed_destructuring.php:49: Method Conformance\Tests\ArraysKeyedDestructuring\Order::name does not exist [UndefinedMethod]
arrays_keyed_destructuring.php:55: Argument 1 of echo cannot be mixed, expecting string [MixedArgument]
arrays_keyed_destructuring.php:55: Method Conformance\Tests\ArraysKeyedDestructuring\User::missing does not exist [UndefinedMethod]

Source

<?php

declare(strict_types=1);

namespace Conformance\Tests\ArraysKeyedDestructuring;

/**
 * Keyed destructuring from an array shape.
 *
 * Keyed destructuring, `['user' => $user] = $shape`, takes each target's type
 * from the matching key of the shape. The positional form is covered by
 * arrays_list_shape_destructuring; this is the string-keyed counterpart.
 *
 * References:
 * - https://wiki.php.net/rfc/list_keys
 */

final class User
{
    public function name(): string
    {
        return 'user';
    }
}

final class Order
{
    public function total(): int
    {
        return 1;
    }
}

/** @return array{user: User, order: Order} */
function load(): array
{
    return ['user' => new User(), 'order' => new Order()];
}

function destructures(): void
{
    ['user' => $user, 'order' => $order] = load();
    echo $user->name(); // V
    echo $order->total(); // V

    ['user' => $customer] = load();
    echo $customer->total(); // E: the user key holds a User
    ['order' => $purchase] = load();
    echo $purchase->name(); // E: the order key holds an Order
}

function destructuresLiteral(): void
{
    ['user' => $literal] = ['user' => new User()];
    echo $literal->missing(); // E: the literal's user key holds a User
}