8000 Array union results in wrong union type when an optional array key is involved · Issue #4864 · phan/phan · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Array union results in wrong union type when an optional array key is involved #4864
Open
@tgr

Description

@tgr
global $x;
'@phan-var array{k?:1} $x';
'@phan-debug-var $x'; // array{k?:1}
$x += [ 'k' => 2 ];
'@phan-debug-var $x'; // array{k?:1}

There are two problems here:

  1. the k key should not be optional anymore;
  2. the type of the k field should be the union of the values of the two inputs of the += operator.

So the correct union type would be array{k:1|2}.

The first is the more annoying issue as this is a very common pattern:

/**
 * @param array $options An associative array of:
 *   - bar (bool, default false): ...
 * @phan-param array{bar?:bool} 
 */
function foo( array $options = [] ) {
    $options += [
        'bar' => false,
    ];
    if ( $options['bar'] ) ...
}

but it will trigger a PhanTypePossiblyInvalidDimOffset.

The snippets here were tested on https://phan.github.io/demo (Phan 5.4.1), the problem was originally seen in MediaWiki CI (5.4.3).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0