Skip to content

Commit

Permalink
add error thrown before returning with declared returnType
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFrost-Nv27 committed May 3, 2024
1 parent fcf0444 commit d619d04
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
use CodeIgniter\Shield\Exceptions\ValidationException;
use Faker\Generator;
use LogicException;

/**
* @phpstan-consistent-constructor
Expand Down Expand Up @@ -164,6 +165,10 @@ public function addToDefaultGroup(User $user): void

public function fake(Generator &$faker): User
{
if (! is_a($this->returnType, User::class, true)) {
throw new LogicException('Return type must be a subclass of ' . User::class);
}

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

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

$user = new $this->returnType($data);
$user->email = $email;
$user->password_hash = $password_hash;
Expand Down

0 comments on commit d619d04

Please sign in to comment.