From 45253efbb7df25e55285c2f913ab4c653788779b Mon Sep 17 00:00:00 2001 From: Michael Dwyer Date: Tue, 12 May 2026 11:34:30 -0500 Subject: [PATCH] Fix self parameter type resolution --- src/Nelmio/Alice/Loader/Base.php | 14 +++++++++++++- tests/Nelmio/Alice/Loader/BaseTest.php | 23 +++++++++++++++++++++++ tests/Nelmio/Alice/fixtures/Group.php | 11 +++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Nelmio/Alice/Loader/Base.php b/src/Nelmio/Alice/Loader/Base.php index f0daa88f5..74265c21b 100644 --- a/src/Nelmio/Alice/Loader/Base.php +++ b/src/Nelmio/Alice/Loader/Base.php @@ -593,7 +593,19 @@ private function getClassTypeName(\ReflectionParameter $parameter) return null; } - return $type->getName(); + $typeName = $type->getName(); + + if ('self' === strtolower($typeName)) { + return $parameter->getDeclaringClass()->getName(); + } + + if ('parent' === strtolower($typeName)) { + $parentClass = $parameter->getDeclaringClass()->getParentClass(); + + return $parentClass ? $parentClass->getName() : null; + } + + return $typeName; } private function process($data, array $variables) diff --git a/tests/Nelmio/Alice/Loader/BaseTest.php b/tests/Nelmio/Alice/Loader/BaseTest.php index b11751173..6a9bf3a76 100644 --- a/tests/Nelmio/Alice/Loader/BaseTest.php +++ b/tests/Nelmio/Alice/Loader/BaseTest.php @@ -13,6 +13,7 @@ use Nelmio\Alice\TestORM; use Nelmio\Alice\Loader\Base; +use Nelmio\Alice\fixtures\Group; use Nelmio\Alice\fixtures\User; use PHPUnit\Framework\TestCase; @@ -474,6 +475,28 @@ public function testLoadFetchesScalarIdsForClassHints() $this->assertSame($owner, $res['group0']->getOwner()); } + public function testLoadFetchesScalarIdsForSelfClassHints() + { + $relatedGroup = new Group(); + $orm = $this->createMock('Nelmio\Alice\ORMInterface'); + $orm->expects($this->once()) + ->method('find') + ->with(self::GROUP, '42') + ->willReturn($relatedGroup); + + $loader = $this->createLoader(); + $loader->setORM($orm); + $res = $loader->load(array( + self::GROUP => array( + 'group0' => array( + 'relatedGroup' => '42', + ), + ), + )); + + $this->assertSame($relatedGroup, $res['group0']->getRelatedGroup()); + } + public function testLoadParsesFakerData() { $res = $this->loadData(array( diff --git a/tests/Nelmio/Alice/fixtures/Group.php b/tests/Nelmio/Alice/fixtures/Group.php index cbb224381..c40493c1d 100644 --- a/tests/Nelmio/Alice/fixtures/Group.php +++ b/tests/Nelmio/Alice/fixtures/Group.php @@ -10,6 +10,7 @@ class Group private $members = array(); private $creationDate; private $contactEmail; + private $relatedGroup; private $supportEmails = array(); public $contactPerson; public $contactPersonName; @@ -43,6 +44,16 @@ public function setOwner(User $owner) $this->owner = $owner; } + public function getRelatedGroup() + { + return $this->relatedGroup; + } + + public function setRelatedGroup(?self $relatedGroup = null) + { + $this->relatedGroup = $relatedGroup; + } + public function getMembers() { return $this->members;