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

Mark objects returned in controllers (to Twig templates) as used

Open ruudk opened this issue 10 months ago • 1 comments

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.

ruudk avatar Mar 06 '25 15:03 ruudk

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?

janedbal avatar Mar 07 '25 09:03 janedbal