Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redundant filter operators #229

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Filter/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function __construct(Filter\Rule $filter)
*
* @return $this
*/
public function setStrict($strict = true)
public function setStrict(bool $strict = true): self
{
$this->strict = (bool) $strict;
$this->strict = $strict;

return $this;
}
Expand All @@ -44,7 +44,7 @@ public function setStrict($strict = true)
*
* @return string
*/
public function render()
public function render(): string
{
if ($this->string !== null) {
return $this->string;
Expand All @@ -54,7 +54,9 @@ public function render()
$filter = $this->filter;

if ($filter instanceof Filter\Chain) {
$this->renderChain($filter, $this->strict);
if ($this->strict || ! $filter->isEmpty()) {
$this->renderChain($filter, $this->strict);
}
} else {
/** @var Filter\Condition $filter */
$this->renderCondition($filter);
Expand All @@ -71,12 +73,8 @@ public function render()
*
* @return void
*/
protected function renderChain(Filter\Chain $chain, $wrap = false)
protected function renderChain(Filter\Chain $chain, bool $wrap = false): void
{
if (! $this->strict && $chain->isEmpty()) {
return;
}

$chainOperator = null;
switch (true) {
case $chain instanceof Filter\All:
Expand Down Expand Up @@ -107,13 +105,15 @@ protected function renderChain(Filter\Chain $chain, $wrap = false)

foreach ($chain as $rule) {
if ($rule instanceof Filter\Chain) {
$this->renderChain($rule, $this->strict || $rule->count() > 1);
if ($this->strict || ! $rule->isEmpty()) {
$this->renderChain($rule, $this->strict || $rule->count() > 1);
$this->string .= $chainOperator;
}
} else {
/** @var Filter\Condition $rule */
$this->renderCondition($rule);
$this->string .= $chainOperator;
}

$this->string .= $chainOperator;
}

if (! $chain->isEmpty() && (! $this->strict || ! ($chain instanceof Filter\Any && $chain->count() === 1))) {
Expand All @@ -137,7 +137,7 @@ protected function renderChain(Filter\Chain $chain, $wrap = false)
*
* @return void
*/
protected function renderCondition(Filter\Condition $condition)
protected function renderCondition(Filter\Condition $condition): void
{
$value = $condition->getValue();
if (is_bool($value) && ! $value) {
Expand Down
82 changes: 82 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,86 @@ public function testParserRespectsRedundantCharsInStrictMode()
"Filter\Parser doesn't respect group operator for empty nested OR chains with non-empty siblings"
);
}

/* Non-Strict mode tests */

public function testRendererDoesNotDrawRedundantCharsInNonStrictMode()
{
$this->assertEquals(
'',
(new Renderer(Filter::all()))->render(),
"Filter\Renderer draws redundant parentheses for an empty root chain"
);
$this->assertEquals(
'foo=bar',
(new Renderer(Filter::equal('foo', 'bar')))->render(),
"Filter\Renderer draws redundant parentheses for a root chain with a single condition"
);
$this->assertEquals(
'foo=bar&bar=foo',
(new Renderer(Filter::all(
Filter::equal('foo', 'bar'),
Filter::equal('bar', 'foo')
)))->render(),
"Filter\Renderer draws redundant parentheses for a root chain with multiple conditions"
);
$this->assertEquals(
'foo=bar&bar=foo',
(new Renderer(Filter::all(
Filter::equal('foo', 'bar'),
Filter::all(
Filter::equal('bar', 'foo')
)
)))->render(),
"Filter\Renderer draws redundant parentheses for nested chains with a single condition"
);
$this->assertEquals(
'foo=bar&bar=foo',
(new Renderer(Filter::all(
Filter::equal('foo', 'bar'),
Filter::any(
Filter::equal('bar', 'foo')
)
)))->render(),
"Filter\Renderer draws redundant group operator for nested OR chains with a single condition"
);
$this->assertEquals(
'foo=bar',
(new Renderer(Filter::all(
Filter::equal('foo', 'bar'),
Filter::all()
)))->render(),
"Filter\Renderer draws redundant parentheses for empty nested chains"
);
$this->assertEquals(
'foo=bar&(bar=foo)',
(new Renderer(Filter::all(
Filter::equal('foo', 'bar'),
Filter::all(
Filter::all(),
Filter::equal('bar', 'foo')
)
)))->render(),
"Filter\Renderer draws redundant parentheses for empty nested chains with non-empty siblings"
);
$this->assertEquals(
'foo=bar',
(new Renderer(Filter::all(
Filter::equal('foo', 'bar'),
Filter::any()
)))->render(),
"Filter\Renderer draws redundant group operator for empty nested OR chains"
);
$this->assertEquals(
'foo=bar&(bar=foo)',
(new Renderer(Filter::all(
Filter::equal('foo', 'bar'),
Filter::any(
Filter::any(),
Filter::equal('bar', 'foo')
)
)))->render(),
"Filter\Renderer draws redundant group operator for empty nested OR chains with non-empty siblings"
);
}
}
Loading