• Stars
    star
    137
  • Rank 266,121 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Find Unused Public Elements in Your Code

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;
    }
}

More Repositories

1

bladestan

PHPStan analysis for Blade templates
PHP
288
star
2

awesome-php-migrations

Awesome sources for PHP projects migrations - legacy, pattern refactoring, framework switches, temlates and configs...
208
star
3

lines

CLI tool for quick size measure of PHP project, runs anywhere
PHP
163
star
4

punchcard

[Deprecated] Laravel 11 comes with simliar solution out of the box
PHP
142
star
5

type-coverage

PHPStan extension to require minimal type-coverage
PHP
132
star
6

phpstan-bodyscan

Get error count for each PHPStan level
PHP
121
star
7

awesome-php-ast

Awesome sources for amazing AST topic in world PHP
114
star
8

cognitive-complexity

PHPStan rules to measure cognitive complexity of your classes and methods
PHP
102
star
9

tomasvotruba.com

Read about upgrades, static analysis, GPT, Symfony and Laravel
PHP
88
star
10

class-leak

Find leaking classes that you never use... and get rid of them.
PHP
70
star
11

PhpStorm-LiveTemplates

Collection of Live Templates for PhpStorm
12
star
12

rector-composer-plugin

Detect Package Changes and run Rector Upgrade code after composer update
PHP
11
star
13

laracon-2024-rector-demo

Laracon talk for Amsterdam 2024 - Slides are here ↓
PHP
10
star
14

symfony-upgrade-demo-2022

PHP
9
star
15

phpunit-json-result

Print tests result in JSON in PHPUnit 10+ so other tools can use it
PHP
9
star
16

laravelize

Toolkit to help with your Symfony to Laravel migration
PHP
7
star
17

symfony-config-generator

Generate Symfony configs in CLI and use them for autocomplete
PHP
7
star
18

easystan

[WIP]
PHP
6
star
19

yaml-parser

AST for YAML
PHP
5
star
20

aws-sdk-php-symfony

Symfony 6/7+ bundle for Aws integration
PHP
5
star
21

shopsys-spryker-and-sylius-analysis

Static analysis of Shopsys, Sylius and Spryker
PHP
4
star
22

html-parser

Parse and traverse HTML using AST
PHP
3
star
23

rector-annotation-attributes-demo

PHP
3
star
24

editorconfig-fixer

PHP
3
star
25

laratyped

Bunch of PHPStan rules to make sure the Laravel project is clean, typed and reliable
PHP
3
star
26

symfony-legacy-controller-autowire

PHP
2
star
27

porto-01-meetup

PHP
2
star
28

torch

Smoke testing of TWIG templates
PHP
2
star
29

tomasvotruba

GitHub Profile repository :)
1
star
30

rector-syntax-bug

PHP
1
star
31

rector_parallel_test_ownci

PHP
1
star
32

rectoring.com

HTML
1
star
33

embedded-svg

Macro for embeding SVG into HTML. Preparing for Latte 3
PHP
1
star
34

rector-underscore-to-psr4

Test for https://github.com/rectorphp/rector/issues/7016
PHP
1
star
35

tryml

Trim your YAML files to minimum
PHP
1
star
36

mockistan

1
star