-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
Comments
I am thinking about this too... What about this:
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. |
Subscribing, that'd be cool! |
Anyone managed to come up with somewhat usable solution how to analyse lattes via phpstan? The |
@jakubvojacek I don't think so. |
too bad, that'd be killer feature :( |
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. |
If you are intetested I will share my implementation with you guys. |
Please, that'd be perfect, I am sure many would appreciate that |
@MartinMystikJonas I am interested too :) |
Here you go: https://github.com/MetisFW/phpstan-latte |
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 |
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. |
@jakubvojacek @lulco I created PR that will propagate type information into resulting PHP code. See: #275 |
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:
it would be:
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
.The text was updated successfully, but these errors were encountered: