Skip to content

🦺 check for form in filter#28

Open
garak wants to merge 1 commit intomainfrom
add-check
Open

🦺 check for form in filter#28
garak wants to merge 1 commit intomainfrom
add-check

Conversation

@garak
Copy link
Copy Markdown
Member

@garak garak commented Apr 2, 2026

Fix #26

Copilot AI review requested due to automatic review settings April 2, 2026 07:21
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses Issue #26 by adding an explicit guard in Filter::filter() to prevent calling filter() before saveFilter(), aiming to avoid notices/fatals and make debugging easier.

Changes:

  • Add a missing-filter guard in Filter::filter() that throws an exception when no saved form exists.
  • Add a PHPUnit test covering calling filter() without saveFilter().
  • Update CI to use ramsey/composer-install@v4.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/Filter.php Adds an exception guard in filter() and updates constructor property declarations.
tests/FilterTest.php Adds a new test for calling filter() without saveFilter() (plus a new import).
.github/workflows/build.yaml Updates the Composer install GitHub Action major version.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


namespace PUGX\FilterBundle\Tests;

use Filter\FilterException;
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

The added import use Filter\FilterException; is unused and appears to reference a non-existent namespace for this project. With the Symfony PHP-CS-Fixer rules, unused imports typically fail CI; remove this import (or replace it with the actual exception class you intend to assert).

Suggested change
use Filter\FilterException;

Copilot uses AI. Check for mistakes.
Comment on lines +33 to +37
public function testFilterWithoutSaveFilterShouldThrowException(): void
{
$this->expectException(\UnexpectedValueException::class);
$this->filter->filter('foo');
}
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

This new test expects an exception even though no filter data is present in the session. The original bug occurs when the session contains saved filter values but saveFilter() was not called for the current request (so the form is missing). Consider priming the session with filter.foo data in the test so it matches the real failure mode, and assert the exception message as well.

Copilot uses AI. Check for mistakes.
Comment on lines +35 to +36
$this->expectException(\UnexpectedValueException::class);
$this->filter->filter('foo');
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

There is an extra leading space before $this->filter->filter('foo'); which will likely be flagged by PHP-CS-Fixer (Symfony rules). Align indentation with the surrounding code.

Copilot uses AI. Check for mistakes.
Comment on lines 35 to 40
$fname = $name.$this->getSession()->getId();
if (!isset($this->forms[$fname])) {
throw new \UnexpectedValueException(\sprintf('No filter found for "%s". Did you call saveFilter()?', $name));
}
/** @var array<string, mixed>|null $values */
$values = $this->getSession()->get('filter.'.$name);
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

The new guard throws whenever saveFilter() hasn't been called, even if there are no saved filter values and filter() would only be used to retrieve _sort. This is a behavioral change vs. the previous implementation and will break flows like calling sort() then filter() (no form needed). Consider throwing only when session has non-null filter.$name values (or when the form is actually required), so sort-only usage and empty-filter usage keep working while still catching the real misuse case.

Suggested change
$fname = $name.$this->getSession()->getId();
if (!isset($this->forms[$fname])) {
throw new \UnexpectedValueException(\sprintf('No filter found for "%s". Did you call saveFilter()?', $name));
}
/** @var array<string, mixed>|null $values */
$values = $this->getSession()->get('filter.'.$name);
$session = $this->getSession();
$fname = $name.$session->getId();
/** @var array<string, mixed>|null $values */
$values = $session->get('filter.'.$name);
if (!isset($this->forms[$fname])) {
if (null === $values) {
if ([] !== ($sort = $this->getSort($name))) {
$filter['_sort'] = $sort;
}
return $filter;
}
throw new \UnexpectedValueException(\sprintf('No filter found for "%s". Did you call saveFilter()?', $name));
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Warn use if filter method is called without saveFilter

2 participants