Skip to content

Commit 06109f3

Browse files
authored
Merge pull request #12002 from greg0ire/relax-type-declarations
Make proxyDir and proxyNs nullable and optional
2 parents c74df3f + 06a9ef1 commit 06109f3

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Proxy/ProxyFactory.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public function __serialize(): array
132132
/** @var array<class-string, Closure> */
133133
private array $proxyFactories = [];
134134

135+
private readonly string $proxyDir;
136+
private readonly string $proxyNs;
137+
135138
/**
136139
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is
137140
* connected to the given <tt>EntityManager</tt>.
@@ -143,8 +146,8 @@ public function __serialize(): array
143146
*/
144147
public function __construct(
145148
private readonly EntityManagerInterface $em,
146-
private readonly string $proxyDir,
147-
private readonly string $proxyNs,
149+
string|null $proxyDir = null,
150+
string|null $proxyNs = null,
148151
bool|int $autoGenerate = self::AUTOGENERATE_NEVER,
149152
) {
150153
if (! $proxyDir && ! $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
@@ -159,6 +162,17 @@ public function __construct(
159162
throw ORMInvalidArgumentException::invalidAutoGenerateMode($autoGenerate);
160163
}
161164

165+
if ($proxyDir === null && $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
166+
$proxyDir = '';
167+
}
168+
169+
if ($proxyNs === null && $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
170+
$proxyNs = '';
171+
}
172+
173+
$this->proxyDir = $proxyDir;
174+
$this->proxyNs = $proxyNs;
175+
162176
$this->uow = $em->getUnitOfWork();
163177
$this->autoGenerate = (int) $autoGenerate;
164178
$this->identifierFlattener = new IdentifierFlattener($this->uow, $em->getMetadataFactory());

tests/Tests/ORM/Proxy/ProxyFactoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
2121
use Doctrine\Tests\OrmTestCase;
2222
use PHPUnit\Framework\Attributes\Group;
23+
use PHPUnit\Framework\Attributes\RequiresPhp;
24+
use ReflectionClass;
2325
use ReflectionProperty;
2426
use stdClass;
2527

@@ -221,6 +223,24 @@ public function testProxyClonesParentFields(): void
221223
self::assertSame(1000, $cloned->getSalary(), 'Expect properties on the CompanyEmployee class to be cloned');
222224
self::assertSame('Bob', $cloned->getName(), 'Expect properties on the CompanyPerson class to be cloned');
223225
}
226+
227+
#[RequiresPhp('8.4')]
228+
public function testProxyFactoryAcceptsNullProxyArgsWhenNativeLazyObjectsAreEnabled(): void
229+
{
230+
$this->emMock->getConfiguration()->enableNativeLazyObjects(true);
231+
$this->proxyFactory = new ProxyFactory(
232+
$this->emMock,
233+
null,
234+
null,
235+
);
236+
$proxy = $this->proxyFactory->getProxy(
237+
ECommerceFeature::class,
238+
['id' => 42],
239+
);
240+
$reflection = new ReflectionClass($proxy);
241+
242+
self::assertTrue($reflection->isUninitializedLazyObject($proxy));
243+
}
224244
}
225245

226246
abstract class AbstractClass

0 commit comments

Comments
 (0)