Skip to content

Commit 1834fb4

Browse files
authored
Merge pull request #1105 from MrFrost-Nv27/userentity
fix: change hardcoded user entity with declared return type
2 parents e7684c4 + 34806e5 commit 1834fb4

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Models/UserModel.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CodeIgniter\Shield\Entities\User;
2020
use CodeIgniter\Shield\Entities\UserIdentity;
2121
use CodeIgniter\Shield\Exceptions\InvalidArgumentException;
22+
use CodeIgniter\Shield\Exceptions\LogicException;
2223
use CodeIgniter\Shield\Exceptions\ValidationException;
2324
use Faker\Generator;
2425

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

165166
public function fake(Generator &$faker): User
166167
{
167-
return new User([
168+
$this->checkReturnType();
169+
170+
return new $this->returnType([
168171
'username' => $faker->unique()->userName(),
169172
'active' => true,
170173
]);
@@ -226,7 +229,9 @@ public function findByCredentials(array $credentials): ?User
226229
$password_hash = $data['password_hash'];
227230
unset($data['password_hash']);
228231

229-
$user = new User($data);
232+
$this->checkReturnType();
233+
234+
$user = new $this->returnType($data);
230235
$user->email = $email;
231236
$user->password_hash = $password_hash;
232237
$user->syncOriginal();
@@ -383,4 +388,11 @@ public function updateActiveDate(User $user): void
383388
->where('id', $user->id)
384389
->update();
385390
}
391+
392+
private function checkReturnType(): void
393+
{
394+
if (! is_a($this->returnType, User::class, true)) {
395+
throw new LogicException('Return type must be a subclass of ' . User::class);
396+
}
397+
}
386398
}

0 commit comments

Comments
 (0)