dokuwiki-plugin-data icon indicating copy to clipboard operation
dokuwiki-plugin-data copied to clipboard

PHP 7.3: "Parameter must be an array or an object that implements Countable"

Open trollkotze opened this issue 5 years ago • 5 comments

Hello!

Since upgrading a server to PHP 7.3 (only from PHP 7.1 I believe?) there appear now warning messages ~~for admins~~ not only for admins, apparently stemming from code in the Data plugin: Warning: count(): Parameter must be an array or an object that implements Countable in /xxx/yyy/zzz/lib/plugins/data/syntax/entry.php on line 168 I am not an expert in PHP and the new stricter type checks that seems to have been introduced. But here is the code where it happens:

    /**
     * Output the data in a table
     *
     * @param array               $data
     * @param Doku_Renderer_xhtml $R
     */
    function _showData($data, $R) {
        global $ID;
        $ret = '';

        $sectionEditData = ['target' => 'plugin_data'];
        if (!defined('SEC_EDIT_PATTERN')) {
            // backwards-compatibility for Frusterick Manners (2017-02-19)
            $sectionEditData = 'plugin_data';
        }
        $data['classes'] .= ' ' . $R->startSectionEdit($data['pos'], $sectionEditData);

        $ret .= '<div class="inline dataplugin_entry ' . $data['classes'] . '"><dl>';
        $class_names = array();
        foreach($data['data'] as $key => $val) {
// The following line causes the warning:
            if($val == '' || !count($val)) continue;
            $type = $data['cols'][$key]['type'];

       // ...

Not sure what this $data array contains, but it seems PHP 7.3. is warning that it may be of some type for which count(...) is not applicable.

Probably this is just one small instance of possibly more scenarios where PHP 7.3 does some stricter checking than previous PHP versions.

Since it's only putting out "warning", I assume that nothing is really broken by it. But it might be worthwhile to see how to satisfy PHP 7.3 overall.

trollkotze avatar Jan 13 '19 13:01 trollkotze

Changing the line if($val == '' || !count($val)) continue; to if($val == '' || is_null($val) || (is_array($val) && count($val) == 0)) continue; seems to be a valid workaround.

trollkotze avatar Apr 01 '19 10:04 trollkotze

Can you send a pull request?

splitbrain avatar Apr 04 '19 10:04 splitbrain

Okay, I did. Just am not completely sure if that is all there is to it, just my particular use-case does not print a warning anymore.

Oh, and it seems Travis CI fails for all 7.x PHP versions. But not showing what exactly fails, which lines of code are wrong. I suspect it might be just because of other similar cases where some instructions were too laissez-faire for PHP 7.x.

trollkotze avatar Apr 05 '19 02:04 trollkotze

Had the same issue with the discussions plugin - line 425 in action.php (docuwiki/lib/plugins/discussion) $cnt = count($data['comments'])

is changed to:

    if($data['comments'] == '' || is_null($data['comments']) || (is_array($data['comments']) && count($data['comments']) == 0)) {
		$cnt = 0;
	} else {
		$cnt = count($data['comments']);
	}

layolayo avatar Jun 07 '19 00:06 layolayo

Same story here. Waiting for a verified solution. The workaround above is welcome.

Floffyko avatar Oct 09 '19 17:10 Floffyko