-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
PHP Version
8.3
CodeIgniter4 Version
4.6.3
CodeIgniter4 Installation Method
Composer (as dependency to an existing project)
Which operating systems have you tested for this bug?
macOS
Which server did you use?
cli-server (PHP built-in webserver)
Database
MySQL 8
What happened?
The user guide shows calling failValidationErrors($validation->getErrors())
directly, which naturally suggests passing $this->validator->getErrors()
from the Validation library. However, Validation::getErrors()
returns array<string, string>
(field => message), while ResponseTrait::failValidationErrors()
is typed to accept array<int, string>|string
(a list of messages or a single string). This causes static analysis errors when following the docs literally.
Steps to Reproduce
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
class Users extends Controller
{
use ResponseTrait;
public function create()
{
$rules = ['email' => 'required|valid_email'];
if (! $this->validate($rules)) {
// From docs pattern
return $this->failValidationErrors($this->validator->getErrors());
}
return $this->respondCreated();
}
}
Run PHPStan on the controller. It reports:
“Parameter #1 $errors of method …::failValidationErrors() expects list<string>|string, array<string, string> given.”
Actual
Static analysis fails when passing the associative array returned by Validation::getErrors() to failValidationErrors(). The method’s phpdoc/API signature requires array<int, string>|string, i.e., a list.
Expected Output
Following the user guide example should type-check cleanly without additional transformations.
- Update ResponseTrait::failValidationErrors() parameter type to accept associative arrays as well, e.g. array<array-key, string>|string (or array<int|string, string>|string).
- Update the user guide example to pass a list of messages, e.g. array_values($this->validator->getErrors()), and explicitly note the expected shape.
- Align the API reference and phpdoc with whichever approach is chosen so that docs and types match.
Anything else?
References: