Skip to content

Commit

Permalink
[BUGFIX] Fix f:if inline syntax case (#803)
Browse files Browse the repository at this point in the history
Good: {f:if(condition:'{myVariable}',then: '1', else:'0')}
Bad:  {f:if(condition:'{myVariable}',then: 1, else:0)}

This is a regression of 2.9.0. The patch adds coverage
and fixes the case.
  • Loading branch information
lolli42 authored Jun 8, 2023
1 parent 203c14f commit a502df3
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ parameters:
count: 1
path: src/Core/Rendering/RenderingContext.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: src/Core/ViewHelper/AbstractConditionViewHelper.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
Expand Down
10 changes: 9 additions & 1 deletion src/Core/ViewHelper/AbstractConditionViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,22 @@ final public function convert(TemplateCompiler $templateCompiler): array
is_array($defaultValue) && empty($defaultValue) ? '[]' : var_export($defaultValue, true)
);
} elseif ($arguments[$argumentName] instanceof NodeInterface) {
// Argument *is* given to VH, resolve
// Argument *is* given to VH and is a node, resolve
$converted = $arguments[$argumentName]->convert($templateCompiler);
$accumulatedArgumentInitializationCode .= $converted['initialization'];
$argumentInitializationCode .= sprintf(
'\'%s\' => %s,' . chr(10),
$argumentName,
$converted['execution']
);
} else {
// Argument *is* given to VH and is a simple type.
// @todo: Why is this not a node object as well? See f:if inline syntax tests.
$argumentInitializationCode .= sprintf(
'\'%s\' => %s,' . chr(10),
$argumentName,
$arguments[$argumentName]
);
}
}

Expand Down
136 changes: 136 additions & 0 deletions tests/Functional/ViewHelpers/IfThenElseViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ public static function renderDataProvider(): \Generator
'',
];

yield 'then variable argument, else variable argument, verdict true' => [
'<f:if condition="{verdict}" then="{thenVariable}" else="{elseVariable}" />',
['verdict' => true, 'thenVariable' => 'thenArgument', 'elseVariable' => 'elseArgument'],
'thenArgument',
];
yield 'then variable argument, else variable argument, verdict false' => [
'<f:if condition="{verdict}" then="{thenVariable}" else="{elseVariable}" />',
['verdict' => false, 'thenVariable' => 'thenArgument', 'elseVariable' => 'elseArgument'],
'elseArgument',
];
yield 'then variable argument missing, else variable argument, verdict true' => [
'<f:if condition="{verdict}" then="{thenVariable}" else="{elseVariable}" />',
['verdict' => true, 'elseVariable' => 'elseArgument'],
'',
];
yield 'then variable argument missing, else variable argument, verdict false' => [
'<f:if condition="{verdict}" then="{thenVariable}" else="{elseVariable}" />',
['verdict' => false, 'elseVariable' => 'elseArgument'],
'elseArgument',
];
yield 'then variable argument, else variable missing, verdict true' => [
'<f:if condition="{verdict}" then="{thenVariable}" else="{elseVariable}" />',
['verdict' => true, 'thenVariable' => 'thenArgument'],
'thenArgument',
];
yield 'then variable argument, else variable missing, verdict false' => [
'<f:if condition="{verdict}" then="{thenVariable}" else="{elseVariable}" />',
['verdict' => false, 'thenVariable' => 'thenArgument'],
'',
];

/*
* @todo: broken non-compiled, ok compiled
yield 'else argument, else if child, if verdict false, elseif verdict true' => [
Expand All @@ -352,6 +383,111 @@ public static function renderDataProvider(): \Generator
'elseIfChild',
];
*/

yield 'inline syntax, then argument, verdict true' => [
'{f:if(condition:\'{verdict}\', then:\'thenArgument\')}',
['verdict' => true],
'thenArgument',
];
yield 'inline syntax, then argument, verdict false' => [
'{f:if(condition:\'{verdict}\', then:\'thenArgument\')}',
['verdict' => false],
'',
];

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\')}',
['verdict' => false],
'elseArgument',
];

yield 'inline syntax, then argument, else argument, verdict true' => [
'{f:if(condition:\'{verdict}\', then:\'thenArgument\', else:\'elseArgument\')}',
['verdict' => true],
'thenArgument',
];
yield 'inline syntax, then argument, else argument, verdict false' => [
'{f:if(condition:\'{verdict}\', then:\'thenArgument\', else:\'elseArgument\')}',
['verdict' => false],
'elseArgument',
];

yield 'inline syntax, then argument, else argument, no verdict' => [
'{f:if(then:\'thenArgument\', else:\'elseArgument\')}',
[],
'elseArgument',
];

yield 'inline syntax, then argument, else argument, verdict true, verdict variable node' => [
'{f:if(condition:verdict, then:\'thenArgument\', else:\'elseArgument\')}',
['verdict' => true],
'thenArgument',
];
yield 'inline syntax, then argument, else argument, verdict false, verdict variable node' => [
'{f:if(condition:verdict, then:\'thenArgument\', else:\'elseArgument\')}',
['verdict' => false],
'elseArgument',
];

yield 'inline syntax, then argument, else argument, verdict variable missing' => [
'{f:if(condition:\'{verdict}\', then:\'thenArgument\', else:\'elseArgument\')}',
[],
'elseArgument',
];
yield 'inline syntax, then argument, else argument, verdict variable missing, verdict variable node' => [
'{f:if(condition:verdict, then:\'thenArgument\', else:\'elseArgument\')}',
[],
'elseArgument',
];

yield 'inline syntax, then argument variable, else argument variable, verdict true' => [
'{f:if(condition:\'{verdict}\', then:\'{thenVariable}\', else:\'{elseVariable}\')}',
['verdict' => true, 'thenVariable' => 'thenArgument', 'elseVariable' => 'elseArgument'],
'thenArgument',
];
yield 'inline syntax, then argument variable, else argument variable, verdict false' => [
'{f:if(condition:\'{verdict}\', then:\'{thenVariable}\', else:\'{elseVariable}\')}',
['verdict' => false, 'thenVariable' => 'thenArgument', 'elseVariable' => 'elseArgument'],
'elseArgument',
];

yield 'inline syntax, then argument variable, else argument variable node, verdict true' => [
'{f:if(condition:\'{verdict}\', then:thenVariable, else:elseVariable)}',
['verdict' => true, 'thenVariable' => 'thenArgument', 'elseVariable' => 'elseArgument'],
'thenArgument',
];
yield 'inline syntax, then argument variable, else argument variable node, verdict false' => [
'{f:if(condition:\'{verdict}\', then:thenVariable, else:elseVariable)}',
['verdict' => false, 'thenVariable' => 'thenArgument', 'elseVariable' => 'elseArgument'],
'elseArgument',
];

yield 'inline syntax, then argument string 1, else argument string 0, verdict true' => [
'{f:if(condition:\'{verdict}\', then:\'1\', else:\'0\')}',
['verdict' => true],
1,
];
yield 'inline syntax, then argument string 1, else argument string 0, verdict false' => [
'{f:if(condition:\'{verdict}\', then:\'1\', else:\'0\')}',
['verdict' => false],
0,
];
yield 'inline syntax, then argument int 1, else argument int 0, verdict true' => [
'{f:if(condition:\'{verdict}\', then:1, else:0)}',
['verdict' => true],
1,
];
yield 'inline syntax, then argument int 1, else argument int 0, verdict false' => [
'{f:if(condition:\'{verdict}\', then:1, else:0)}',
['verdict' => false],
0,
];
}

/**
Expand Down

0 comments on commit a502df3

Please sign in to comment.