dead-code-detector icon indicating copy to clipboard operation
dead-code-detector copied to clipboard

How to deal with usage in templates (like Twig)?

Open ruudk opened this issue 1 year ago • 2 comments

Currently, the extension is unaware of usage in templates (like Twig).

I'm working on TwigStan. TwigStan is a separate tool that invokes PHPStan (it's not an extension). It compiles Twig templates to PHP and then cleans them up. It then executes PHPStan.

I was thinking: if TwigStan runs PHPStan with dead-code-detector extension enabled, we could export the MethodCallCollector to a JSON file. This data has all the method calls from the Twig templates to entities / methods.

Then, when normally running PHPStan on the whole project, we could include the data from JSON with the other MethodCallCollector.

Now, dead-code-detector has a full picture about calls in PHP and in Twig.

The downside of this approach is obviously that the dead-code-detector extension has a dependency on the TwigStan run.

But I think it could be fine to only run dead-code-detector on CI, after TwigStan was completed.

ruudk avatar Sep 14 '24 07:09 ruudk

It compiles Twig templates to PHP and then cleans them up. It then executes PHPStan.

If your "methods called in twig" can be exported to some json, then it should be easy to create some TwigEntrypointProvider that reads that and marks such methods as used, no?

janedbal avatar Sep 14 '24 11:09 janedbal

Looking at what the EntrypointProvider does, it just mimics a call to something. Interesting, I'll play with that in the future 😊

ruudk avatar Sep 14 '24 14:09 ruudk