9

help me please

geting error sizeof(): Parameter must be an array or an object that implements Countable

ErrorException {#654 ▼
  #message: "sizeof(): Parameter must be an array or an object that implements Countable"
  #code: 0
  #file: "C:\Primer_Proyecto\Ventas\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalModel.php"
  #line: 179
  #severity: E_WARNING
  trace: {▼
    C:\Primer_Proyecto\Ventas\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalModel.php:179 {▶}
    Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() {}
    C:\Primer_Proyecto\Ventas\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalModel.php:179 {▶}
    C:\Primer_Proyecto\Ventas\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalModel.php:281 {▶}
    C:\Primer_Proyecto\Ventas\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalModel.php:296 {▶}
    C:\Primer_Proyecto\Ventas\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php:557 {▶}
    C:\Primer_Proyecto\Ventas\app\paypal.php:26 {▼
      › try{\r
      › \t$payment->create($this->_apiContext);\r
      › }\r
      arguments: {▶}
    }

This is the paypal.php code


public function generate(){
    $payment = \PaypalPayment::payment()->setIntent("sale")
        ->setPayer($this->payer())
        ->setTransactions([$this->transaction()])
        ->setRedirectURLs($this->redirectURLs());

    try {
        $payment->create($this->_apiContext);
    }
    catch(\Exception $ex){
        dd($ex);
        exit(1);
    }

    return $payment;
}


public function __construct($shopping_cart){
    $this->_apiContext = \PaypalPayment::ApiContext($this->_ClientId, $this  ->_ClientSecrete);
    $config = config("paypal_payment");
    $flatConfig = array_dot($config);
    $this->_apiContext->setConfig($flatConfig);
    $this->shopping_cart = $shopping_cart;
}

I do not see the error, I have stayed too long looking for what is my mistake

Ulai Nava
  • 167
  • 1
  • 1
  • 12
  • `$this->_apiContext` appears not to contain what you think it does. So make a debug output using var_dump to check. – CBroe Mar 27 '18 at 07:07

1 Answers1

20

The error is in the paypal\rest-api-sdk-php package you are using. The version of the package you are using is, apparently, not exactly compatible with PHP 7.2.

The specific error you are getting has been fixed in the latest version of the package (1.13.0). Update the package to the latest version, and this issue will be fixed. I cannot say what other issues may pop up, though.

In the 1.12.0 version, the specific line that is failing is:

} elseif (sizeof($v) <= 0 && is_array($v)) {

In PHP 7.2, if $v is not Countable, the sizeof() call will emit a warning, and Laravel will turn that warning into an exception.

In the 1.13.0 version, they updated the condition to be

} elseif (is_array($v) && sizeof($v) <= 0) {

Now, sizeof() will only be called when $v is an array, and therefore is guaranteed to be Countable, thus eliminating the warning.

patricus
  • 59,488
  • 15
  • 143
  • 145
  • OMG and i've been trying to figure this out for hours... my current version is: `PHP 7.2.7-1+ubuntu17.10.1+deb.sury.org+1 (cli) (built: Jun 22 2018 08:45:20) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.7-1+ubuntu17.10.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans` --- how can i know if it's 1.13.0 or 1.12.0? I have tried `apt-get update/upgrade` and it didn't affect php in any way – Imnotapotato Jul 11 '18 at 14:10
  • 1
    @RickSanchez The `1.13.0` vs `1.12.0` versions specified are referring to the version of the `paypal\rest-api-sdk-php` package installed by composer. This is not talking about a system level package. If you'd like to know exactly what version of that package you're using, you'd need to look inside your `composer.lock` file. If you need more help, I'd suggest posting a question. – patricus Jul 11 '18 at 17:27
  • 2
    php 7.3 also has a `is_countable()` that can be used to test the variable before counting https://www.php.net/manual/en/function.is-countable.php – Nathanael Feb 10 '20 at 10:41