Skip to content
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

Propagate type information in compiled template code to allow static analysis #262

Open
MartinMystikJonas opened this issue Feb 18, 2021 · 13 comments

Comments

@MartinMystikJonas
Copy link

MartinMystikJonas commented Feb 18, 2021

I just finished first draft of script that uses PHPStan to satic analysis of compiled Latte templates in our projects.

One think I noticed is that {varType} or {templateType} macros does not propagate any type related information to compiled Latte templates so PHPStan cannot check it.

Is there any plan to address this?

I could try to prepare PR which will address this but I would like to disccuss what approach to take here.

One option would be to simply add relevant /** @var type $variable */ annotations after each extract of parameters.

So insted of:

extract($this->params);
$a->call(); // PHPStan: OK

it would be:

extract($this->params);
/** @var string $a */
$a->call(); // PHPStan error: Cannot call method call() on string.

Another IMHO better approach would be to generate known params as template properties and instead of addressign them as $variable use $this->variable if variable is known but I am not sure how difficult that change would be.

And for {templateType} another option woudl be to add property $template of given type, populate it with passed values and address variables from templateType as $this->template->variable.

@lulco
Copy link

lulco commented Mar 29, 2021

Hi @MartinMystikJonas, @dg

I am thinking about this too...

What about this:

/** @var int $a */
$a = $this->params['a'];

/** @var string $b */
$b = $this->params['b'];

/** @var Selection $c */
$c = $this->params['c'];

extract($this->params);

where $a, $b, $c annotations are from varType or properties from templateType and could be used for static analysis, and extract($this->params) just keep it working like before this change.

@jakubvojacek
Copy link

Subscribing, that'd be cool!

@jakubvojacek
Copy link

Anyone managed to come up with somewhat usable solution how to analyse lattes via phpstan? The @var annotation would certainly be helpful

@lulco
Copy link

lulco commented May 26, 2021

@jakubvojacek I don't think so. @var annotations are just first part of it. I tried to add them to compiled templates and run phpstan on them but there are many other issues like filters etc.

@jakubvojacek
Copy link

too bad, that'd be killer feature :(

@MartinMystikJonas
Copy link
Author

I have managed to solve it. It is just proof of concept at the moment and without var annotations it is missing important checks. But it works. At the momeny I simply use PHpStan stubs to define known filters but I have idea how that can by solved better.

@MartinMystikJonas
Copy link
Author

If you are intetested I will share my implementation with you guys.

@jakubvojacek
Copy link

Please, that'd be perfect, I am sure many would appreciate that

@lulco
Copy link

lulco commented May 26, 2021

@MartinMystikJonas I am interested too :)

@MartinMystikJonas
Copy link
Author

Here you go: https://github.com/MetisFW/phpstan-latte

@jakubvojacek
Copy link

Thank you, managed to make it work. But seems like that without the var annotation it won't find much useful. Most of the errors we get are accessing non existing properties etc. Which the phpstan would have caught if he knew the type

@MartinMystikJonas
Copy link
Author

Well that is exactly the reason I started this issue in the first place. It wont catch most common errors without propagation of type information to compiled template.

@MartinMystikJonas
Copy link
Author

@jakubvojacek @lulco I created PR that will propagate type information into resulting PHP code. See: #275

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

No branches or pull requests

3 participants