Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}