From f12ebf405e924bf909e3dfb4410cf5a7ada16eb5 Mon Sep 17 00:00:00 2001 From: Caleb White Date: Wed, 19 Jun 2024 13:24:11 -0500 Subject: [PATCH] fix: improve performance and robustness of Relation::getMorphAlias() --- src/Illuminate/Database/Eloquent/Relations/Relation.php | 4 ++-- tests/Database/DatabaseEloquentRelationTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index cba4ca2afed0..d94671d4f777 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -505,11 +505,11 @@ public static function getMorphedModel($alias) * Get the alias associated with a custom polymorphic class. * * @param string $className - * @return int|string|null + * @return int|string */ public static function getMorphAlias(string $className) { - return array_flip(static::$morphMap)[$className] ?? null; + return array_search($className, static::$morphMap, strict: true) ?: $className; } /** diff --git a/tests/Database/DatabaseEloquentRelationTest.php b/tests/Database/DatabaseEloquentRelationTest.php index 47bd92710bcf..0fac8f8d3b7f 100755 --- a/tests/Database/DatabaseEloquentRelationTest.php +++ b/tests/Database/DatabaseEloquentRelationTest.php @@ -239,8 +239,8 @@ public function testGetMorphAlias() { Relation::morphMap(['user' => 'App\User']); - $this->assertEquals('user', Relation::getMorphAlias('App\User')); - $this->assertNull(Relation::getMorphAlias('Does\Not\Exist')); + $this->assertSame('user', Relation::getMorphAlias('App\User')); + $this->assertSame('Does\Not\Exist', Relation::getMorphAlias('Does\Not\Exist')); } public function testWithoutRelations()