{"id":15472608,"url":"https://github.com/sirbrillig/phpcs-import-detection","last_synced_at":"2025-06-30T15:34:33.276Z","repository":{"id":46119721,"uuid":"140340614","full_name":"sirbrillig/phpcs-import-detection","owner":"sirbrillig","description":"A set of phpcs sniffs to look for unused or unimported symbols.","archived":false,"fork":false,"pushed_at":"2024-01-28T19:04:09.000Z","size":118,"stargazers_count":32,"open_issues_count":10,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-25T15:48:17.787Z","etag":null,"topics":["linting","php","phpcs"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sirbrillig.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2018-07-09T20:48:55.000Z","updated_at":"2025-05-02T19:52:39.000Z","dependencies_parsed_at":"2024-01-28T20:25:35.889Z","dependency_job_id":"f8250268-16e7-446f-ae35-1990b3e37604","html_url":"https://github.com/sirbrillig/phpcs-import-detection","commit_stats":{"total_commits":70,"total_committers":7,"mean_commits":10.0,"dds":"0.11428571428571432","last_synced_commit":"7035ef6f3a15db182b59664d2c060918aa827e16"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirbrillig%2Fphpcs-import-detection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirbrillig%2Fphpcs-import-detection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirbrillig%2Fphpcs-import-detection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirbrillig%2Fphpcs-import-detection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sirbrillig","download_url":"https://codeload.github.com/sirbrillig/phpcs-import-detection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirbrillig%2Fphpcs-import-detection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259265634,"owners_count":22831207,"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":["linting","php","phpcs"],"created_at":"2024-10-02T02:40:23.480Z","updated_at":"2025-06-11T12:37:00.286Z","avatar_url":"https://github.com/sirbrillig.png","language":"PHP","readme":"# ImportDetection\n\nA set of [phpcs](https://github.com/squizlabs/PHP_CodeSniffer) sniffs to look for unused or unimported symbols.\n\nThis adds a sniff which shows warnings if a symbol (function, constant, class) is used and is not defined directly, imported explicitly, nor has its namespace imported.\n\n\u003e [!WARNING]\n\u003e PHP 8 changed how `use` statements are tokenized, leading to [this bug](https://github.com/sirbrillig/phpcs-import-detection/issues/52) which basically breaks this sniff. This sniff also has fairly poor performance. I don't have time with my current work to continue to refactor this sniff at the moment and I wouldn't recommend it until at least that issue is fixed. If anyone wants to work on improvements, feel free to open a PR!\n\nWhen code is moved around, it can be problematic if classes which are used in a relative or global context get moved to a different namespace. In those cases it's better if the classes use their fully-qualified namespace, or if they are imported explicitly using `use` (in which case they can be detected by a linter like this one). These warnings should help when refactoring code to avoid bugs.\n\nIt also detects imports which are _not_ being used.\n\nFor example:\n\n```php\nnamespace Vehicles;\nuse Registry;\nuse function Vehicles\\startCar;\nuse Chocolate; // this will be a warning because `Chocolate` is never used\nclass Car {\n  public function drive() {\n    startCar(); // this is fine because `startCar` is imported\n    Registry\\registerCar($this); // this is fine because `Registry` is imported\n    \\DrivingTracker\\registerDrive($this); // this is fine because it's fully-qualified\n    goFaster(); // this will be a warning because `goFaster` was not imported\n  }\n}\n```\n\n**Note:** This sniff is a lightweight syntax checker providing a scan of the current file and it doesn't know what other files might have defined. Therefore it will warn you about implicitly imported symbols even if they're in the same namespace. It's safe to import something from the same namespace and can even improve readability, but if you'd prefer to scan multiple files, I suggest using static analysis tools like [psalm](https://psalm.dev/) or [phpstan](https://github.com/phpstan/phpstan).\n\n## Installation\n\nTo use these rules in a project which is set up using [composer](https://href.li/?https://getcomposer.org/), we recommend using the [phpcodesniffer-composer-installer library](https://href.li/?https://github.com/DealerDirect/phpcodesniffer-composer-installer) which will automatically use all installed standards in the current project with the composer type `phpcodesniffer-standard` when you run phpcs.\n\n```\ncomposer require --dev sirbrillig/phpcs-import-detection dealerdirect/phpcodesniffer-composer-installer\n```\n\n## Configuration\n\nWhen installing sniff standards in a project, you edit a `phpcs.xml` file with the `rule` tag inside the `ruleset` tag. The `ref` attribute of that tag should specify a standard, category, sniff, or error code to enable. It’s also possible to use these tags to disable or modify certain rules. The [official annotated file](https://href.li/?https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml) explains how to do this.\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"MyStandard\"\u003e\n \u003cdescription\u003eMy library.\u003c/description\u003e\n \u003crule ref=\"ImportDetection\"/\u003e\n\u003c/ruleset\u003e\n```\n\n## Sniff Codes\n\nThere are two sniff codes that are reported by this sniff. Both are warnings.\n\n- `ImportDetection.Imports.RequireImports.Symbol`: A symbol has been used but not imported\n- `ImportDetection.Imports.RequireImports.Import`: A symbol has been imported and not used\n\nIn any given file, you can use phpcs comments to disable these sniffs. For example, if you have a global class called `MyGlobalClass` which you don't want to import, you could use it like this:\n\n```php\n\u003c?php\n\n$instance = new MyGlobalClass(); // phpcs:ignore ImportDetection.Imports.RequireImports.Symbol -- this class is global\n$instance-\u003edoSomething();\n```\n\nFor a whole file, you can ignore a sniff like this:\n\n```php\n\u003c?php\n// phpcs:disable ImportDetection.Imports.RequireImports.Symbol\n\n$instance = new MyGlobalClass();\n$instance-\u003edoSomething();\n```\n\nFor a whole project, you can use the `phpcs.xml` file to disable these sniffs or modify their priority. For example, to disable checks for unused imports, you could use a configuration like this:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"MyStandard\"\u003e\n \u003cdescription\u003eMy library.\u003c/description\u003e\n \u003crule ref=\"ImportDetection\"/\u003e\n \u003crule ref=\"ImportDetection.Imports.RequireImports.Import\"\u003e\n   \u003cseverity\u003e0\u003c/severity\u003e\n \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\n## Ignoring Symbol Patterns\n\nOftentimes there might be global symbols that you want to use without importing or using a fully-qualified path.\n\n(Remember that function call resolution first searches the current namespace, then the global namespace, but constant and class resolution only searches the current namespace! You still have to import things like `Exception` or use the fully-qualified `\\Exception`.)\n\nYou can ignore certain patterns by using the `ignoreUnimportedSymbols` config option. It is a regular expression. Here is an example for some common WordPress symbols:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"MyStandard\"\u003e\n \u003cdescription\u003eMy library.\u003c/description\u003e\n \u003crule ref=\"ImportDetection\"/\u003e\n \u003crule ref=\"ImportDetection.Imports.RequireImports\"\u003e\n   \u003cproperties\u003e\n    \u003cproperty name=\"ignoreUnimportedSymbols\" value=\"/^(wp_parse_args|OBJECT\\S*|ARRAY_\\S+|is_wp_error|__|esc_html__|get_blog_\\S+)$/\"/\u003e\n  \u003c/properties\u003e\n \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\nDespite the name, you can also use the `ignoreUnimportedSymbols` pattern to ignore specific unused imports.\n\n## Ignoring Global Symbols in Global Namespace\n\nIf a file is in the global namespace, then sometimes it may be unnecessary to import functions that are also global. If you'd like to ignore global symbol use in the global namespace, you can enable the `ignoreGlobalsWhenInGlobalScope` option, like this:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"MyStandard\"\u003e\n \u003cdescription\u003eMy library.\u003c/description\u003e\n \u003crule ref=\"ImportDetection\"/\u003e\n \u003crule ref=\"ImportDetection.Imports.RequireImports\"\u003e\n   \u003cproperties\u003e\n    \u003cproperty name=\"ignoreGlobalsWhenInGlobalScope\" value=\"true\"/\u003e\n  \u003c/properties\u003e\n \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\n## Ignoring WordPress Patterns\n\nA common use-case is to ignore all the globally available WordPress symbols. Rather than trying to come up with a pattern to ignore them all yourself, you can set the config option `ignoreWordPressSymbols` which will ignore as many of them as it knows about. For example:\n\n```xml\n\u003c?xml version=\"1.0\"?\u003e\n\u003cruleset name=\"MyStandard\"\u003e\n \u003cdescription\u003eMy library.\u003c/description\u003e\n \u003crule ref=\"ImportDetection\"/\u003e\n \u003crule ref=\"ImportDetection.Imports.RequireImports\"\u003e\n   \u003cproperties\u003e\n    \u003cproperty name=\"ignoreWordPressSymbols\" value=\"true\"/\u003e\n  \u003c/properties\u003e\n \u003c/rule\u003e\n\u003c/ruleset\u003e\n```\n\n## Usage\n\nMost editors have a phpcs plugin available, but you can also run phpcs manually. To run phpcs on a file in your project, just use the command-line as follows (the `-s` causes the sniff code to be shown, which is very important for learning about an error).\n\n```\nvendor/bin/phpcs -s src/MyProject/MyClass.php\n```\n\n## See Also\n\n- [VariableAnalysis](https://github.com/sirbrillig/phpcs-variable-analysis): Find undefined and unused variables.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsirbrillig%2Fphpcs-import-detection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsirbrillig%2Fphpcs-import-detection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsirbrillig%2Fphpcs-import-detection/lists"}