Install unused-public via Composer
mainInstall the package as a development dependency using Composer. This package requires PHP 7.2 or higher.
composer require tomasvotruba/unused-public --devrepository·main·Indexed 41 days ago
https://github.com/tomasvotruba/unused-publicA 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.
Install the package as a development dependency using Composer. This package requires PHP 7.2 or higher.
composer require tomasvotruba/unused-public --devIf 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;
}
}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: trueTo 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:
- templatesTo 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.5If you are using the PHPStan extension installer, the package is automatically registered. You can enable detection for specific public elements (methods, properties, or constants) in your phpstan.neon configuration file.
# phpstan.neon
parameters:
unused_public:
methods: true
properties: true
constants: true