Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Allow upgrading unquoted strings with interpolation #1

Open
jtojnar opened this issue May 31, 2022 · 2 comments
Open

Allow upgrading unquoted strings with interpolation #1

jtojnar opened this issue May 31, 2022 · 2 comments

Comments

@jtojnar
Copy link

jtojnar commented May 31, 2022

Upgrading to Latte 2.11, I get the following deprecation:

Expression 'show#mail$mail->id' should be wrapped in double quotes.

It would be nice if this could upgrade these as suggested by David:

<a n:href="show#mail$mail->id $mail->root->id, tree => true">Zobrazit ve stromu</a>
---
<a n:href='"show#mail{$mail->id}" $mail->root->id, tree => true'>Zobrazit ve stromu</a>

Similarly for a component with multiplier:

<p n:snippet="participator-$meeting->id">{control participator-$meeting->id}</p>
--
<p n:snippet='"participator-{$meeting->id}"'>{control "participator-{$meeting->id}"}</p>
@TomasVotruba
Copy link
Member

TomasVotruba commented May 31, 2022

Good idea 👍

At the moment, Latte 2.x does not support AST, so this is done with regex.
If you can come up with regex for this use case, implement LatteUpgraderInterface and process the file contents.

You can inspire here:
https://github.com/TomasVotruba/barista/blob/main/src/Upgrade/IfCurrentLatteUpgrader.php

@jtojnar
Copy link
Author

jtojnar commented Jun 1, 2022

Thinking about it, it probably be too hard – the variable string can be in any n:attribute as well as in any macro and filter, so we would first need to find all of those, and then tokenize them and run a state machine on that. So it might be easier to just run analyze.

But it turns out analyze is fine with the second example. In fact Latte is too, when compiling, only crashing at runtime with “Unsupported operand types: string - int” because it parsed the string as a subtraction.

I guess we could also fuzz templates using something like QuickCheck against types described by templateType class introduced in Latte 2.7. There is a PHP port but it does not look very active and would require some plumbing like creating generators for the template classes.

Edit: Thinking about it a bit more, templates can depend on extra macros which would require running the application for them to be registered, which could have side effects. Unless we use type inference to find the types of the used macros and helpers and then generate mocks for them. Or maybe the check could just require all functions to be declared in the class using the new attributes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants