Introduce registerUndefinedTestCallback - #4687
Conversation
fabpot
left a comment
There was a problem hiding this comment.
Can you add a note in CHANGELOG?
|
How does this behave regarding the fact that test names can be either 1 identifier or 2 identifiers ? Will this call the undefined test callback for the first identifier before trying to find a known test named in 2 words ? And doesn't this require a special handling for guard nodes for the case where the callback is throwing its own SyntaxError to provide a custom error message ? |
Yes, the same way Currently, if I define a Here, when implementing registerUndefinedTestCallback, I'll have to return false for
I copied the implementation of registerUndefinedTokenParserCallback, registerUndefinedFunctionCallback and registerUndefinedFilterCallback ; why would I need a special handling here ? |
The difference is that currently, there is nothing special being run when the first identifier does not correspond to an undefined test. The fact that the parser checks 2 different test names is not an observable behavior currently.
Because those undefined callbacks also required special handling to account for the case where the callback is throwing. See #4575 where this was implemented to fix #4505 |
This makes it possible for an undefined test callback to break core tests. This looks very fragile to me, and is not something I would like to see being shipped in Twig (it will be a maintenance nightmare, especially to preserve BC on it). |
b9ebb03 to
c849b16
Compare
After a new look @stof, this issue doesn't exist since the Twig Parser is doing the right check (2 word or 1 word) based on the number of name token. Lines 493 to 502 in 98b664a So you can mock the tests without expecting issue with one/two words tests. I tried on my project. BTW, I think tests are used in the format
Should be updated |
|
Indeed, I used the wrong example when using
would be great to add tests covering this to prevent regressions (add a undefined test handler doing something for |
|
And I suggest also adding a test guarding a 2-word test name to ensure it works. |
GuardTokenParser didn't have support for 2-word test in the first place. I had to add it. All tests are added. |
ae09931 to
28e6f52
Compare
| # 3.22.0 (2025-XX-XX) | ||
|
|
||
| * Add support for two words test in guard tag | ||
| * Add `Environment::registerUndefinedTestCallback()` method. |
There was a problem hiding this comment.
| * Add `Environment::registerUndefinedTestCallback()` method. | |
| * Add `Environment::registerUndefinedTestCallback()` |
fabpot
left a comment
There was a problem hiding this comment.
I think you also need to update the docs for that new feature.
ed43b27 to
64a7fea
Compare
Indeed, I updated the doc |
64a7fea to
cc12df9
Compare
|
Thank you @VincentLanglet. |
This is similar to
registerUndefinedFilterCallbackandregisterUndefinedFunctionCallback