['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 | Version | Result | Diagnostics |
|---|---|---|---|
| phan | 6.0.11Phan 6.0.11 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.5PHPStan - PHP Static Analysis Tool 2.3.5 | 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-rulesarrays_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.9Psalm 6.20.9@f67170ac3c3b931f5088681e21d6c8babc601c08 next: 7.0.4 |
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.59.0mago 1.59.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.91.0mir 0.91.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 diffLine 47: Expected 1 error(s) Line 49: Expected 1 error(s) Line 55: Expected 1 error(s) |
| phpy | 1.0.19490phpy 1.0.19490 | 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.11879.104PhpStorm 262.11879.104 | Fail | No diagnostics reported. Expectation diffLine 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 diffLine 47: Expected 1 error(s) Line 49: Expected 1 error(s) Line 55: Expected 1 error(s) |
| phpstan-strict | 2.3.5PHPStan - PHP Static Analysis Tool 2.3.5 | 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.4Psalm 7.0.4@fa5b1f6e665cb743838ffd865b8fc96207f9409f | 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] |
<?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
}