idea-php-symfony2-plugin icon indicating copy to clipboard operation
idea-php-symfony2-plugin copied to clipboard

Mark code as used

Open stof opened this issue 5 years ago • 8 comments

PHPStorm 2019.2 has a new inspection reporting unused code. But this marks a bunch of Symfony-specific cases as unused while they are not (well, some of them are more about related packages and may belong to a separate plugin, but I'm still listing them here):

  • controller methods should be marked as used when a route is defined for them
  • commands should be marked as used when they are registered as commands in the container (as they are registered in the CLI)
  • event listeners should be marked as used:
    • for event subscribers, the listener methods should be marked as used based on getSubscribedEvents
    • services tagged as kernel.event_listener should mark the listener methods as used too
  • twig extension methods should be marked as used when they are references by registered functions/filters/tests (even better would be to check for usages of the function/filter/test in templates instead, but that may be too much)
  • doctrine repositories should be marked as used when they are referenced as repository in the mapping of an entity
  • constraint validators should be marked as used by their associated constraint
  • validation constraint should be marked as used when they are used in some mapping file (XML or YAML) or as annotation (this case should be handled in a generic way by the annotations plugin instead).
  • voters should be marked as used when they are registered as voters in the container

the list probably continues with other kind of services being tagged as hooks in other services btw.

note: I will keep updating the list if I think about more things.

stof avatar Sep 03 '19 09:09 stof

Thanks for this list, just for related issues there is also one on PhpStorm itself to provide an API for this: https://youtrack.jetbrains.com/issue/WI-47938

Maxim Kolmakov commented 2 Aug 2019 13:13

@James Antrim The main point of of the unused declaration inspection is to find unused public classes/methods :) What is missing is additional support for frameworks magic which is provided by 3rd-party Symfony/Laravel plugin and our own Drupal. Drupal is covered by WI-47049 and Symfony by this issue. We will implement the required API and provide a PR to the plugins that will solve all the listed issue that is why umbrella issue is better in this case than separately listed problems.

Haehnchen avatar Sep 03 '19 13:09 Haehnchen

controller methods should be marked as used when a route is defined for them

I'd see it as a bonus if they're only be marked as used if that route is actually used anywhere and/or the method is referenced (i.e. controller('...') in a Twig template)

MisatoTremor avatar Sep 04 '19 09:09 MisatoTremor

that would be an issue to me. Routes define public entry points to your site (at least GET routes for websites). Knowing whether these routes are used would involve scanning all external links pointing to your site too.

stof avatar Sep 04 '19 09:09 stof

  • Entity getters and setters (which are used in Forms): getX, addX, removeX.
  • Methods (only) called in Twig, but there is a separate issue for that: #872

I'd see it as a bonus if they're only be marked as used if that route is actually used anywhere and/or the method is referenced (i.e. controller('...') in a Twig template)

$route = 'home_' . $user->getUserType()';
$url = $this->generateUrl($route);

Good luck with that :smirk:

stephanvierkant avatar Sep 04 '19 14:09 stephanvierkant

Good points indeed. 😊 But maybe still viable as an option which is off by default

MisatoTremor avatar Sep 05 '19 00:09 MisatoTremor

Just to add if it wasn't already:

  • Factory classes and methods that are defined in services are shown as unused.

johnpancoast avatar Feb 04 '20 20:02 johnpancoast

Matouš Němec commented 8 Jan 2022 20:39

Hello, solution is extension point implicitUsageProvider. You can implement com.intellij.codeInsight.daemon.ImplicitUsageProvider to your plugin to solve this problem. Use method isImplicitUsage.

  • so finally at least PhpClasses and Method now can be marked as used. lets start with controllers.
  • other elements like fields (properties) are not trigger by this new extension point (eg for supporting Doctrine metadata)

Haehnchen avatar May 03 '22 19:05 Haehnchen

Hi, here is a specific case which is broken for me:

config/services.yaml

services:
    app.dashboard_helper:
        class: App\Helper\DashboardHelper
        public: false

config/packages/twig.yaml

twig:
    globals:
      dashboard_helper: '@app.dashboard_helper'

src/Helper/DashboardHelper.php

<?php

namespace App\Helper;

class DashboardHelper
{
    public function getTest(): string
    {
        return 'test';
    }
}

template.twig

...
    {% set testString = dashboard_helper.getTest() %}
...

RESULT

DashboardHelper::getTest() is detected as unused

michnovka avatar Oct 08 '22 09:10 michnovka