From 1564c3bbbce99cc9d58f0b578b3e5776927c3757 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 22 Mar 2024 11:23:45 +0100 Subject: [PATCH 1/2] 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 71d08b131..03055a9ef 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 @@ public function isHtml(): bool 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 @@ public function render() $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 06d60bdcb..3bcc153a0 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 98c0c79c8..b472980a2 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 960709e7e..a2b761a0d 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 From 6e69867758ef6a195427dbdca2255e8cdf7b58a9 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 22 Mar 2024 11:24:08 +0100 Subject: [PATCH 2/2] ObjectDetail: Increase output length limit to 10000 --- library/Icingadb/Widget/Detail/ObjectDetail.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/library/Icingadb/Widget/Detail/ObjectDetail.php b/library/Icingadb/Widget/Detail/ObjectDetail.php index a6881732d..4ba0e6984 100644 --- a/library/Icingadb/Widget/Detail/ObjectDetail.php +++ b/library/Icingadb/Widget/Detail/ObjectDetail.php @@ -423,7 +423,10 @@ protected function createPluginOutput(): array if (empty($this->object->state->output) && empty($this->object->state->long_output)) { $pluginOutput = new EmptyState(t('Output unavailable.')); } else { - $pluginOutput = new PluginOutputContainer(PluginOutput::fromObject($this->object)); + $pluginOutput = new PluginOutputContainer( + PluginOutput::fromObject($this->object) + ->setCharacterLimit(10000) + ); CopyToClipboard::attachTo($pluginOutput); }