unused-public PHPStan Extension

repository·main·Indexed 41 days ago

https://github.com/tomasvotruba/unused-public

A PHPStan extension for detecting unused public methods, properties, and constants. It helps identify elements that can be refactored to private or protected modifiers, featuring support for local-only method detection, template path exclusions for Twig and Blade, and the @api annotation to skip public API elements.

Tokens
612
Snippets
6
Records
6
Agent score
2%

What's inside unused-public

  1. Skip public elements using the @api annotation

    main

    If a public element is intentionally designed to be part of a public API (e.g., in an open-source library) and should not be reported as unused, mark the class or the specific element with the @api PHPDoc annotation.

    final class Book
    {
        /**
         * @api
         */
        public function getName()
        {
            return $this->name;
        }
    }
  2. Detect local-only public methods

    main

    Use the local_methods option to identify public methods that are only called within the same class or local scope. These are candidates to be refactored to private or protected modifiers.

    # phpstan.neon
    parameters:
        unused_public:
            local_methods: true
  3. Exclude methods called in Twig or Blade templates

    main

    To prevent false positives caused by methods being called exclusively in Twig or Blade templates, provide the directories containing your templates in the template_paths configuration key.

    # phpstan.neon
    parameters:
        unused_public:
            template_paths:
                - templates
  4. Set a threshold for allowed unused public methods

    main

    To handle large numbers of reported unused methods gradually, you can set a maximum allowed percentage of unused methods instead of a boolean toggle. If the percentage of unused methods exceeds this value, PHPStan will alert you; otherwise, it will be tolerated.

    # phpstan.neon
    parameters:
        unused_public:
            methods: 2.5