Skip to content

Incorrect type inferred from array destructuring #7155

Closed
phpstan/phpstan-src
#5857

Description

@devfrey

Bug report

I'm sorry for the lack of a more specific title. I don't know how else to describe this issue.

PHPStan is unable to correctly infer the type from the following array destructure:

/** @var \Closure|string $event */
$event = '';
$callback = null;

if ($event instanceof \Closure) {
    [$event, $callback] = ['event-name', $event];
}

// $callback should be Closure|null, but is reported as 'event-name'|null

This example is based on this real-world code.

Code snippet that reproduces the problem

https://phpstan.org/r/f75d63a2-9a54-46ee-8f89-ecf6de70c5a9

Expected output

PHPStan should infer $callback as \Closure|null.

Activity

  1. ondrejmirtes commented on Apr 29, 2022

    @ondrejmirtes
    Member

    Root cause: https://phpstan.org/r/d12b75ee-fa29-4c38-8177-b7159d2665e2

    Array destructuring is confused when overwriting an existing variable.

  2. added this to the Easy fixes milestone on Apr 29, 2022
  3. 13 remaining items

  4. github-actions commented on Oct 4, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  5. locked as resolved and limited conversation to collaborators on Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions