How to solve the error with Warning: count(): Parameter must be an array or an object that implements Countable in ....?

Magento throws in the logs these errors:
ERR (3): Warning: count(): Parameter must be an array or an object that implements Countable in .../app/code/local/Mage/Catalog/Block/Product/List.php on line 299
This code:
/**
 * Retrieve block cache tags based on product collection
*
 * @return array
*/
 public function getCacheTags()
{
 $data = array(self::CACHE_TAG);
 if ($category = Mage::registry('current_category')) {
 $data[] = Mage_Catalog_Model_Category::CACHE_TAG . "_" . $category->getId();
}
!!!This line 299 ---> if (count($products = $this->getProductList())) {
 foreach ($products as $p) {
 $data[] = Mage_Catalog_Model_Product::CACHE_TAG . "_" . $p->getId();
}
}
 return $data;
}

 public function getCacheLifetime()
{
 return ($this->getData('cache_lifetime'))?intval($this->getData('cache_lifetime')):3600;
}

}


I hope there's a simple way to fix it, tell me.
A good specialist in the wings there, and don't trust unfamiliar, was a sad experience, more than once. There is still a community of intellectuals and collective intelligence...
And Yes, still a problem to find a reliable and intelligent admin/provera of periodic remote work. Some have the ambition and the queries do not correspond to the level of other careless slobs. To pity on their experience is understood and excessive confidence only aggravated the situation. I know that there are smart guys, but without the appropriate knowledge and expertise difficult to assess the aptitude.
And that I have a mediocre online store, plans and potential for growth as Napoleon, but unfortunately stalled it the human factor.
March 23rd 20 at 19:13
2 answers
March 23rd 20 at 19:15
Solution
How to fix: "count(): Parameter must be an array or an object that implements Countable"?

As a "quick fix" may offer to get rid of count, as there is empty: if (!empty($products = $this->getProductList()))
Well, Yes, sort of helped that decision. Thanks for the help. - genesis.Buckridge commented on March 23rd 20 at 19:18
@genesis.Buckridge, there is a button for the solution. - Tavares87 commented on March 23rd 20 at 19:21
March 23rd 20 at 19:17
Solution
This error returns the count when it is passed null. Prior to php 7.2 will return 0, and with version 7.2 of php error will be described.

You can fix, so
$products = $this->getProductList();
if ($products) {


a better way
$products = $this->getProductList();
if (is_iterable($products)) {


and even better, like so
$products = $this->getProductList();
if (is_countable($products)) {

but for this option you need php or 7.3 or polyfil which can be found in the comments to the function description https://www.php.net/manual/ru/function.is-countable.php

Find more questions by tags MagentoPHP