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

Fix: Match short namespace symbols #91

Merged
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
6 changes: 4 additions & 2 deletions src/Symbol/NamespaceSymbol.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace ComposerUnused\SymbolParser\Symbol;

use function rtrim;

final class NamespaceSymbol implements SymbolInterface
{
/** @var string */
private $namespace;

public function __construct(string $namespace)
{
$this->namespace = $namespace;
$this->namespace = rtrim($namespace, '\\') . '\\';
}

public static function fromClass(string $class): self
Expand All @@ -26,6 +28,6 @@ public function getIdentifier(): string

public function matches(SymbolInterface $symbol): bool
{
return strpos($symbol->getIdentifier(), $this->namespace) === 0;
return strpos(rtrim($symbol->getIdentifier(), '\\') . '\\', $this->namespace) === 0;
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Symbol/NamespaceSymbolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@ public function itShouldMatchNameSpaceFromClass(): void

self::assertTrue($namespaceSymbol->matches($namespaceSymbolFromClass));
}

/**
* @test
*/
public function itShouldMatchShortNamespaces(): void
{
$namespaceSymbol = new NamespaceSymbol('Foo\\Baz\\');
$symbol = new Symbol('Foo\\Baz');

self::assertTrue($namespaceSymbol->matches($symbol));
}
}