Skip to content

Commit 38fe579

Browse files
committed
Bring back byParent for dumpClassReference method
1 parent fd31255 commit 38fe579

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/CodeGenerator.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,18 @@ public function dumpAttribute(FullyQualified | string $fqcn) : string
246246
/**
247247
* Generates a class reference string (e.g., Foo::class)
248248
*/
249-
public function dumpClassReference(FullyQualified | string $fqcn, bool $import = true) : string
249+
public function dumpClassReference(FullyQualified | string $fqcn, bool $import = true, bool $byParent = false) : string
250250
{
251-
return sprintf('%s::class', $import ? $this->import($fqcn) : '\\' . (string) $fqcn);
251+
$fqcn = FullyQualified::maybeFromString($fqcn);
252+
253+
return sprintf(
254+
'%s::class',
255+
match (true) {
256+
$import && $byParent => $this->importByParent($fqcn),
257+
$import => $this->import($fqcn),
258+
default => '\\' . $fqcn,
259+
},
260+
);
252261
}
253262

254263
/**

tests/CodeGeneratorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,24 @@ public function testDumpClassReference() : void
363363
);
364364
}
365365

366+
public function testDumpClassReferenceByParent() : void
367+
{
368+
self::assertSame(
369+
'Models\\User::class',
370+
$this->generator->dumpClassReference('App\\Models\\User', byParent: true),
371+
);
372+
}
373+
366374
public function testDumpClassReferenceWithoutImport() : void
367375
{
368376
self::assertSame(
369377
'\\App\\Models\\User::class',
370378
$this->generator->dumpClassReference('App\\Models\\User', false),
371379
);
380+
self::assertSame(
381+
'\\App\\Models\\User::class',
382+
$this->generator->dumpClassReference('\\App\\Models\\User', false),
383+
);
372384
}
373385

374386
public function testMaybeNowDocWithSingleLine() : void

0 commit comments

Comments
 (0)