Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to type hint truly empty arrays #5159

Closed
mfn opened this issue Jun 10, 2021 · 3 comments
Closed

Ability to type hint truly empty arrays #5159

mfn opened this issue Jun 10, 2021 · 3 comments

Comments

@mfn
Copy link

mfn commented Jun 10, 2021

Feature request

I brought this previously up #2739 but it was closed without action.

I would like to bring this up once more, the necessity to have:

  • something returning a "complex shape of array": array{foo:string,bar:string,baz:string,daz:string}
  • and in some cases returning an empty array
  • the use-case would be to improve the handling in legacy code bases, or code which can't switch to alternatives like value objects, for whatever reason

Example

/**
 * @return array{foo:string,bar:string,baz:string,daz:string}
*/
function foo()
{
  if (some error condition) {
    return [];
  }
  if (some other condition) {
    return [];
  }
  return [
    'foo' => '…',
    'bar' => '…',
    'baz' => '…',
    'daz' => '…',
  ];
}

One of the previously proposed solution was to make each key of the array optional: but this would mean it's legal to return a partial array which is not the intent here.

Another solution, as a workaround, was to declare it e.g. array{foo:string,bar:string,baz:string,daz:string} | array{dummy?:null}

But in the this begs the question: since we already have the great array shape type documentator:

can't we just make it support array{} ?

It seems it would fit it very nicely. This was suggested by @adaamz in #2739 (comment)

Thank you for reconsidering!

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jun 11, 2021

Although myself I don't understand why do you need to typehint a function that always returns an empty array - at that point you know you can replace the call with []. Such function is of very little usefulness....

but it was easy enough to implement it so I did it: phpstan/phpdoc-parser@ea0b174 + phpstan/phpstan-src@d36852a

@mfn
Copy link
Author

mfn commented Jun 11, 2021

Thank you!

I don't understand why do you need to typehint a function that always returns an empty array

But it's not "always", see my example!

Basically it's a legacy / idiotic behaviour 😼 to return [] instead of e.g. null which obviously would be more idiomatic but, again, I'm talking about legacy code here.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants