splitbrain / dokuwiki-plugin-data

Add and query structured data in your DokuWiki
http://www.dokuwiki.org/plugin:data
GNU General Public License v2.0
50 stars 34 forks source link

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

Open trollkotze opened 5 years ago

trollkotze commented 5 years ago

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 commented 5 years ago

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.

splitbrain commented 5 years ago

Can you send a pull request?

trollkotze commented 5 years ago

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.

layolayo commented 5 years ago

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']);
    }
Floffyko commented 4 years ago

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