2

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.

2
  • Hi, it's better to ask in GitHub PHPStan issues rather than here. Some types are only internally represented in PHPStan but you can't express them in PHPDocs. Commented Feb 11, 2020 at 9:29
  • Opened github.com/phpstan/phpstan/issues/3045 Commented Mar 2, 2020 at 22:24

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.