Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
trollkotze opened this issue Jan 13, 2019 · 5 comments

Comments

@trollkotze
Copy link
Contributor

trollkotze commented Jan 13, 2019

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
Copy link
Contributor Author

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
Copy link
Owner

Can you send a pull request?

@trollkotze
Copy link
Contributor Author

trollkotze commented Apr 5, 2019

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
Copy link

layolayo commented Jun 7, 2019

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
Copy link

Floffyko commented Oct 9, 2019

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

qurben added a commit to csrdelft/wiki that referenced this issue Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants