Skip to content

Commit d74c099

Browse files
authored
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.
1 parent 568a130 commit d74c099

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

src/Models/UserModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ public function fake(Generator &$faker): User
182182
*/
183183
public function findById($id): ?User
184184
{
185-
return $this->find($id);
185+
$result = $this->find($id);
186+
187+
return $result instanceof User ? $result : null;
186188
}
187189

188190
/**

0 commit comments

Comments
 (0)