From d74c099313fac7694ba572b4c504afdfb9d60347 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 31 Mar 2025 07:27:25 +0330 Subject: [PATCH] fix: Ensure `findById` returns `null` instead of an array when ID is `null` Although the phpdocs states that the ID ($id) should not be `null`, in some cases, data may be retrieved from the database where the ID is set to `null`. When `null` is passed to the `find($id)` method, instead of returning null or a User object, it returns an empty array ([]). This leads to a type mismatch with the method signature, which expects a return type of `?User`, causing the error: ``` CodeIgniter\Shield\Models\UserModel::findById(): Return value must be of type ?CodeIgniter\Shield\Entities\User, array returned ``` This change ensures that the method always adheres to the expected return type (?User), preventing unexpected runtime errors. --- src/Models/UserModel.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index e4a002fbb..123285ece 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -182,7 +182,9 @@ public function fake(Generator &$faker): User */ public function findById($id): ?User { - return $this->find($id); + $result = $this->find($id); + + return $result instanceof User ? $result : null; } /**