Skip to content

Commit b104176

Browse files
committed
bug #1335 Ignore use statement generator duplicates (mdoutreluingne)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- Ignore use statement generator duplicates Follow #1328 (comment) Following weaverryan's idea I've improved `UseStatementGenerator` in this PR to ignore duplicates. Commits ------- 1bfaf30 Ignore use statement generator duplicates
2 parents a1733f8 + 1bfaf30 commit b104176

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Util/UseStatementGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public function __toString(): string
4545
$class = $aliasClass;
4646
}
4747

48-
$transformed[$key] = str_replace('\\', ' ', $class);
48+
$transformedClass = str_replace('\\', ' ', $class);
49+
// Let's not add the class again if it already exists.
50+
if (!\in_array($transformedClass, $transformed, true)) {
51+
$transformed[$key] = $transformedClass;
52+
}
4953
}
5054

5155
asort($transformed);

tests/Util/UseStatementGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,20 @@ public function testUseStatementsWithAliases(): void
9090
EOT;
9191
self::assertSame($expected, (string) $unsorted);
9292
}
93+
94+
public function testUseStatementsWithDuplicates(): void
95+
{
96+
$unsorted = new UseStatementGenerator([
97+
\Symfony\UX\Turbo\Attribute\Broadcast::class,
98+
\ApiPlatform\Core\Annotation\ApiResource::class,
99+
\ApiPlatform\Core\Annotation\ApiResource::class,
100+
]);
101+
102+
$expected = <<< 'EOT'
103+
use ApiPlatform\Core\Annotation\ApiResource;
104+
use Symfony\UX\Turbo\Attribute\Broadcast;
105+
106+
EOT;
107+
self::assertSame($expected, (string) $unsorted);
108+
}
93109
}

0 commit comments

Comments
 (0)