Jump to content

PHP 8 Fatal Error


wyclef

Recommended Posts

Hello, I’m getting a fatal error when upgrading from PHP 7.4 to PHP 8:  Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in...

on the following count line.

 



function orderByDate($items) {
    //$tempDate = strtotime($item);
    $referenceArray = array();
    for ($i = 0; $i < count($items); $i++) {
        $referenceArray[] = strtotime($items[$i]['pubDate']);
    }
    array_multisort($referenceArray, SORT_DESC, $items);
    return $items;
}

 

Anyone know how to resolve this?

Edited by wyclef
Link to comment
Share on other sites

I think this fixes it? testing now... how does this look?

function orderByDate($items) {
    $referenceArray = array();
    for ($i = 0; $i < (is_countable($items) ? count($items) : 0); $i++) {
        $referenceArray[] = strtotime($items[$i]['pubDate']);
    }
    array_multisort($referenceArray, SORT_DESC, $items);
    return $items;
}

 

Link to comment
Share on other sites

As Barand has said - you really should have gone into your code to find the places that this function is being called and ensure that you are sending an array to it.  Obviously your approach has a 'bug' in it to be sending the invalid argument to this function.

OTOH - if you don't want to do this work - you could have simply added a test at the beginning of the function for is_array() and if it fails simply return a null or whatever you are expecting from this function.  Your convoluted expression makes for difficult reading when someone else comes along and tries to figure out your code.  As in:

function orderByDate($items) {
	if (!is_array($items))
		return null; // or something
	...
	...
	...

 

Link to comment
Share on other sites

Let's assume it is ok for null to be passed sometimes, and you aren't able to hunt that down and fix it. 

A better fix would be:

 

<?php

function orderByDate(?array $items): array 
{
    $items = $items ?? [];
    $referenceArray = array();
    foreach ($items as $item) {
        $referenceArray[] = strtotime($item['pubDate']);
    }
    array_multisort($referenceArray, SORT_DESC, $items);
    return $items;
}

This eliminates the need for a for loop and counter, adds the php7/8 typehints, and uses the null coalescing operator to handle a parameter when it's null.

This will allow either a valid array or null, and the end result is that you will get an empty array returned if null was passed, which I assume was the way it used to function.

A unit test would be better, but here's a little test script of the function demonstrating that it works as expected:

$t = [
    ['pubDate' => '10 September 2000', 'name' => 'apple'],
    ['pubDate' => 'now', 'name' => 'banana'],
    ['pubDate' => '+1 week', 'name' => 'coconut']
];

var_dump($t);

$t = orderByDate($t);
var_dump($t);

$t = orderByDate(null);
var_dump($t);

Output:

array(3) {
  [0]=>
  array(2) {
    ["pubDate"]=>
    string(17) "10 September 2000"
    ["name"]=>
    string(5) "apple"
  }
  [1]=>
  array(2) {
    ["pubDate"]=>
    string(3) "now"
    ["name"]=>
    string(6) "banana"
  }
  [2]=>
  array(2) {
    ["pubDate"]=>
    string(7) "+1 week"
    ["name"]=>
    string(7) "coconut"
  }
}
array(3) {
  [0]=>
  array(2) {
    ["pubDate"]=>
    string(7) "+1 week"
    ["name"]=>
    string(7) "coconut"
  }
  [1]=>
  array(2) {
    ["pubDate"]=>
    string(3) "now"
    ["name"]=>
    string(6) "banana"
  }
  [2]=>
  array(2) {
    ["pubDate"]=>
    string(17) "10 September 2000"
    ["name"]=>
    string(5) "apple"
  }
}
array(0) {
}

 

array_multisort is a strange, non-intuitive function, but it does perform some magic in this case.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.