Skip to content

Commit

Permalink
ClassName::fromString(): trim leading namespace separator (phpactor#32
Browse files Browse the repository at this point in the history
)

* `ClassName::fromString()`: trim leading namespace separator

* CI: bump PHP version to 8.1

* Cast to string

---------

Co-authored-by: __ <__@__>
  • Loading branch information
przepompownia and __ authored Jan 26, 2024
1 parent 290dabd commit fad4fde
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
matrix:
php-version:
- '7.4'
- '8.1'

steps:
-
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
strategy:
matrix:
php-version:
- '7.4'
- '8.1'

steps:
-
Expand Down Expand Up @@ -85,8 +85,7 @@ jobs:
strategy:
matrix:
php-version:
- '7.4'
- '8.0'
- '8.1'

steps:
-
Expand Down
2 changes: 1 addition & 1 deletion lib/Adapter/Composer/ComposerClassToFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates

// order with the longest prefixes first
uksort($candidates, function ($prefix1, $prefix2) {
return strlen($prefix2) <=> strlen($prefix1);
return strlen((string)$prefix2) <=> strlen((string)$prefix1);
});

// flatten to a single array
Expand Down
2 changes: 1 addition & 1 deletion lib/Domain/ClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __toString()
public static function fromString(string $string): self
{
$new = new self();
$new->fullyQualifiedName = $string;
$new->fullyQualifiedName = ltrim($string, self::DEFAULT_NAMESPACE_SEPARATOR);

return $new;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Integration/Simple/SimpleClassToFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public function testClassToFile(): void
]), $candidates);
}

public function testAbsoluteClassNameToFile(): void
{
$candidates = $this->classToFile->classToFileCandidates(ClassName::fromString('\\Acme\\Foobar'));

$this->assertEquals(FilePathCandidates::fromFilePaths([
FilePath::fromString(__DIR__ . '/../../Workspace/lib/Foobar.php')
]), $candidates);
}

public function testClassToFileInvalid(): void
{
$candidates = $this->classToFile->classToFileCandidates(
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/ClassNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function provideBeginsWith()
'Foobar',
true,
],
[
'\\Foobar\\BarFoo',
'Foobar\\BarFoo',
true,
],
[
'Foobar\\BarFoo',
'Foobar\\BarFoo',
Expand Down

0 comments on commit fad4fde

Please sign in to comment.