https://github.com/researchgate/phpnsc
Small tool to check for missing use statements when using PHP namespaces
https://github.com/researchgate/phpnsc
Last synced: over 1 year ago
JSON representation
Small tool to check for missing use statements when using PHP namespaces
- Host: GitHub
- URL: https://github.com/researchgate/phpnsc
- Owner: researchgate
- License: mit
- Created: 2011-09-23T13:18:27.000Z (almost 15 years ago)
- Default Branch: master
- Last Pushed: 2025-03-28T10:19:10.000Z (over 1 year ago)
- Last Synced: 2025-03-28T11:27:32.664Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 125 KB
- Stars: 43
- Watchers: 8
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
phpnsc
======

[](https://packagist.org/packages/rg/phpnsc)
[](https://packagist.org/packages/rg/phpnsc)
[](https://packagist.org/packages/rg/phpnsc)
The PHP namespace checker, checks all files in a project for invalid class references.
It prints an error, if a class was used with it's simple or relative name but is:
- not in the same namespace
- or not in the corresponding sub namespace
- or does not have a corresponding use statement
Usage
-----
- Add a JSON config file to your project root (see `config.sample.json`)
- Run phpnsc:
```bash
phpnsc run path/to/config.json
```
Configuration
-------------
The configuration has to be in a JSON file, that usually is placed in the root of your project. Example:
```json
{
"sources": [
{
"vendor": "vendorNamespace",
"folders": {
"root": "path/to/sources",
"include": [
"subpackage",
"subpackage_two"
],
"exclude": [
"subpackage/excluded"
]
},
"filetypes": {
"include": [
".php"
],
"exclude": [
".config.php"
]
}
}
],
"output": [
{
"class": "rg\\tools\\phpnsc\\CheckstyleOutput",
"parameter": "build/logs/phpnsc.xml"
},
{
"class": "rg\\tools\\phpnsc\\ConsoleOutput",
"parameter": ""
}
]
}
```
- sources: list of source folders to be scanned
- vendor: specifies the root vendor namespace of your project, see (https://gist.github.com/1234504)
- folders/root: the root folder of your project
- include: which sub-folders of your project root should be included?
- exclude: are there be any sub-sub-folders that should be excluded?
- filetypes: file types that should be included and excluded from analysis
- output: array of output classes that should be used. Currently support are:
- CheckstyleOutput (checkstyle compatible XML file, e.g. for a CI server)
- ConsoleOutput
How it works
------------
First the tool scans for all files matching the criteria defined in the config file. For each file it strips out all
unnecessary stuff like comments, strings or non php content. After that it analyzes all defined classes and interfaces
in your project and all used and referenced classes and interfaces in each file based on regular expressions.
In the next step, it then uses this information to deduct if all classes and interfaces are referenced correctly.
Current limitations
-------------------
This tool is still alpha. It is certainly possible, that it does not find all errors of that it finds false positives.
Additionally it can not handle importing several namespaces with one use statement and may have problems with using
aliases.