HuasoFoundries/jpgraph

Warning On PiePlot Legends

Closed this issue · 3 comments

$this->plots[$i]->Legend($this);

This make issues that caused for :

PHP Warning:  count(): Parameter must be an array or an object that implements Countable vendor/amenadiel/jpgraph/src/plot/PiePlot.php on line 361

$n = min(count($this->legends), count($this->data));

This small warning raise when use example from README.md

Oh. I tried to reproduce your error and couldn't. Then I saw that the changelog for count at php.net

Version |  Description
------------------------------------------
7.2.0   | count() will now yield a warning on invalid countable types passed to the array_or_countable parameter.

I could dedicate a few hours to adds checks for every usage of count (364 occurences across 60 files) to use it only on arrays. However, I'm not sure if the code uses count on Countable objects (remember, this project is just a fork of the abandoned jpGraph). Furthermore, the method is_countable has only been added as of PHP 7.3.

Perhaps, based on https://stackoverflow.com/questions/12346479/how-to-check-for-arrayness-in-php

I could declare a global polyfill as:

if (!function_exists('is_countable')) {
    function is_countable($c) {
        return is_array($c) || $c instanceof Countable;
    }
}

/**
 * Returns the item count of the variable, or zero if it's non countable
 *
 * @param mixed  $var  The variable whose items we want to count
 */
function safe_count($var) {
    if(is_countable($val)) {
        return count($val);
    }
    return 0;
}

Nah ...
But on some reason.
I think the code less of validation.

I found some error notice / warning also , but is OK(I've forgot where are) 😂
Because maybe very hard to used this lib,
So i was try to test with custom modded examples.

I'm releasing a new tag v3.6.20 fixing this issue

https://github.com/HuasoFoundries/jpgraph/releases/tag/v3.6.20