3

I'm working on a Laravel project and there happens to be a code snippet like this:

sprintf("%s %s", __('foo.bar.buzz'), __('foo.bar.baz'));

Note that the __ function is a helper function from Laravel. The PHPDoc comment for the __ function includes the line @return string|array|null.

This trigger a phpstan error since sprintf cannot take an array as one of its optional parameters.

:49    Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, array|string|null given.
:49    Parameter #3 ...$values of function sprintf expects bool|float|int|string|null, array|string|null given.

I added phpstan-ignore lines on the problematic code so it becomes

sprintf("%s %s",
    __('foo.bar.buzz'),  /* @phpstan-ignore argument.type */
    __('foo.bar.baz')    /* @phpstan-ignore argument.type */
);

and the error went away on my local.

However, when I push the changes to a remote GitLab repo, the errors still occured on the GitLab CI. I can make sure each __ function calls never returns an array.

Is there a workaround for this issue?

4
  • 2
    It’s better to report issues like these on PHPStan’s GitHub repository. Commented Aug 1, 2024 at 11:48
  • @OndřejMirtes yeah true, but I was hoping for a quicker response here Commented Aug 1, 2024 at 12:16
  • You'd definitely get a faster response on GitHub :) Commented Aug 15, 2024 at 9:51
  • @OndřejMirtes haha true you already replied on my issue there! Commented Aug 15, 2024 at 10:28

1 Answer 1

0

After checking the phpstan version used in the GitLab CI, I found out that it's using v1.8.11 while I have v1.11.8 on my local. Apparently /* @phpstan-ignore argument.type */ does not work on the former version. Changing it to /* @phpstan-ignore-line */ solved the problem.

I posted the same problem on the phpstan's Github here

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.