safe: Cannot redeclare Safe\array_combine()

Hi there, congrats on the project, I just discovered it and I think is great.

Now, I’ve tried to add it to one of my libraries and now when I run phpstan (with our without the rule) I get the following error:

PHP Fatal error:  Cannot redeclare Safe\array_combine() (previously declared in /home/user/project/vendor/thecodingmachine/safe/generated/array.php:20) in /home/user/project/vendor/thecodingmachine/safe/generated/array.php on line 20

Fatal error: Cannot redeclare Safe\array_combine() (previously declared in /home/user/project/vendor/thecodingmachine/safe/generated/array.php:20) in /home/user/project/vendor/thecodingmachine/safe/generated/array.php on line 20

In array.php line 20:
                                                                                                                                                              
  Cannot redeclare Safe\array_combine() (previously declared in /home/user/project/vendor/thecodingmachine/safe/generated/array.php:20)  
                                                                                                                                                              

The file obviously don’t have the function declared twice and I’m completely lost as for where it could be coming from. Any ideas? I’m on 1.3.3.

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 21 (9 by maintainers)

Most upvoted comments

@asbiin You are right, it isn’t fixed, or at least, the fix isn’t merged yet. There was a MR which we thought fixed the error but it was reverted since it actually didn’t fix it (#333 if I’m not mistaken). Anyway, there’s an open MR right now (#350) which should fix it for good. Also, if #348 gets into an actual MR and it gets merged will also fix this. I don’t know why this issue was closed in the first place.

@Kharhamel Please consider storing class files and function files in separate directories.

It turns out I was wrong and the issue was not on larastan but on the Safe library.

I left a comment on the larastan issue commenting my findings but long story short, the issue comes from using array as a param or return type on PHPDocs. Since all functions are defined within the Safe namespace and Safe\array points to a file following PSR4 specs, PHPStan tries to load the class Safe\array by loading the file array.php, which was already loaded and then causes the Cannot redeclare errors.

Changing the type on the PHPDocs to \array (making the global namespace specific) fixes the error, but it needs to be changed on all Safe functions that deal or return an array.