-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
π‘ Node assert
β
#4231
base: 3.x
Are you sure you want to change the base?
π‘ Node assert
β
#4231
Conversation
(wip)
(wip)
Usage / syntax examples
(Table made with some gpt - for representational purposes only) |
$compiler->addDebugInfo($this); | ||
// TODO disable in production(?) | ||
|
||
$compiler | ||
->write(' if (!') | ||
->subcompile($this->getNode('expr')) | ||
->raw(") {\n") | ||
->indent() | ||
->write("throw new RuntimeError('Failed');\n") | ||
->outdent() | ||
->raw("}\n") | ||
; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using assert()
function?
With OPcache configuration, you can literally remove assert()
calls from the code... or here you planned to not write anything if production?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an idea too ! I would say a node would be easier to parse / use by node visitors but i may be wrong here π
or here you planned to not write anything if production?
I planned... nothing π the "idea" just emerged earlier tonight during the "var/type" discussion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The suggestion is about the compiled code, not about the Twig API
Actually, to be able to use PHP's assert
function which is a no-op at runtime in production mode, it needs to be a Twig tag and not Twig function usable in an expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π
When it comes to asserting variable types, that can be left over to extensions like TwigStan, once I do like the idea of using There's currently no way to intentionally throw an Exception of trigger a warning (aside from something silly like I'd propose that the
@ruudk is working on compiling Twig templates to PHP in such a way that PHPStan can work its magic. If the |
I'm curious to see how that would look like... but also if that's something we can include in the "default" Twig compilation (I suppose this is based on attributes/annotations with no runtime side-effects). |
You can read more about TwigStan here: #4233 |
Acting as a spinoff to #4165 Add tag to declare variable types
(as I don't want to disrupt the main conversation too much π )
The idea is to introduce assertions.
I'm opening this to gather feedback and ideas. I'd like to see if there's something interesting hereβor not. :)
This could enhance static analysis, define contracts in templates, and serve as a validator during includes/rendering.
Open to any feedback :)