GitXplorerGitXplorer
T

unused-public

public
164 stars
12 forks
0 issues

Commits

List of commits on branch main.
Verified
8d46b4d4e2c01425acbae5b84ca8a08eb7eeb843

restore type coverage (#142)

TTomasVotruba committed a month ago
Verified
160ae3f99fbdc2929446989bf6475b1e8c003a42

Bump to Rector 2 and PHPStan 2 (#141)

TTomasVotruba committed a month ago
Unverified
f06119309041b7c3fa2e519afffab284c50f80a7

cs

TTomasVotruba committed a month ago
Verified
30516153775e7fb8429bcfd1594866003407caa3

Detect callables in any static array (#140)

sstaabm committed a month ago
Verified
177f34403f4ad4664bcfe4b7328b669a5b755394

Detect callables invoked via `register_shutdown_function` (#139)

sstaabm committed a month ago
Verified
c82b690c4b262b2d3b460449224388f22be8250b

Reduce memory consumption of collectors (#131)

sstaabm committed 4 months ago

README

The README file for this repository.

Find Unused Public Elements in Your Code



It's easy to find unused private class elements, because they're not used in the class itself. But what about public methods/properties/constants?

 final class Book
 {
     public function getTitle(): string
     {
         // ...
     }

-    public function getSubtitle(): string
-    {
-        // ...
-    }
}

How can we detect unused public element?

  • find a public method
  • find all public method calls in code and templates
  • if the public method is not found, it probably unused

That's exactly what this package does.


This technique is very useful for private projects and to detect accidentally used public modifier that should be changed to private as called locally only.


Install

composer require tomasvotruba/unused-public --dev

The package is available for PHP 7.2+ version.


Usage

With PHPStan extension installer, everything is ready to run.

Enable each item on their own with simple configuration:

# phpstan.neon
parameters:
    unused_public:
        methods: true
        properties: true
        constants: true

Do you have hundreds of reported public method? You don't have time to check them all, but want to handle them gradually?

Set maximum allowed % configuration instead:

# phpstan.neon
parameters:
    unused_public:
        methods: 2.5

This means maximum 2.5 % of all public methods is allowed as unused:

  • If it's 5 %, you'll be alerted.
  • If it's 1 %, it will be skipped as tolerated.

Do you want to check local-only method calls that should not be removed, but be turned into private/protected instead?

# phpstan.neon
parameters:
    unused_public:
        local_methods: true

Exclude methods called in templates

Some methods are used only in TWIG or Blade templates, and could be reported false positively as unused.

{{ book.getTitle() }}

How can we exclude them? Add your TWIG or Blade template directories in config to exclude methods names:

# phpstan.neon
parameters:
    unused_public:
        template_paths:
            - templates

Known Limitations

In some cases, the rules report false positives:

  • when used only in templates, apart Twig paths, it's not possible to detect them

Skip Public-Only Methods

Open-source vendors design public API to be used by projects. Is element reported as unused, but it's actually designed to be used public?

Mark the class or element with @api annotation to skip it:

final class Book
{
    /**
     * @api
     */
    public function getName()
    {
        return $this->name;
    }
}