laminas-filter’s FilterInterface’s filter() has
@throws Exception\RuntimeException If filtering $value is impossible.
but almost no filters throw exceptions.
In reality, most filters seem to follow the approach if we can't filter it, return the value that we were provided
.
One example is ToFloat which says
If the value provided is non-scalar, the value will remain unfiltered
From the code for how most filters are written, it seems filters should be implemented so they don’t throw errors.
Do you have an explicit question or is it just a statement?
Filters should not throw exceptions so that subsequent validators can do their job and give appropriate messages to the user.
A new major version for laminas-filter is in work and any help is welcome!
Sorry, my question was essentially “should we actually throw exceptions?”
Filters should not throw exceptions so that subsequent validators can do their job and give appropriate messages to the user.
But this gives me my answer, thanks!
I think removing the @throws in the interface, and adding what I quoted from you above would be a good improvement.
In our project, we replaced laminas-filter/src/DateTimeFormatter.php at 2.40.x · laminas/laminas-filter · GitHub to not throw an error.
I might have some time, I’ll take a look at the new version and see if there’s anything I might be able to help with.
1 Like