phpstan / phpdoc-parser

Next-gen phpDoc parser with support for intersection types and generics
MIT License
1.36k stars 61 forks source link

Shared shape definition for union of object and array #187

Closed johnbillion closed 2 months ago

johnbillion commented 1 year ago

tl;dr:

(object|array){ foo: int, bar: string }

Several function parameters in WordPress can be passed as an associative array or a stdClass. This is an unfortunate result of some legacy functions casting arrays to objects with (object) $array.

The object shape support added in PHPStan 1.10.12 is helpful in this regard, thank you for working on it. It would be great if a shape could be shared in an object and array union to avoid duplicating all the elements of the shape.

Here's how such a parameter can be documented now:

@phpstan-param object{
  foo: int,
  bar: string,
  baz: bool,
}|array{
  foo: int,
  bar: string,
  baz: bool,
} $args

If the shape could be used for a union of object|array it would avoid this duplication.

@phpstan-param (object|array){
  foo: int,
  bar: string,
  baz: bool,
} $args
ondrejmirtes commented 2 months ago

Err, no :) Just repeat it in a union.

github-actions[bot] commented 1 month ago

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.