Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Name Collision Detector Laravel Package

shipmonk/name-collision-detector

Fast CLI tool to detect duplicate names in PHP code: classes, interfaces, traits, enums, functions, and constants defined multiple times in the same namespace. Scans thousands of files in seconds, supports config/excludes, exits non-zero on collisions.

View on GitHub
Deep Wiki
Context7

Name collision detector

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
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport