ngmy / unused-public
Detect unused public properties, constants and methods in your code
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Installs: 1 188
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 12
Type:phpstan-extension
Requires
- php: ^8.1
- phpstan/phpstan: ^1.10
- webmozart/assert: ^1.11
Requires (Dev)
- nikic/php-parser: ^4.18
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/extension-installer: ^1.3
- phpunit/phpunit: ^10.3
- rector/rector: ^0.18.2
- symplify/easy-ci: ^11.2
- symplify/easy-coding-standard: ^12.0
- tomasvotruba/class-leak: ^0.1.3
- tomasvotruba/cognitive-complexity: ^0.2.2
- tomasvotruba/type-coverage: ^0.2
- tracy/tracy: ^2.10
This package is auto-updated.
Last update: 2024-11-15 15:35:43 UTC
README
It's easy to find unused private class elements, because they're not used in the class itself:
final class Book { public function getTitle(): string { // ... } - private function getSubtitle(): string - { - // ... - } }
But what about public class elements?
How can we detect such element?
- find a e.g. public method
- find all public method calls
- compare those in simple diff
- 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 open public API that should be used only locally.
Install
composer require tomasvotruba/unused-public --dev
The package is available on PHP 7.2-8.1 versions in tagged releases.
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 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; } }
Public Methods Used in Tests Only
Some public methods are designed to be used in tests only. Is element reported as unused, but it's actually used in tests?
Mark the class or element with @internal
annotation to reported as used:
final class Book { /** * @internal */ public function getName() { return $this->name; } }