From 1564c3bbbce99cc9d58f0b578b3e5776927c3757 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 22 Mar 2024 11:23:45 +0100 Subject: [PATCH] PluginOutput: Limit length to 1000 by default --- library/Icingadb/Util/PluginOutput.php | 21 +++++++++++++++++++++ phpstan-baseline-7x.neon | 10 ++++++++++ phpstan-baseline-8x.neon | 10 ++++++++++ phpstan-baseline-standard.neon | 12 +----------- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/library/Icingadb/Util/PluginOutput.php b/library/Icingadb/Util/PluginOutput.php index 71d08b13..03055a9e 100644 --- a/library/Icingadb/Util/PluginOutput.php +++ b/library/Icingadb/Util/PluginOutput.php @@ -64,6 +64,9 @@ class PluginOutput extends HtmlString /** @var bool Whether the output contains HTML */ protected $isHtml; + /** @var int The maximum amount of characters to process */ + protected $characterLimit = 1000; + /** @var bool Whether output will be enriched */ protected $enrichOutput = true; @@ -93,6 +96,20 @@ class PluginOutput extends HtmlString return $this->isHtml; } + /** + * Set the maximum amount of characters to process + * + * @param int $limit + * + * @return $this + */ + public function setCharacterLimit(int $limit): self + { + $this->characterLimit = $limit; + + return $this; + } + /** * Set whether the output should be enriched * @@ -185,6 +202,10 @@ class PluginOutput extends HtmlString $output = $this->processHtml($output); } + if ($this->characterLimit) { + $output = substr($output, 0, $this->characterLimit); + } + $this->renderedOutput = $output; return $output; diff --git a/phpstan-baseline-7x.neon b/phpstan-baseline-7x.neon index 06d60bdc..3bcc153a 100644 --- a/phpstan-baseline-7x.neon +++ b/phpstan-baseline-7x.neon @@ -125,6 +125,11 @@ parameters: count: 1 path: library/Icingadb/Util/PerfData.php + - + message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Util\\\\PluginOutput\\:\\:render\\(\\) should return string but returns string\\|false\\|null\\.$#" + count: 1 + path: library/Icingadb/Util/PluginOutput.php + - message: "#^Parameter \\#1 \\$str of function trim expects string, string\\|null given\\.$#" count: 1 @@ -135,6 +140,11 @@ parameters: count: 1 path: library/Icingadb/Util/PluginOutput.php + - + message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Util\\\\PluginOutput\\:\\:\\$renderedOutput \\(string\\) does not accept string\\|false\\|null\\.$#" + count: 1 + path: library/Icingadb/Util/PluginOutput.php + - message: "#^Parameter \\#1 \\$str of function trim expects string, mixed given\\.$#" count: 1 diff --git a/phpstan-baseline-8x.neon b/phpstan-baseline-8x.neon index 98c0c79c..b472980a 100644 --- a/phpstan-baseline-8x.neon +++ b/phpstan-baseline-8x.neon @@ -125,11 +125,21 @@ parameters: count: 1 path: library/Icingadb/Util/PerfData.php + - + message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Util\\\\PluginOutput\\:\\:render\\(\\) should return string but returns string\\|null\\.$#" + count: 1 + path: library/Icingadb/Util/PluginOutput.php + - message: "#^Parameter \\#1 \\$string of function trim expects string, string\\|null given\\.$#" count: 1 path: library/Icingadb/Util/PluginOutput.php + - + message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Util\\\\PluginOutput\\:\\:\\$renderedOutput \\(string\\) does not accept string\\|null\\.$#" + count: 1 + path: library/Icingadb/Util/PluginOutput.php + - message: "#^Parameter \\#1 \\$string of function trim expects string, mixed given\\.$#" count: 1 diff --git a/phpstan-baseline-standard.neon b/phpstan-baseline-standard.neon index 960709e7..a2b761a0 100644 --- a/phpstan-baseline-standard.neon +++ b/phpstan-baseline-standard.neon @@ -5040,11 +5040,6 @@ parameters: count: 1 path: library/Icingadb/Util/PluginOutput.php - - - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Util\\\\PluginOutput\\:\\:render\\(\\) should return string but returns string\\|null\\.$#" - count: 1 - path: library/Icingadb/Util/PluginOutput.php - - message: "#^Parameter \\#1 \\$html of method Icinga\\\\Module\\\\Icingadb\\\\Util\\\\PluginOutput\\:\\:processHtml\\(\\) expects string, string\\|null given\\.$#" count: 1 @@ -5067,7 +5062,7 @@ parameters: - message: "#^Parameter \\#1 \\$string of function substr expects string, string\\|null given\\.$#" - count: 2 + count: 3 path: library/Icingadb/Util/PluginOutput.php - @@ -5075,11 +5070,6 @@ parameters: count: 1 path: library/Icingadb/Util/PluginOutput.php - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Util\\\\PluginOutput\\:\\:\\$renderedOutput \\(string\\) does not accept string\\|null\\.$#" - count: 1 - path: library/Icingadb/Util/PluginOutput.php - - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Web\\\\Control\\\\GridViewModeSwitcher\\:\\:getTitle\\(\\) should return string but returns string\\|null\\.$#" count: 1