Skip to content

Commit 9a1346b

Browse files
committed
Fix: prevent $sourceFilePathNames from shadowing retroactively added paths when ColocatedMappingDriver::addPaths() is called after instance creation.
After github.com/doctrine/pull/429/ had been merged, it could bring in subtle bugs during migration toward the new `$sourceFilePathNames` approach. If the client code had called `addPaths()` after the instance creation, it would've been ignored (without any warning or so), and paths would not have been used. This commit fixes that. Retroactively added paths will be taken into account as well as ``$sourceFilePathNames`.
1 parent beef6b3 commit 9a1346b

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Persistence/Mapping/Driver/ColocatedMappingDriver.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ public function getAllClassNames(): array
161161
}
162162

163163
/** @var iterable<string> $sourceFilePathNames */
164-
$sourceFilePathNames = $this->sourceFilePathNames ?? $this->pathNameIterator($filesIterator);
164+
$sourceFilePathNames = $this->mergeIterables(
165+
$this->sourceFilePathNames ?? [],
166+
$this->pathNameIterator($filesIterator),
167+
);
165168
$includedFiles = [];
166169

167170
foreach ($sourceFilePathNames as $sourceFile) {
@@ -217,4 +220,24 @@ private function pathNameIterator(iterable $filesIterator): Generator
217220
yield $file->getPathname();
218221
}
219222
}
223+
224+
/**
225+
* @param iterable<TKey, T> $iterable1
226+
* @param iterable<TKey, T> $iterable2
227+
*
228+
* @return Generator<TKey, T>
229+
*
230+
* @template TKey
231+
* @template T
232+
*/
233+
private function mergeIterables(iterable $iterable1, iterable $iterable2): Generator
234+
{
235+
foreach ($iterable1 as $key => $item) {
236+
yield $key => $item;
237+
}
238+
239+
foreach ($iterable2 as $key => $item) {
240+
yield $key => $item;
241+
}
242+
}
220243
}

tests/Persistence/Mapping/ColocatedMappingDriverTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ public function testGetAllClassNamesForIterableFilePathNames(): void
8181
self::assertSame([Entity::class], $classes, 'The driver should only return the class names from the provided file path names, excluding transient class names.');
8282
}
8383

84+
public function testGetAllClassNamesForBothIterableFilePathNamesAndRetroactivelyAddedDirectoryPaths(): void
85+
{
86+
$driver = $this->createFilePathNamesDriver([__DIR__ . '/_files/colocated/Entity.php']);
87+
88+
$driver->addPaths([__DIR__ . '/_files/colocated/']);
89+
90+
$classes = $driver->getAllClassNames();
91+
sort($classes);
92+
93+
self::assertSame(
94+
[Entity::class, EntityFixture::class],
95+
$classes,
96+
'The driver should return class names from both the provided file path names and the retroactively added directory paths (these should not be ignored).',
97+
);
98+
}
99+
84100
/** @return Generator<string, array{string}> */
85101
public static function pathProvider(): Generator
86102
{

0 commit comments

Comments
 (0)