Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop BetterReflection to speed up analysis #12

Merged
merged 5 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Name collision detector

Simple tool which allows you to detect if there are no classes/functions/constants defined multiple times within the same namespace.
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:
Expand All @@ -10,26 +11,18 @@ composer require --dev shipmonk/name-collision-detector
```

## Usage:
Check duplicate classes, constants and functions:
Check duplicate types:
```sh
vendor/bin/detect-collisions dir1 dir2 dir3
```

Or you can select what to check:

```sh
vendor/bin/detect-collisions --classes src tests # check only duplicate classes
vendor/bin/detect-collisions --functions src tests # check only duplicate functions
vendor/bin/detect-collisions --constants src tests # check only duplicate constants
```

Example output:
```
Foo\NamespacedClass2 is defined 2 times:
> /tests/sample-collisions/file2.php
> /tests/sample-collisions/file2.php

GlobalClass1 is defined 2 times:
GlobalInterface1 is defined 2 times:
> /tests/sample-collisions/file1.php
> /tests/sample-collisions/file2.php
```
Expand All @@ -40,6 +33,6 @@ Typically, you have PSR-4 autoloading solving this problem for you, but there ar
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.

## Versions
- 1.x supports PHP 7.2 - PHP 8.2
## Supported PHP versions
- PHP 7.2 - PHP 8.2

23 changes: 6 additions & 17 deletions bin/detect-collisions
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

use ShipMonk\FileParsingException;
use ShipMonk\InvalidPathProvidedException;
use ShipMonk\NameCollisionDetector;

Expand All @@ -16,16 +17,10 @@ foreach ($autoloadFiles as $autoloadFile) {
}
}

$possibleOptions = ['classes', 'functions', 'constants'];
$providedOptions = getopt('', $possibleOptions, $restIndex);
$check = array_keys($providedOptions);
$check = $check === [] ? $possibleOptions : $check; // check all if no option is provided

$cwd = getcwd();
$providedFolders = array_slice($argv, $restIndex);
$directories = array_map(static function (string $arg) use ($cwd): string {
$directories = array_map(static function ($arg) use ($cwd) {
return $cwd . '/' . $arg;
}, $providedFolders);
}, array_slice($argv, 1));

if ($directories === []) {
echo "ERROR: no directories provided, use e.g. `detect-collisions src tests`\n";
Expand All @@ -34,19 +29,13 @@ if ($directories === []) {

try {
$detector = new NameCollisionDetector($directories, $cwd);
} catch (InvalidPathProvidedException $e) {
$collisions = $detector->getCollidingTypes();

} catch (InvalidPathProvidedException | FileParsingException $e) {
echo "ERROR: {$e->getMessage()}\n";
exit(255);
}

echo "Checking duplicates of " . implode(' and ', $check) . " in " . implode(', ', $providedFolders) . ":\n\n";

$collisions = array_merge(
in_array('classes', $check, true) ? $detector->getCollidingClasses() : [],
in_array('functions', $check, true) ? $detector->getCollidingFunctions() : [],
in_array('constants', $check, true) ? $detector->getCollidingConstants() : []
);

foreach ($collisions as $name => $filePaths) {
$count = count($filePaths);
echo "$name is defined $count times:\n";
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
{
"name": "shipmonk/name-collision-detector",
"description": "Simple tool to find class/function/constant name duplicates within your project.",
"description": "Simple tool to find ambiguous classes or any other name duplicates within your project.",
"license": [
"MIT"
],
"keywords": [
"namespace",
"collision",
"ambiguous",
"autoload",
"classname"
"classname",
"autoloading"
],
"require": {
"roave/better-reflection": "4.* || 5.* || 6.*"
"php": "^7.2 || ^8.0",
"ext-tokenizer": "*"
},
"require-dev": {
"editorconfig-checker/editorconfig-checker": "^10.3.0",
Expand Down
Loading