diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index bc48ad796..27020b0b8 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -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; @@ -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, ]); @@ -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(); @@ -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); + } + } }