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 authored Jun 19, 2024
1 parent 9b9e862 commit 02a0b5f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
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 02a0b5f

Please sign in to comment.