Skip to content

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

@MartinMystikJonas

Description

@MartinMystikJonas

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions