From fcf0444d193bee5c6589635d02c749274de094aa Mon Sep 17 00:00:00 2001 From: MrFrost-Nv27 Date: Tue, 23 Apr 2024 18:34:11 +0700 Subject: [PATCH 1/5] change hardcoded user entity with declared return type --- src/Models/UserModel.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index bc48ad796..45a3f2442 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -164,7 +164,7 @@ public function addToDefaultGroup(User $user): void public function fake(Generator &$faker): User { - return new User([ + return new $this->returnType([ 'username' => $faker->unique()->userName(), 'active' => true, ]); @@ -226,7 +226,7 @@ public function findByCredentials(array $credentials): ?User $password_hash = $data['password_hash']; unset($data['password_hash']); - $user = new User($data); + $user = new $this->returnType($data); $user->email = $email; $user->password_hash = $password_hash; $user->syncOriginal(); From d619d0412c6b4817766d033008a3a746bb6b3017 Mon Sep 17 00:00:00 2001 From: Nova Adi Saputra <70581926+MrFrost-Nv27@users.noreply.github.com> Date: Thu, 2 May 2024 22:03:37 +0000 Subject: [PATCH 2/5] add error thrown before returning with declared returnType --- src/Models/UserModel.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 45a3f2442..f576bbc45 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -21,6 +21,7 @@ use CodeIgniter\Shield\Exceptions\InvalidArgumentException; use CodeIgniter\Shield\Exceptions\ValidationException; use Faker\Generator; +use LogicException; /** * @phpstan-consistent-constructor @@ -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, @@ -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; From b9d044147014f09f65fd44c8725295a78fb63909 Mon Sep 17 00:00:00 2001 From: Nova Adi Saputra <70581926+MrFrost-Nv27@users.noreply.github.com> Date: Thu, 2 May 2024 22:08:47 +0000 Subject: [PATCH 3/5] change comment and signing commit --- src/Models/UserModel.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index f576bbc45..c9c9bce47 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -166,7 +166,7 @@ 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); + throw new LogicException('Model Return type must be a subclass of ' . User::class); } return new $this->returnType([ @@ -232,7 +232,7 @@ public function findByCredentials(array $credentials): ?User unset($data['password_hash']); if (! is_a($this->returnType, User::class, true)) { - throw new LogicException('Return type must be a subclass of ' . User::class); + throw new LogicException('Model Return type must be a subclass of ' . User::class); } $user = new $this->returnType($data); From f9d5dd1e87dc80e099c1278ffb280beefe21cbec Mon Sep 17 00:00:00 2001 From: MrFrost-Nv27 Date: Fri, 3 May 2024 05:13:26 +0700 Subject: [PATCH 4/5] signing commit --- src/Models/UserModel.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index c9c9bce47..f576bbc45 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -166,7 +166,7 @@ public function addToDefaultGroup(User $user): void public function fake(Generator &$faker): User { if (! is_a($this->returnType, User::class, true)) { - throw new LogicException('Model Return type must be a subclass of ' . User::class); + throw new LogicException('Return type must be a subclass of ' . User::class); } return new $this->returnType([ @@ -232,7 +232,7 @@ public function findByCredentials(array $credentials): ?User unset($data['password_hash']); if (! is_a($this->returnType, User::class, true)) { - throw new LogicException('Model Return type must be a subclass of ' . User::class); + throw new LogicException('Return type must be a subclass of ' . User::class); } $user = new $this->returnType($data); From 34806e55088ba1a71fbcbf60316d306b6ccda76e Mon Sep 17 00:00:00 2001 From: Nova Adi Saputra <70581926+MrFrost-Nv27@users.noreply.github.com> Date: Fri, 3 May 2024 00:47:48 +0000 Subject: [PATCH 5/5] create checkreturnType method --- src/Models/UserModel.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index f576bbc45..27020b0b8 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -19,9 +19,9 @@ 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; -use LogicException; /** * @phpstan-consistent-constructor @@ -165,9 +165,7 @@ 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); - } + $this->checkReturnType(); return new $this->returnType([ 'username' => $faker->unique()->userName(), @@ -231,9 +229,7 @@ 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); - } + $this->checkReturnType(); $user = new $this->returnType($data); $user->email = $email; @@ -392,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); + } + } }