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 variable types to generated code to allow statical analysis (master) #275

Conversation

MartinMystikJonas
Copy link

@MartinMystikJonas MartinMystikJonas commented Sep 27, 2021

  • new feature
  • BC break? no

With this change Latte will propagate known types infomation from {varType}, {define} and {parameters} into compiled PHP code in form of /** @var type $var */ annotations. This will allow statical anylysis of resulting code by static analysers like PHPStan, Psalm, etc.

See discussion about this feature here #262

@MartinMystikJonas
Copy link
Author

MartinMystikJonas commented Sep 27, 2021

@dg Can you please chek this if it is valid approach from your point of view? It it seems ok to you I will add some more tests for edge cases and support for extracting types from {templateType}

@MartinMystikJonas
Copy link
Author

@dg Should I consider that like as approval of this approach? If so can you please approve to run workflows? Thanks.

@dg dg force-pushed the master branch 2 times, most recently from 6d7e553 to 53e5da9 Compare October 2, 2021 22:59
@@ -547,7 +552,7 @@ private function addBlock(MacroNode $node, string $layer = null): Block
private function extractMethod(MacroNode $node, Block $block, string $params = null): void
{
if (preg_match('#\$|n:#', $node->content)) {
$node->content = '<?php extract(' . ($node->name === 'block' && $node->closest(['embed']) ? 'end($this->varStack)' : '$this->params') . ');'
$node->content = '<?php ' . ($node->name === 'block' && $node->closest(['embed']) ? 'extract(end($this->varStack));' : $this->getCompiler()->paramsExtraction)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is necessary to keep params extraction inside block same as in top-level. Previous version always extracted all params inside block even when {parameters} is used.

@@ -174,7 +177,8 @@ private function buildClassBody(array $tokens): string
$this->output = &$output;
$this->inHead = true;
$this->macroNode = new RootNode;
$this->htmlNode = $this->context = $this->paramsExtraction = null;
$this->htmlNode = $this->context = null;
$this->paramsExtraction = $this->defaultParamsExtraction;
Copy link
Author

@MartinMystikJonas MartinMystikJonas Oct 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default params extraction needs to be set in the beggining so macros can extend it with type information (prepend @var annotations)

@dg
Copy link
Member

dg commented Oct 3, 2021

Hi, please give me some time, I'm busy this week.

Otherwise, it's best to do PR against the last released version, because I change and forcepush the master a lot.

@MartinMystikJonas
Copy link
Author

@dg Understood. Take your time and look at it whenever you will have time to check it. Let me know if you have any questions or requests how to finish this PR.

I will rebase it onto last release.

@MartinMystikJonas
Copy link
Author

New PR targeted onto v2.10 here #276

@MartinMystikJonas MartinMystikJonas changed the title Propagate variable types to generated code to allow statical analysis Propagate variable types to generated code to allow statical analysis (master) Oct 3, 2021
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

Successfully merging this pull request may close these issues.

2 participants