{"id":15527422,"url":"https://github.com/tomasvotruba/unused-public","last_synced_at":"2025-05-15T07:07:24.378Z","repository":{"id":64834706,"uuid":"577911335","full_name":"TomasVotruba/unused-public","owner":"TomasVotruba","description":"Find Unused Public Elements in Your Code","archived":false,"fork":false,"pushed_at":"2025-03-07T15:28:05.000Z","size":450,"stargazers_count":178,"open_issues_count":0,"forks_count":12,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-15T07:07:15.839Z","etag":null,"topics":["dead-code","phpstan","static-analysis"],"latest_commit_sha":null,"homepage":"https://tomasvotruba.com/blog/can-phpstan-find-dead-public-methods/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TomasVotruba.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"tomasvotruba","custom":"https://www.paypal.me/rectorphp"}},"created_at":"2022-12-13T20:12:03.000Z","updated_at":"2025-04-25T22:45:42.000Z","dependencies_parsed_at":"2024-03-23T11:42:59.106Z","dependency_job_id":"963f3582-c8f9-4767-aa66-b8f29d330f69","html_url":"https://github.com/TomasVotruba/unused-public","commit_stats":{"total_commits":174,"total_committers":7,"mean_commits":"24.857142857142858","dds":"0.27586206896551724","last_synced_commit":"c82b690c4b262b2d3b460449224388f22be8250b"},"previous_names":[],"tags_count":114,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasVotruba%2Funused-public","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasVotruba%2Funused-public/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasVotruba%2Funused-public/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomasVotruba%2Funused-public/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomasVotruba","download_url":"https://codeload.github.com/TomasVotruba/unused-public/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254292042,"owners_count":22046426,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dead-code","phpstan","static-analysis"],"created_at":"2024-10-02T11:06:15.661Z","updated_at":"2025-05-15T07:07:19.357Z","avatar_url":"https://github.com/TomasVotruba.png","language":"PHP","funding_links":["https://github.com/sponsors/tomasvotruba","https://www.paypal.me/rectorphp"],"categories":[],"sub_categories":[],"readme":"# Find Unused Public Elements in Your Code\n\n\u003cbr\u003e\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg src=\"/docs/unused_public.jpg\" style=\"width: 10em\"\u003e\n\u003c/div\u003e\n\n\u003cbr\u003e\n\nIt's easy to find unused private class elements, because they're not used in the class itself. But what about public methods/properties/constants?\n\n```diff\n final class Book\n {\n     public function getTitle(): string\n     {\n         // ...\n     }\n\n-    public function getSubtitle(): string\n-    {\n-        // ...\n-    }\n}\n```\n\n**How can we detect unused public element?**\n\n* find a public method\n* find all public method calls in code and templates\n* if the public method is not found, it probably unused\n\nThat's exactly what this package does.\n\n\u003cbr\u003e\n\nThis technique is very useful for private projects and to detect accidentally used `public` modifier that should be changed to `private` as called locally only.\n\n\u003cbr\u003e\n\n## Install\n\n```bash\ncomposer require tomasvotruba/unused-public --dev\n```\n\nThe package is available for PHP 7.2+ version.\n\n\u003cbr\u003e\n\n## Usage\n\nWith [PHPStan extension installer](https://github.com/phpstan/extension-installer), everything is ready to run.\n\nEnable each item on their own with simple configuration:\n\n```yaml\n# phpstan.neon\nparameters:\n    unused_public:\n        methods: true\n        properties: true\n        constants: true\n```\n\n\u003cbr\u003e\n\nDo you have hundreds of reported public method? You don't have time to check them all, but want to handle them gradually?\n\nSet maximum allowed % configuration instead:\n\n```yaml\n# phpstan.neon\nparameters:\n    unused_public:\n        methods: 2.5\n```\n\nThis means maximum 2.5 % of all public methods is allowed as unused:\n\n* If it's 5 %, you'll be alerted.\n* If it's 1 %, it will be skipped as tolerated.\n\n\u003cbr\u003e\n\nDo you want to check local-only method calls that should not be removed, but be turned into `private`/`protected` instead?\n\n```yaml\n# phpstan.neon\nparameters:\n    unused_public:\n        local_methods: true\n```\n\n\u003cbr\u003e\n\n## Exclude methods called in templates\n\nSome methods are used only in TWIG or Blade templates, and could be reported false positively as unused.\n\n```twig\n{{ book.getTitle() }}\n```\n\nHow can we exclude them? Add your TWIG or Blade template directories in config to exclude methods names:\n\n\n```neon\n# phpstan.neon\nparameters:\n    unused_public:\n        template_paths:\n            - templates\n```\n\n\u003cbr\u003e\n\n## Known Limitations\n\nIn some cases, the rules report false positives:\n\n* when used only in templates, apart Twig paths, it's not possible to detect them\n\n\u003cbr\u003e\n\n## Skip Public-Only Methods\n\nOpen-source vendors design public API to be used by projects. Is element reported as unused, but it's actually designed to be used public?\n\nMark the class or element with `@api` annotation to skip it:\n\n```php\nfinal class Book\n{\n    /**\n     * @api\n     */\n    public function getName()\n    {\n        return $this-\u003ename;\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasvotruba%2Funused-public","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomasvotruba%2Funused-public","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasvotruba%2Funused-public/lists"}