efabrica/phpstan-latte

0.17.0 2024-03-27 10:46 UTC

README

PHPStan extension to check compiled Latte templates in context of a Presenter or a Component etc. Templates are compiled to PHP code which is then analyzed with PHPStan rules according to a configured rule level and rules from loaded extensions.

The extension is based on Tomas Votruba's blog series and his packages symplify and reveal.

Preview

Features

  • Supports Latte 2 and Latte 3
  • Templates are analysed in context in which they are rendered (one template can be analysed with different contexts if it is used on multiple places)
  • Automatically collects all existing template variables, components, simple forms, render calls,... from PHP code.
  • Analyses existence and parameters of filters and functions.
  • Can analyse existence and parameters of links (if you configure applicationMapping).
  • Can use pre-configured latte engine from your application container (no need to configure manually)
  • Analyses existence of components, forms and form fields used in template
  • Analyses included templates in context of including template
  • Can resolve rendered template path even from more complex expressions (but still has limitations)
  • Can report unanalysed templates
  • You can use annotations to fine-tuning analysis of latte templates
  • If you use some non-standard and magic using rendering you can implement your own template resolvers (experimental)

Read How it works, when it doesn't, and troubleshooting

Installation

To use this extension, require it in Composer:

composer require efabrica/phpstan-latte --dev

If you also install phpstan/extension-installer then you're all set!

Manual installation

Add this line to your phpstan.neon:

includes:
    - vendor/efabrica/phpstan-latte/rules.neon

It is recommended to use this extension with phpstan-nette extension for better analysis of templates.

Configuration

If analysis reports errors about missing context (variables/components, ...) set in external packages you need to set option latte.collectedPaths as descibed in Analyser configuration.

Annotations

There are cases that cannot be resolved automatically by static analysis.

In these cases annotations line @phpstan-latte-ignore or @phpstan-latte-var could be used to guide resolvers to analyse latte templates correctly.

How to use annotations

How to extend this extension

This extension supports only common built-in methods for resolving paths to latte files, collecting variables, components, forms etc. However, we know that in a wild there are applications which use their own methods for these purposes. That's why we create this extension extensible. See more about this topic here.

Development

Changelog

If you want to contribute your PR are welcomed. If you want to discuss your idea first then create issue.

Thank you for using this package. Let us know if it helped you or what could be improved.