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

Apply filter even if the operator is not provided #6591

Merged
merged 1 commit into from
Nov 15, 2020

Conversation

VincentLanglet
Copy link
Member

Subject

Bug reported #6590

I am targeting this branch, because bug fix.

Changelog

### Fixed
- Apply filter even if the operator is not provided.

('' !== $this->values[$filterFormName]['type'] || '' !== $this->values[$filterFormName]['value'])
) {

$hasValue = isset($this->values[$filterFormName]['value'])
Copy link
Member

@core23 core23 Nov 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be extract some methods here?

Copy link
Member Author

@VincentLanglet VincentLanglet Nov 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO there is too much private method here.
Testing applyFilter wasn't easy.

Creating one method for a one line check seems overkill.
I already use one time variable for improving the readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO there is too much private method here.

That's an other problem of the class. The class / method does too much stuff.

Creating one method for a one line check seems overkill.

Not, if you look at the NPath complexity here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not, if you look at the NPath complexity here...

I don't see why it would improve the complexity.
What method do you want to extract ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @core23 probably means to have a hasType and hasValue method, or just one method with an extra 'value'|'type' parameter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but it's basically a method

private function hasValue($key1, $key2)
{
    return isset($this->values[$key1][$key2]) && '' !== $this->values[$key1][$key2];
}

used this way

if ($this->hasValue($filterFormName, 'value') || $this->hasValue($filterFormName, 'type')) {
     $filter->apply($this->query, $data[$filterFormName]);
}

I don't understand how it improved the complexity. I just feel like it's loosing context.

I changed the code for

$value = $this->values[$filterFormName]['value'] ?? '';
$type = $this->values[$filterFormName]['type'] ?? '';

if ('' !== $value || '' !== $type) {
    $filter->apply($this->query, $data[$filterFormName]);
}

Isn't it better ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good enough to me 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My 2 cents, maybe giving some semantic to the method, something like:

foreach ($this->getFilters() as $name => $filter) {
    $this->values[$name] = $this->values[$name] ?? null;

    if ($this->canFilterBeApplied($filter, $this->values)) {
        $filter->apply($this->query, $data[$filterFormName]);
    }
}

Maybe there should be a method in FilterInterface to check if a filter can be applied or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more concern about the line

$this->values[$name] = $this->values[$name] ?? null;

Which I don't understand.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the purpose of this PR is to fix a bug, so I would avoid to introduce a new method in the interface or to remove a line I don't understand here, in order to quickly release a 3.80.1 version.

tests/Datagrid/DatagridTest.php Outdated Show resolved Hide resolved
tests/Datagrid/DatagridTest.php Outdated Show resolved Hide resolved
@VincentLanglet VincentLanglet merged commit 7930543 into sonata-project:3.x Nov 15, 2020
@VincentLanglet VincentLanglet deleted the fixApplyFilter branch November 15, 2020 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants