tomasvotruba / unused-public
Detect unused public properties, constants and methods in your code
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Installs: 1 369 657
Dependents: 76
Suggesters: 0
Security: 0
Stars: 148
Watchers: 4
Forks: 12
Open Issues: 0
Type:phpstan-extension
Requires
- php: ^7.2 || ^8.0
- phpstan/phpstan: ^1.10.19
- webmozart/assert: ^1.11
- dev-main
- 1.0.0
- 0.4.0
- 0.3.11
- 0.3.10
- 0.3.9
- 0.3.8
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2.72
- 0.3.2
- 0.3.1.72
- 0.3.1
- 0.3.0.72
- 0.3.0
- 0.2.0.72
- 0.2.0
- 0.1.14.72
- 0.1.14
- 0.1.13.72
- 0.1.13
- 0.1.12.72
- 0.1.12
- 0.1.11.72
- 0.1.11
- 0.1.10.72
- 0.1.10
- 0.1.9.72
- 0.1.9
- 0.1.8.72
- 0.1.8
- 0.1.7.72
- 0.1.7
- 0.1.6.72
- 0.1.6
- 0.1.5.72
- 0.1.5
- 0.1.4.72
- 0.1.4
- 0.1.3.72
- 0.1.3
- 0.1.2.72
- 0.1.2
- 0.1.1.72
- 0.1.1
- 0.1.0.72
- 0.1.0
- 0.0.35.72
- 0.0.35
- 0.0.34.72
- 0.0.34
- 0.0.33.72
- 0.0.33
- 0.0.32.72
- 0.0.32
- 0.0.31.72
- 0.0.31
- 0.0.30.72
- 0.0.30
- 0.0.29.72
- 0.0.29
- 0.0.28.72
- 0.0.28
- 0.0.27.72
- 0.0.27
- 0.0.26.72
- 0.0.26
- 0.0.25.72
- 0.0.25
- 0.0.24.72
- 0.0.24
- 0.0.23.72
- 0.0.23
- 0.0.22
- 0.0.21.72
- 0.0.21
- 0.0.20.72
- 0.0.20
- 0.0.19.72
- 0.0.19
- 0.0.18.72
- 0.0.18
- 0.0.17.72
- 0.0.17
- 0.0.16.72
- 0.0.16
- 0.0.15.72
- 0.0.15
- 0.0.14.72
- 0.0.14
- 0.0.13.72
- 0.0.13
- 0.0.12.72
- 0.0.12
- 0.0.11.72
- 0.0.11
- 0.0.10.72
- 0.0.10
- 0.0.9.72
- 0.0.9
- 0.0.8.72
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-tv-open-php
- dev-tv-skip-silent-methods
This package is auto-updated.
Last update: 2024-10-31 15:47:26 UTC
README
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; } }