Skip to content

Commit

Permalink
Simplifed render.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Oct 4, 2023
1 parent f495df7 commit 595a5d5
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

- name: Run psalm
run: vendor/bin/psalm src
run: vendor/bin/psalm --config=psalm.xml
2 changes: 1 addition & 1 deletion all_batch.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ECHO -------------------------------------- START BATCH %time% -----------------
ECHO -------------------------------------- PHP-CS-FIXER -------------------------------------- && ^
.\vendor\bin\php-cs-fixer.bat fix --diff --dry-run && ^
ECHO -------------------------------------- PHP-PSALM ----------------------------------------- && ^
.\vendor\bin\psalm.bat --show-info=true && ^
.\vendor\bin\psalm.bat --config=psalm.xml --show-info=true && ^
ECHO -------------------------------------- PHP-STAN ------------------------------------------ && ^
.\vendor\bin\phpstan.bat analyse --no-progress --memory-limit=2G && ^
ECHO -------------------------------------- PHP-RECTOR ---------------------------------------- && ^
Expand Down
11 changes: 10 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand All @@ -18,4 +18,13 @@
</ignoreFiles>
</projectFiles>

<issueHandlers>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<file name="src/Highcharts/AbstractChart.php" />
<file name="src/Highcharts/Highchart.php" />
<file name="src/Highcharts/Highstock.php" />
</errorLevel>
</PropertyNotSetInConstructor>
</issueHandlers>
</psalm>
97 changes: 42 additions & 55 deletions src/Highcharts/AbstractChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use Laminas\Json\Json;

/**
* @psalm-suppress PropertyNotSetInConstructor
* Abstract chart.
*/
abstract class AbstractChart implements ChartInterface
{
// the script end line
private const END_LINE = ",\n";

// the space prefix
private const SPACE = ' ';

Expand Down Expand Up @@ -91,7 +94,7 @@ protected function createExpression(string $expression, bool $trim = true): Expr
*/
protected function getRenderTo(): string
{
return $this->chart->renderTo ?? 'chart';
return (string) ($this->chart->renderTo ?? 'chart');
}

protected function initArrayOption(string $name): void
Expand All @@ -104,42 +107,40 @@ protected function initChartOption(string $name): void
$this->{$name} = new ChartOption($name);
}

protected function jsonEncode(ChartOption $chartOption): string
protected function jsonEncode(ChartOption|array $data, string $name = ''): string
{
if ($chartOption->hasData()) {
$name = $chartOption->getName();
$encoded = \json_encode($chartOption->getData());

return self::SPACE . "$name: $encoded,\n";
if ($data instanceof ChartOption) {
$name = $data->getName();
$data = $data->getData();
}

return '';
}

protected function renderCallbackArray(array $data, string $name): string
{
if ([] !== $data) {
// Zend\Json is used in place of json_encode to preserve JS anonymous functions
$encoded = Json::encode($data, false, self::ZEND_ENCODE_OPTIONS);

return self::SPACE . "$name: $encoded,\n";
if ([] === $data) {
return '';
}

return '';
$encoded = \json_encode($data);

return self::SPACE . "$name: $encoded" . self::END_LINE;
}

protected function renderCallbackOption(ChartOption $chartOption): string
protected function renderCallback(ChartOption|array $data, string $name = ''): string
{
if ($chartOption->hasData()) {
return $this->renderCallbackArray($chartOption->getData(), $chartOption->getName());
if ($data instanceof ChartOption) {
$name = $data->getName();
$data = $data->getData();
}
if ([] === $data) {
return '';
}

return '';
// Zend\Json is used in place of json_encode to preserve JS anonymous functions
$encoded = Json::encode(valueToEncode: $data, options: self::ZEND_ENCODE_OPTIONS);

return self::SPACE . "$name: $encoded" . self::END_LINE;
}

protected function renderChart(): string
{
return $this->renderCallbackOption($this->chart);
return $this->renderCallback($this->chart);
}

protected function renderChartCommon(string &$chartJS): void
Expand All @@ -162,7 +163,7 @@ protected function renderChartCommon(string &$chartJS): void
protected function renderChartEnd(string &$chartJS, string $engine): void
{
// trim last trailing comma and close parenthesis
$chartJS = \rtrim($chartJS, ",\n") . "\n });\n";
$chartJS = \rtrim($chartJS, self::END_LINE) . "\n });\n";
if ('' !== $engine) {
$chartJS .= "});\n";
}
Expand All @@ -180,13 +181,7 @@ protected function renderChartStart(string &$chartJS, string $engine): void

protected function renderColors(): string
{
if ([] !== $this->colors) {
$encoded = \json_encode($this->colors);

return self::SPACE . "colors: $encoded,\n";
}

return '';
return $this->jsonEncode($this->colors, 'colors');
}

protected function renderCredits(): string
Expand All @@ -205,7 +200,7 @@ protected function renderEngine(string $engine): string

protected function renderExporting(): string
{
return $this->renderCallbackOption($this->exporting);
return $this->renderCallback($this->exporting);
}

protected function renderGlobal(): string
Expand All @@ -220,25 +215,25 @@ protected function renderLang(): string

protected function renderLegend(): string
{
return $this->renderCallbackOption($this->legend);
return $this->renderCallback($this->legend);
}

protected function renderOptions(): string
{
$result = '';
if ($this->global->hasData() || $this->lang->hasData()) {
$result .= "\n Highcharts.setOptions({\n";
$result .= $this->renderGlobal();
$result .= $this->renderLang();
$result .= " });\n";
if (!$this->global->hasData() && !$this->lang->hasData()) {
return '';
}

return $result;
$result = "\n Highcharts.setOptions({\n";
$result .= $this->renderGlobal();
$result .= $this->renderLang();

return $result . " });\n";
}

protected function renderPlotOptions(): string
{
return $this->renderCallbackOption($this->plotOptions);
return $this->renderCallback($this->plotOptions);
}

protected function renderScrollbar(): string
Expand All @@ -248,7 +243,7 @@ protected function renderScrollbar(): string

protected function renderSeries(): string
{
return $this->renderCallbackArray($this->series, 'series');
return $this->renderCallback($this->series, 'series');
}

protected function renderSubtitle(): string
Expand All @@ -263,24 +258,16 @@ protected function renderTitle(): string

protected function renderTooltip(): string
{
return $this->renderCallbackOption($this->tooltip);
return $this->renderCallback($this->tooltip);
}

protected function renderXAxis(): string
{
if ($this->xAxis instanceof ChartOption) {
return $this->renderCallbackOption($this->xAxis);
}

return $this->renderCallbackArray($this->xAxis, 'xAxis');
return $this->renderCallback($this->xAxis, 'xAxis');
}

protected function renderYAxis(): string
{
if ($this->yAxis instanceof ChartOption) {
return $this->renderCallbackOption($this->yAxis);
}

return $this->renderCallbackArray($this->yAxis, 'yAxis');
return $this->renderCallback($this->yAxis, 'yAxis');
}
}
12 changes: 3 additions & 9 deletions src/Highcharts/Highchart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
namespace Ob\HighchartsBundle\Highcharts;

/**
* This class is part of the Ob/HighchartsBundle.
* Highchart chart.
*
* See Highcharts documentation at http://www.highcharts.com/ref/.
*
* @psalm-suppress PropertyNotSetInConstructor
*/
class Highchart extends AbstractChart
{
Expand Down Expand Up @@ -44,11 +42,7 @@ protected function renderChartStart(string &$chartJS, string $engine): void

private function renderColorAxis(): string
{
if ($this->colorAxis instanceof ChartOption) {
return $this->renderCallbackOption($this->colorAxis);
}

return $this->renderCallbackArray($this->colorAxis, 'colorAxis');
return $this->renderCallback($this->colorAxis, 'colorAxis');
}

private function renderDrilldown(): string
Expand All @@ -58,7 +52,7 @@ private function renderDrilldown(): string

private function renderNoData(): string
{
return $this->renderCallbackOption($this->noData);
return $this->renderCallback($this->noData);
}

private function renderPane(): string
Expand Down
6 changes: 2 additions & 4 deletions src/Highcharts/Highstock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
namespace Ob\HighchartsBundle\Highcharts;

/**
* This class is part of the Ob/HighchartsBundle.
* Highstock chart.
*
* See Highcharts documentation at http://www.highcharts.com/ref/.
*
* @psalm-suppress PropertyNotSetInConstructor
*/
class Highstock extends AbstractChart
{
Expand All @@ -35,6 +33,6 @@ protected function renderChartStart(string &$chartJS, string $engine): void

protected function renderRangeSelector(): string
{
return $this->renderCallbackOption($this->rangeSelector);
return $this->renderCallback($this->rangeSelector);
}
}
9 changes: 6 additions & 3 deletions tests/Highcharts/ScrollbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
*/
class ScrollbarTest extends TestCase
{
/** @psalm-var array<string, mixed> */
private array $scrollbar = [];

/*
* @var array
*/
/* @psalm-var array<string, mixed> */
private array $usedOptions = [];

/**
Expand All @@ -43,10 +42,13 @@ protected function setUp(): void

/**
* Scrollbar config output.
*
* @throws \Exception
*/
public function testConfig(): void
{
$chart = new Highchart();
/** @psalm-var mixed $value */
foreach ($this->scrollbar as $key => $value) {
// Config randomization
if (0 === \random_int(0, 5)) {
Expand All @@ -57,6 +59,7 @@ public function testConfig(): void
}
$result = $chart->render();
\preg_match('|scrollbar: (\{[^\}]+\})+|', $result, $matches);
/** @psalm-var array $options */
$options = \json_decode($matches[1], true);
$this->assertCount(\count($this->usedOptions), \array_intersect($this->usedOptions, $options));
}
Expand Down

0 comments on commit 595a5d5

Please sign in to comment.