Magento – Magento 2.3.1 php 7.2 upgrade Parameter must be an array or an object

magento2.3PHP

Getting this error when trying to complete upgrade of Mgaento 2.2.2 (running on PHP7.1) to Magento 2.3.1 (running on PHP 7.2)

1 exception(s): Exception #0 (Exception): Warning: count(): Parameter
must be an array or an object that implements Countable in
/home/mytheme/public_html/app/design/frontend/Sm/topshop/Sm_Deals/templates/default.phtml
on line 16

I managed to fix a lot of errors with researching but stuck with this one.

$start = ($start <= 0 || $start > count($list)) ? 0 : $start - 1;

Best Answer

As of PHP 7.2 this is a warning about a non-Countable object being passed to count():

count() will now yield a warning on invalid countable types passed to the array_or_countable parameter.

Without any further code to look at (e.g. the code that defines the $list variable) or knowing what $list contains, I would guess that however $list is defined one of the valid results is null. You have two options:

  1. Check the value of $list before that line and redefine it if necessary:
if(!is_array($list) || !($list instanceof \Countable) {
    $list = [];
}
  1. Find out how $list is defined and fix it so it returns only an array or an object that implements the Countable interface.
Related Topic