Skip to content

Commit

Permalink
fix: improve performance and robustness of Relation::getMorphAlias()
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jun 19, 2024
1 parent 9b9e862 commit 797ee4f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public static function getMorphedModel($alias)
*/
public static function getMorphAlias(string $className)
{
return array_flip(static::$morphMap)[$className] ?? null;
return array_search($className, static::$morphMap, strict: true) ?: $className;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 797ee4f

Please sign in to comment.