Skip to content

Commit

Permalink
Merge pull request #1105 from MrFrost-Nv27/userentity
Browse files Browse the repository at this point in the history
fix: change hardcoded user entity with declared return type
  • Loading branch information
kenjis authored May 4, 2024
2 parents e7684c4 + 34806e5 commit 1834fb4
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Entities\UserIdentity;
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
use CodeIgniter\Shield\Exceptions\LogicException;
use CodeIgniter\Shield\Exceptions\ValidationException;
use Faker\Generator;

Expand Down Expand Up @@ -164,7 +165,9 @@ public function addToDefaultGroup(User $user): void

public function fake(Generator &$faker): User
{
return new User([
$this->checkReturnType();

return new $this->returnType([
'username' => $faker->unique()->userName(),
'active' => true,
]);
Expand Down Expand Up @@ -226,7 +229,9 @@ public function findByCredentials(array $credentials): ?User
$password_hash = $data['password_hash'];
unset($data['password_hash']);

$user = new User($data);
$this->checkReturnType();

$user = new $this->returnType($data);
$user->email = $email;
$user->password_hash = $password_hash;
$user->syncOriginal();
Expand Down Expand Up @@ -383,4 +388,11 @@ public function updateActiveDate(User $user): void
->where('id', $user->id)
->update();
}

private function checkReturnType(): void
{
if (! is_a($this->returnType, User::class, true)) {
throw new LogicException('Return type must be a subclass of ' . User::class);
}
}
}

0 comments on commit 1834fb4

Please sign in to comment.