halfpastfouram /
phpchartjs
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Halfpastfour\PHPChartJS\Renderer; |
||
| 4 | |||
| 5 | use Laminas\Json\Json as JsonHelper; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class Json |
||
| 9 | * |
||
| 10 | * @package Halfpastfour\PHPChartJS\Renderer |
||
| 11 | */ |
||
| 12 | class Json extends Renderer |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Render the necessary JSON for the chart to function in the frontend. |
||
| 16 | * |
||
| 17 | * @param int|false $flags |
||
| 18 | * |
||
| 19 | * @return string |
||
| 20 | */ |
||
| 21 | public function render($flags = null) |
||
| 22 | { |
||
| 23 | $config = [ |
||
| 24 | 'type' => constant(get_class($this->chart) . "::TYPE"), |
||
| 25 | 'data' => [], |
||
| 26 | ]; |
||
| 27 | |||
| 28 | $labels = $this->chart->labels()->jsonSerialize(); |
||
| 29 | if ($labels) { |
||
|
0 ignored issues
–
show
|
|||
| 30 | $config['data']['labels'] = $labels; |
||
| 31 | } |
||
| 32 | |||
| 33 | $dataSets = $this->chart->dataSets()->jsonSerialize(); |
||
| 34 | if ($dataSets) { |
||
|
0 ignored issues
–
show
The expression
$dataSets of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 35 | $config['data']['datasets'] = $dataSets; |
||
| 36 | } |
||
| 37 | |||
| 38 | $options = $this->chart->options()->jsonSerialize(); |
||
| 39 | if (! empty($options)) { |
||
| 40 | $config['options'] = $options; |
||
| 41 | } |
||
| 42 | |||
| 43 | $output = JsonHelper::encode($config, false, ['enableJsonExprFinder' => true]); |
||
| 44 | if ($flags & Renderer::RENDER_PRETTY) { |
||
| 45 | $output = JsonHelper::prettyPrint($output); |
||
| 46 | } |
||
| 47 | |||
| 48 | return $output; |
||
| 49 | } |
||
| 50 | } |
||
| 51 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.