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() in PHPStan and Safe 2.0 #29

Open
webard opened this issue Jan 17, 2022 · 8 comments
Open

Cannot redeclare Safe\array_combine() in PHPStan and Safe 2.0 #29

webard opened this issue Jan 17, 2022 · 8 comments

Comments

@webard
Copy link

webard commented Jan 17, 2022

During vendor/bin/phpstan I have error. I have no idea how to solve this, because error file and line is same as first declaration of function.

Cannot redeclare Safe\array_combine() (previously declared in /var/www/html/vendor/thecodingmachine/safe/generated/array.php:20)

  at vendor/thecodingmachine/safe/generated/array.php:20
     16▕  * for each array isn't equal.
     17▕  * @throws ArrayException
     18▕  *
     19▕  */
  ➜  20▕ function array_combine(array $keys, array $values): array
     21▕ {
     22▕     error_clear_last();
     23▕     $result = \array_combine($keys, $values);
     24▕     if ($result === false) {

      +1 vendor frames 
  2   [internal]:0
      Whoops\Run::handleShutdown()
@dbrekelmans
Copy link
Collaborator

Hi @webard, do you have a public repo where this is reproducable so I can investigate?

@webard
Copy link
Author

webard commented Jan 17, 2022

Hi, unfortunately that is private repo. How can I help you?

@webard
Copy link
Author

webard commented Jan 17, 2022

Everything is fine with versions:

  • thecodingmachine/safe v1.3.3
  • thecodingmachine/phpstan-safe-rule v1.1.0

With versions:

thecodingmachine/safe v2.0.2
thecodingmachine/phpstan-safe-rule v1.2.0

I found something weird.

I use laravel v8.79, and these packages:

"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"symplify/phpstan-extensions": "^10.0.0",
"symplify/phpstan-rules": "^10.0.0",
"thecodingmachine/phpstan-safe-rule": "^1.1",
"thecodingmachine/phpstan-strict-rules": "^1.0",
"nunomaduro/larastan": "^1.0.0",

error with array_combine occurs when I use some of Safe\ function in config files. I use exec function in two places in my config files, when I delete use function Safe\exec from these two files, everything is fine:

 ------ ------------------------------------------------------------------------------------------------------- 
  Line   config/app.php                                                                                         
 ------ ------------------------------------------------------------------------------------------------------- 
  18     Function exec is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use  
         function Safe\exec;' at the beginning of the file to use the variant provided by the                   
         'thecodingmachine/safe' library.                                                                       
 ------ ------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------- 
  Line   config/sentry.php                                                                                      
 ------ ------------------------------------------------------------------------------------------------------- 
  8      Function exec is unsafe to use. It can return FALSE instead of throwing an exception. Please add 'use  
         function Safe\exec;' at the beginning of the file to use the variant provided by the                   
         'thecodingmachine/safe' library.                                                                       
 ------ ------------------------------------------------------------------------------------------------------- 

@webard
Copy link
Author

webard commented Jan 17, 2022

Problem occurs when use Safe\exec. With other functions (i checked json_encode, array_combine) used in config, everything is fine too!

@dbrekelmans
Copy link
Collaborator

Very strange.. not quite sure what would be the cause. I'm glad you found a workaround for now. I think perhaps the actual cause is located in the Safe library itself, not in the phpstan plugin.

@mariusjp
Copy link

mariusjp commented Feb 24, 2022

Because the issue related to the safe library itself has been closed and people are being redirected here (by the last comment) I would like to clarify how to easily reproduce this issue.

I do believe this has nothing to do with this repo, but because of the above mentioned redirect:

I created a new laravel project with composer create-project laravel/laravel PROJECT_NAME and did two requires:

composer require nunomaduro/larastan
composer require thecodingmachine/safe

Which resulted in these versions:

laravel/framework: 9.2.0
nunomaduro/larastan: 2.0.1 
thecodingmachine/safe: 2.1.2
(phpstan/phpstan: 1.4.6)

Added a simple class:

<?php

declare(strict_types=1);

namespace App;

use Safe\Exceptions\NetworkException;

final class LarastanTest
{
    /**
     * @throws NetworkException
     */
    public function testDnsRecord(): void
    {
        $dns = \Safe\dns_get_record('https://foo.bar');
    }
}

And a phpstan.neon conf file:

includes:
    - ./vendor/nunomaduro/larastan/extension.neon
    - ./vendor/phpstan/phpstan/conf/bleedingEdge.neon

parameters:
    level: 9

I ran into this error:

Cannot redeclare Safe\array_flip() (previously declared in /Users/***/dev/code/stan_array_combine/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▕  */
  ➜  27▕ function array_flip(array $array): array
     28▕ {
     29▕     error_clear_last();
     30▕     $result = \array_flip($array);
     31▕     if ($result === null) {


   Whoops\Exception\ErrorException

  Cannot redeclare Safe\array_flip() (previously declared in /Users/***/dev/code/stan_array_combine/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▕  */
  ➜  27▕ function array_flip(array $array): array
     28▕ {
     29▕     error_clear_last();
     30▕     $result = \array_flip($array);
     31▕     if ($result === null) {

      +1 vendor frames
  2   [internal]:0
      Whoops\Run::handleShutdown()

BUT when I remove the include for nunomaduro/larastan in the phpstan.neon conf file and do a composer require thecodingmachine/phpstan-safe-rule (1.2.0) the error does NOT occur.

So I guess we need to once again "redirect" to another repo?

@dbrekelmans
Copy link
Collaborator

Hey @MJTheOne, thanks for the elaborate report!
Sorry, perhaps I was too quick to close the other issue.

Am I correct to say this only happens when used in combination with nunomaduro/larastan, not with phpstan/phpstan?
I know of larastan, but I'm not familiar with what it does compared to phpstan itself.
Perhaps the maintainers of larastan can shed some light on this issue?

@mariusjp
Copy link

mariusjp commented Mar 7, 2022

I must say, as elaborate as my answer was I did not consider just using phpstan/phpstan. I tested it with nunomaduro/larastan and with this repo thecodingmachine/phpstan-safe-rule.

In the first example I got the error, the second I did not.
Hence why I did feel the need to respond here because of the closure at the main repo thecodingmachine/safe

I could also have opened an issue at nunomaduro/larastan and linked to this issue in hindsight 🤔

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

No branches or pull requests

3 participants