dead-code-detector
dead-code-detector copied to clipboard
Mark objects returned in controllers (to Twig templates) as used
Let's say you have the following:
final readonly class SomeModel
{
public function __construct(
private string $name,
) {}
public function getName() : string
{
return $this->name;
}
}
And you return it inside a controller to a Twig template like this:
#[AsController]
final class SomeController extends AbstractController
{
#[Route(path: '/some', name: 'some')]
#[Template('some.html.twig')]
public function __invoke() : array
{
return [
'some' => new SomeModel('Ruud')
];
}
}
It will report that getName is unused. But it might be, we have no idea if it is used or not since it goes to Twig.
Would it be possible to do something similar to MemberUsageExcluder, but then for Inclusion?
I want all ClassMemberUsages that originate from a Controller Action, and go to a Template, marked as used.
Sure, you can do this easily with AST-based customization. This specific usecase also feels like it might even be natively in this repo, WDYT?