PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 24, 2022

[FIXED] How to represent a non-empty array?

 November 24, 2022     phpstan     No comments   

Issue

I tried using intersection of types

/**
 * @param array&nonEmpty $users
 */
function sayHello($users): void {
    echo 'Hello, ' . $users[0];
}

but it complains:

PHPDoc tag @param for parameter $users contains unresolvable type.

(live demo)

If I strip the annotation to just nonEmpty, the error changes to

Parameter $users of function sayHello() has invalid typehint type nonEmpty

I do see the class responsible for the nonEmpty type in the source tree as well as mentioned several times in the tests and issues but I just could not make it work.


Solution

Just to provide an answer for those coming from search engines:

PHPStan now has non-empty-array type you can use. Here is an example.



Answered By - Can Vural
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing