shipmonk / name-collision-detector
Simple tool to find ambiguous classes or any other name duplicates within your project.
Installs: 711 462
Dependents: 7
Suggesters: 0
Security: 0
Stars: 31
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^7.2 || ^8.0
- ext-json: *
- ext-tokenizer: *
- nette/schema: ^1.1.0
Requires (Dev)
- editorconfig-checker/editorconfig-checker: ^10.3.0
- ergebnis/composer-normalize: ^2.19
- phpstan/phpstan: ^1.8.7
- phpstan/phpstan-phpunit: ^1.1.1
- phpstan/phpstan-strict-rules: ^1.2.3
- phpunit/phpunit: ^8.5.28 || ^9.5.20
- shipmonk/composer-dependency-analyser: ^1.0.0
- slevomat/coding-standard: ^8.0.1
This package is auto-updated.
Last update: 2024-12-01 10:35:34 UTC
README
Simple tool which allows you to detect if there are no types defined multiple times within the same namespace. This means that any ambiguous class, interface, enum, trait, constant or function is reported. Non-zero exit code is returned when any duplicate is found.
Installation:
composer require --dev shipmonk/name-collision-detector
Usage:
Check duplicate types:
vendor/bin/detect-collisions dir1 dir2 dir3 # relative to cwd
Example error output:
Foo\NamespacedClass2 is defined 2 times:
> /tests/sample-collisions/file2.php:23
> /tests/sample-collisions/file2.php:45
GlobalInterface1 is defined 2 times:
> /tests/sample-collisions/file1.php:8
> /tests/sample-collisions/file2.php:11
Example success output:
OK (no name collision found)
* analysed files: 9867
* excluded files: 0
* elapsed time: 1.057 s
- Note the performance: 10 000 files takes few seconds!
Configuration:
If file named collision-detector.json
is present within current working directory, its contents are taken as configuration options. Possible config options:
{ "scanPaths": ["src", "tests"], // files/directories to scan, relative to config file directory, glob not supported "excludePaths": ["tests/collisions"], // files/directories to exclude, relative to config file directory, glob not supported "fileExtensions": ["php"], // file extensions to parse "ignoreParseFailures": false // skip files with parse errors or not }
Paths provided by CLI arguments have priority over those in scanDirs
.
You can provide custom path to config file by vendor/bin/detect-collisions --configuration path/to/config.json
Reasoning
Having colliding classes within project can cause crazy headaches while debugging why something works only sometimes. Typically, you have PSR-4 autoloading which often solves this problem for you, but there are cases (like PHPStan rules test files) where you want to write any code (with classmap autoloading). And in such cases, the test may work when executed in standalone run, but fail when running all the tests together (depending on which class was autoloaded first). Therefore, having a collision detector in CI might be useful.
Composer's ambiguous class resolution
Composer can also detect class duplicates and exit with non-zero, but it runs much slower and lacks function and constant detection. Using composer version >= 2.8.1
, you can use:
composer dump --optimize --strict-ambiguous
Supported PHP versions
- PHP 7.2 - PHP 8.4