From fddf029066a10ee041801dfb3ccf7180e0301366 Mon Sep 17 00:00:00 2001 From: Simon Praetorius Date: Sun, 2 Feb 2025 18:16:13 +0100 Subject: [PATCH] [BUGFIX] Consistent return types for condition-based ViewHelpers In most cases, condition-based ViewHelpers already return an empty string as default value, for example if the verdict is false and else is not set. However, there are a few cases where `null` would be returned instead, which would in the template implicitly converted to an empty string in the vast majority of cases anyway. This change makes sure that all closures or direct ViewHelper node evaluations default to an empty string if `null` is returned, making the behavior consistent to other code paths where an empty string is returned explicitly. --- src/Core/ViewHelper/AbstractConditionViewHelper.php | 10 +++++----- .../ViewHelpers/IfThenElseViewHelperTest.php | 5 ++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Core/ViewHelper/AbstractConditionViewHelper.php b/src/Core/ViewHelper/AbstractConditionViewHelper.php index fabe33f8..9e25645c 100644 --- a/src/Core/ViewHelper/AbstractConditionViewHelper.php +++ b/src/Core/ViewHelper/AbstractConditionViewHelper.php @@ -97,7 +97,7 @@ protected function renderThenChild() // Closure might be present if ViewHelper is called from a cached template if ($this->thenClosure !== null) { - return ($this->thenClosure)(); + return ($this->thenClosure)() ?? ''; } // The following code can only be evaluated for uncached templates where the node structure @@ -123,7 +123,7 @@ protected function renderThenChild() if ($elseViewHelperEncountered) { return ''; } - return $this->renderChildren(); + return $this->renderChildren() ?? ''; } /** @@ -142,7 +142,7 @@ protected function renderElseChild() // the "body" closure if condition is met foreach ($this->elseIfClosures as $elseIf) { if ($elseIf['condition']()) { - return $elseIf['body'](); + return $elseIf['body']() ?? ''; } } } @@ -156,7 +156,7 @@ protected function renderElseChild() // Closure might be present if ViewHelper is called from a cached template if ($this->elseClosure !== null) { - return ($this->elseClosure)(); + return ($this->elseClosure)() ?? ''; } // The following code can only be evaluated for uncached templates where the node structure @@ -188,7 +188,7 @@ protected function renderElseChild() return $this->arguments['else']; } - return $elseNode instanceof ViewHelperNode ? $elseNode->evaluate($this->renderingContext) : ''; + return $elseNode instanceof ViewHelperNode ? $elseNode->evaluate($this->renderingContext) ?? '' : ''; } /** diff --git a/tests/Functional/ViewHelpers/IfThenElseViewHelperTest.php b/tests/Functional/ViewHelpers/IfThenElseViewHelperTest.php index bd2c9313..512070b7 100644 --- a/tests/Functional/ViewHelpers/IfThenElseViewHelperTest.php +++ b/tests/Functional/ViewHelpers/IfThenElseViewHelperTest.php @@ -54,7 +54,7 @@ public static function renderDataProvider(): \Generator yield 'else argument, verdict true' => [ '', ['verdict' => true], - null, + '', ]; yield 'else argument, verdict false' => [ '', @@ -396,8 +396,7 @@ public static function renderDataProvider(): \Generator yield 'inline syntax, else argument, verdict true' => [ '{f:if(condition:\'{verdict}\', else:\'elseArgument\')}', ['verdict' => true], - // @todo: wtf? - null, + '', ]; yield 'inline syntax, else argument, verdict false' => [ '{f:if(condition:\'{verdict}\', else:\'elseArgument\')}',