Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
datamweb authored Mar 31, 2025
commit d74c099313fac7694ba572b4c504afdfb9d60347
4 changes: 3 additions & 1 deletion src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Loading