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

Cannot redeclare Safe\array_combine() #253

Closed
j3j5 opened this issue Oct 29, 2020 · 21 comments · Fixed by #333 or #350
Closed

Cannot redeclare Safe\array_combine() #253

j3j5 opened this issue Oct 29, 2020 · 21 comments · Fixed by #333 or #350

Comments

@j3j5
Copy link
Contributor

j3j5 commented Oct 29, 2020

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!

@Kharhamel
Copy link
Collaborator

I don't know sorry. Do you still have the issue if you remove the library and re install it with composer?

@j3j5
Copy link
Contributor Author

j3j5 commented Oct 29, 2020

Steps to reproduce:

  • Clone this repo
  • Run composer install
  • Run ./vendor/bin/phpstan analyse

It should be reproducible on your machine, I'm using PHP 7.4.11 just in case it has anything to do.

@j3j5
Copy link
Contributor Author

j3j5 commented Nov 13, 2020

I've been digging a bit further, the issue is only with the array_ functions but I can't find why phpstan thinks they've been previously declared. If I remove the line from composer.json that includes the array.php file and dump the autoloader classes again, phpstan runs just fine.

It is a very strange issue.

FWIW, installing the library on another project and using it doesn't seem to throw the error, so everything points to an error on phpstan and how it loads the autoloader, but I have no idea where else to look.

@nepda
Copy link

nepda commented Dec 15, 2020

Do you have any updates on this?

@mariusjp
Copy link

mariusjp commented Mar 9, 2021

This is still an issue. Just ran into it myself.

Edit after annoyed post:
I've pinned it down to the usage of Safe\dns_get_record();

When I omit this from my codebase it works fine.
thecodingmachine/safe: v1.3.3
nunomaduro/larastan: v0.6.13

@nepda
Copy link

nepda commented Feb 9, 2022

It seems like the function_exists call is dropped, and the issue is a thing again.

@tpetry
Copy link

tpetry commented Feb 15, 2022

It's happening on 2.1.2 for me too.

@webard
Copy link

webard commented Feb 23, 2022

In combination with phpstan or rector, the error still occurs (v2.1.2).

@dbrekelmans
Copy link
Collaborator

dbrekelmans commented Feb 24, 2022

As pointed out in #253 (comment), this issue is exclusive to the phpstan extension. As far as I can tell, we only declare Safe\array_combine once (see: https://github.com/thecodingmachine/safe/search?q=array_combine%28).

There is a bug report in the phpstan safe extension for this: thecodingmachine/phpstan-safe-rule#29, let's continue discussion there.

That said, I have no idea what causes this atm. If anyone is willing to look into it, go ahead!

@dbrekelmans
Copy link
Collaborator

I've re-opened this issue per thecodingmachine/phpstan-safe-rule#29 (comment)

@dongm2ez
Copy link

dongm2ez commented Mar 9, 2022

I have same problem. But function is array_flip.

PHP Fatal error: Cannot redeclare Safe\array_flip(),

thecodingmachine/safe: v2.1.2
nunomaduro/larastan: 1.0.3

@j3j5
Copy link
Contributor Author

j3j5 commented Mar 9, 2022

Left a comment on the larastan repo, I'm pretty sure the issue is on there. The different errors come from different versions of thecodingmachine/safe, it used to be only one array.php file but now there are 2 with different functions.

@j3j5
Copy link
Contributor Author

j3j5 commented Mar 16, 2022

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.

@szepeviktor
Copy link
Contributor

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

@asbiin
Copy link
Contributor

asbiin commented May 2, 2022

@j3j5 This one is still not fixed, maybe you could reopen the issue?
Thank you for your work! I hope you'll find a proper fix soon ...

@asbiin
Copy link
Contributor

asbiin commented May 2, 2022

I also have the exact same error with array_flip:

      Cannot redeclare Safe\array_flip() (previously declared in               
     vendor/thecodingmachine/safe/deprecated/array.php:27)                                                               
                                                                                
       at vendor/thecodingmachine/safe/deprecated/array.php:27                  
          23▕  * @throws ArrayException                                         
          24▕  * @deprecated The Safe version of this function is no longer     
     needed in PHP 8.0+                                                         
          25▕  *                                                                
          26▕  */                                                               
       ➜  27function array_flip(array $array): array                          
          28▕ {                                                                 
          29error_clear_last();                                           
          30$result = \array_flip($array);                                
          31if ($result === null) {                                       

@j3j5
Copy link
Contributor Author

j3j5 commented May 2, 2022

@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 Kharhamel reopened this May 9, 2022
@Kharhamel
Copy link
Collaborator

I created a new pre release tag to test @j3j5 new fix.

@Kharhamel
Copy link
Collaborator

@j3j5 did you test the new tag? Did it solve your issue?

@j3j5
Copy link
Contributor Author

j3j5 commented May 22, 2022

Yes, so far the error is gone from my packages that include both, larastan and safe. Also, I've been running the alpha on a personal project and I didn't find any issue so far.

@Kharhamel
Copy link
Collaborator

Very well, I think I will do an official release soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
10 participants