From 48cb6a41115452621e7a9dc420c24fa48156b475 Mon Sep 17 00:00:00 2001 From: marco76tv Date: Tue, 20 Aug 2024 18:05:54 +0200 Subject: [PATCH] style(AnswersChartData.php): improve code readability by using backslashes for global functions like in_array and is_array refactor(AnswersChartData.php): simplify condition checks by using strict comparison operators and rearrange code for better clarity perf(AnswersChartData.php): optimize code by reducing unnecessary function calls and improving variable naming for better understanding --- Datas/AnswersChartData.php | 65 +++++++++++++++----------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/Datas/AnswersChartData.php b/Datas/AnswersChartData.php index 53850c83..1a07e732 100755 --- a/Datas/AnswersChartData.php +++ b/Datas/AnswersChartData.php @@ -67,7 +67,7 @@ public function getChartJsData(): array // dddx($this->answers->toCollection()); // } - if (in_array($this->chart->type, ['pieAvg', 'pie1'], false)) { + if (\in_array($this->chart->type, ['pieAvg', 'pie1'], false)) { $data = $this->answers->toCollection()->pluck('avg')->all(); if (isset($this->chart->max)) { @@ -76,14 +76,14 @@ public function getChartJsData(): array if ($other > 0.01) { $data[] = $other; $labels[] = $this->chart->answer_value_no_txt ?? 'answer_value_no_txt'; - if (\count($labels) === 2 && \strlen($labels[0]) < 3) { + if (2 === \count($labels) && \strlen($labels[0]) < 3) { $labels[0] = $this->chart->answer_value_txt; } } } } - if (isset($data[0]) && is_array($data[0])) { // questionario multiplo + if (isset($data[0]) && \is_array($data[0])) { // questionario multiplo // dddx([$this->chart, $this->answers]); $legends = array_keys($data[0]); foreach ($legends as $key => $legend) { @@ -108,13 +108,13 @@ public function getChartJsData(): array $data[] = $other; $labels[] = $this->chart->answer_value_no_txt ?? 'answer_value_no_txt'; Assert::notNull($labels[0], '['.__FILE__.']['.__LINE__.']'); - if (\count($labels) === 2 && \strlen($labels[0]) < 3) { + if (2 === \count($labels) && \strlen($labels[0]) < 3) { $labels[0] = $this->chart->answer_value_txt; } } } - if (isset($this->answers->toCollection()->pluck('avg')[0]) && ! is_string($this->answers->toCollection()->pluck('avg')[0])) { + if (isset($this->answers->toCollection()->pluck('avg')[0]) && ! \is_string($this->answers->toCollection()->pluck('avg')[0])) { $label = 'Media'; } else { $label = 'Percentuale'; @@ -148,7 +148,7 @@ public function getChartJsOptionsArray(): array { $title = []; - if ($this->title !== 'no_set') { + if ('no_set' !== $this->title) { $title = [ 'display' => true, 'text' => $this->title, @@ -158,7 +158,7 @@ public function getChartJsOptionsArray(): array ]; } - if ($this->footer !== 'no_set') { + if ('no_set' !== $this->footer) { $title = [ 'display' => true, 'text' => $this->footer, @@ -170,7 +170,7 @@ public function getChartJsOptionsArray(): array 'title' => $title, ]; - if ($this->chart->type === 'horizbar1') { + if ('horizbar1' === $this->chart->type) { $options['indexAxis'] = 'y'; } @@ -186,10 +186,10 @@ public function getChartJsBarOptionsJs(string $js): string $indexAxis = 'x'; $value = ''; - if ($this->chart->max === 100.0) { + if (100.0 === $this->chart->max) { $value = ' %'; } - if ($this->chart->type === 'horizbar1') { + if ('horizbar1' === $this->chart->type) { $indexAxis = 'y'; $value = ' %'; } @@ -197,7 +197,7 @@ public function getChartJsBarOptionsJs(string $js): string $title = '{}'; $labels = '{}'; - if (count($this->getChartJsData()['datasets']) == 1 && $this->chart->type !== 'horizbar1') { + if (1 === \count($this->getChartJsData()['datasets']) && 'horizbar1' !== $this->chart->type) { $labels = "{ name: { align: 'center', @@ -225,7 +225,7 @@ public function getChartJsBarOptionsJs(string $js): string $title = '{}'; // dddx($this); - if ($this->title !== 'no_set' && $this->chart->type === 'horizbar1') { + if ('no_set' !== $this->title && 'horizbar1' === $this->chart->type) { $title = "{ display: true, text: '".$this->title."', @@ -235,7 +235,7 @@ public function getChartJsBarOptionsJs(string $js): string }"; } - if ($this->footer !== 'no_set') { + if ('no_set' !== $this->footer) { $title = "{ display: true, text: '".$this->footer."', @@ -243,7 +243,7 @@ public function getChartJsBarOptionsJs(string $js): string }"; } $tooltip = '{}'; - if ($this->chart->type === 'bar2' && count($this->getChartJsData()['datasets']) == 1) { + if ('bar2' === $this->chart->type && 1 === \count($this->getChartJsData()['datasets'])) { $tooltip = "{ callbacks: { label: function(context) { @@ -386,7 +386,7 @@ public function getChartJsBarOptionsJs(string $js): string public function getChartJsDoughnutOptionsJs(string $js): string { $title = '{}'; - if ($this->title !== 'no_set') { + if ('no_set' !== $this->title) { $title = "{ display: true, text: '".$this->title."', @@ -397,9 +397,13 @@ public function getChartJsDoughnutOptionsJs(string $js): string } $first_answer = $this->answers->first(); $label = '--'; +<<<<<<< HEAD if ($first_answer != null) { +======= + if (null !== $first_answer) { +>>>>>>> ff702a8 (up) Assert::isInstanceOf($first_answer, AnswerData::class, '['.__LINE__.']['.__FILE__.']'); - $label = round(floatval($this->answers->first()->avg), 2); + $label = round((float) $this->answers->first()->avg, 2); } $js = <<answers->first(), AnswerData::class, '['.__LINE__.']['.__FILE__.']'); $options['plugins']['doughnutLabel'] = [ - 'label' => round(floatval($this->answers->first()->avg), 2), + 'label' => round((float) $this->answers->first()->avg, 2), ]; return $options; @@ -490,28 +494,11 @@ public function getChartJsOptionsJs(): RawJs $method = 'getChartJs'.Str::of($chartjs_type)->studly()->toString().'OptionsJs'; $js = $this->{$method}($js); - // dddx( - // RawJs::make('{ - // '.$js.' - // }') - // ); + // return '{'.$js.'}'; return RawJs::make('{ '.$js.' }'); - /* - return RawJs::make(<< '€' + value, - }, - }, - }, - } - JS); - */ } // funzione deprecata, utilizzata nella dashboard precedente @@ -519,7 +506,7 @@ public function getChartJsOptions(): array { $title = []; - if ($this->title !== 'no_set') { + if ('no_set' !== $this->title) { $title = [ 'display' => true, 'text' => $this->title, @@ -529,7 +516,7 @@ public function getChartJsOptions(): array ]; } - if ($this->footer !== 'no_set') { + if ('no_set' !== $this->footer) { $title = [ 'display' => true, 'text' => $this->footer, @@ -541,7 +528,7 @@ public function getChartJsOptions(): array 'title' => $title, ]; - if ($this->chart->type === 'horizbar1') { + if ('horizbar1' === $this->chart->type) { $options['indexAxis'] = 'y'; } @@ -551,4 +538,4 @@ public function getChartJsOptions(): array return $options; } -} +} \ No newline at end of file