1313
1414namespace CodeIgniter \API ;
1515
16+ use CodeIgniter \Format \Format ;
1617use CodeIgniter \Format \FormatterInterface ;
1718use CodeIgniter \HTTP \IncomingRequest ;
19+ use CodeIgniter \HTTP \RequestInterface ;
1820use CodeIgniter \HTTP \ResponseInterface ;
1921
2022/**
2123 * Provides common, more readable, methods to provide
2224 * consistent HTTP responses under a variety of common
2325 * situations when working as an API.
2426 *
25- * @property bool $stringAsHtml Whether to treat string data as HTML in JSON response.
26- * Setting `true` is only for backward compatibility.
27+ * @property RequestInterface $request
28+ * @property ResponseInterface $response
29+ * @property bool $stringAsHtml Whether to treat string data as HTML in JSON response.
30+ * Setting `true` is only for backward compatibility.
2731 */
2832trait ResponseTrait
2933{
@@ -84,7 +88,7 @@ trait ResponseTrait
8488 * Provides a single, simple method to return an API response, formatted
8589 * to match the requested format, with proper content-type and status code.
8690 *
87- * @param array|string|null $data
91+ * @param array<string, mixed> |string|null $data
8892 *
8993 * @return ResponseInterface
9094 */
@@ -118,9 +122,9 @@ protected function respond($data = null, ?int $status = null, string $message =
118122 /**
119123 * Used for generic failures that no custom methods exist for.
120124 *
121- * @param array |string $messages
122- * @param int $status HTTP status code
123- * @param string|null $code Custom, API-specific, error code
125+ * @param list<string> |string $messages
126+ * @param int $status HTTP status code
127+ * @param string|null $code Custom, API-specific, error code
124128 *
125129 * @return ResponseInterface
126130 */
@@ -146,7 +150,7 @@ protected function fail($messages, int $status = 400, ?string $code = null, stri
146150 /**
147151 * Used after successfully creating a new resource.
148152 *
149- * @param array|string|null $data
153+ * @param array<string, mixed> |string|null $data
150154 *
151155 * @return ResponseInterface
152156 */
@@ -158,7 +162,7 @@ protected function respondCreated($data = null, string $message = '')
158162 /**
159163 * Used after a resource has been successfully deleted.
160164 *
161- * @param array|string|null $data
165+ * @param array<string, mixed> |string|null $data
162166 *
163167 * @return ResponseInterface
164168 */
@@ -170,7 +174,7 @@ protected function respondDeleted($data = null, string $message = '')
170174 /**
171175 * Used after a resource has been successfully updated.
172176 *
173- * @param array|string|null $data
177+ * @param array<string, mixed> |string|null $data
174178 *
175179 * @return ResponseInterface
176180 */
@@ -287,15 +291,17 @@ protected function failServerError(string $description = 'Internal Server Error'
287291 * Handles formatting a response. Currently, makes some heavy assumptions
288292 * and needs updating! :)
289293 *
290- * @param array|string|null $data
294+ * @param array<string, mixed> |string|null $data
291295 *
292296 * @return string|null
293297 */
294298 protected function format ($ data = null )
295299 {
300+ /** @var Format $format */
296301 $ format = service ('format ' );
297302
298- $ mime = ($ this ->format === null ) ? $ format ->getConfig ()->supportedResponseFormats [0 ]
303+ $ mime = $ this ->format === null
304+ ? $ format ->getConfig ()->supportedResponseFormats [0 ]
299305 : "application/ {$ this ->format }" ;
300306
301307 // Determine correct response type through content negotiation if not explicitly declared
@@ -313,14 +319,10 @@ protected function format($data = null)
313319 $ this ->response ->setContentType ($ mime );
314320
315321 // if we don't have a formatter, make one
316- if (! isset ($ this ->formatter )) {
317- // if no formatter, use the default
318- $ this ->formatter = $ format ->getFormatter ($ mime );
319- }
322+ $ this ->formatter ??= $ format ->getFormatter ($ mime );
320323
321324 $ asHtml = $ this ->stringAsHtml ?? false ;
322325
323- // Returns as HTML.
324326 if (
325327 ($ mime === 'application/json ' && $ asHtml && is_string ($ data ))
326328 || ($ mime !== 'application/json ' && is_string ($ data ))
@@ -338,6 +340,7 @@ protected function format($data = null)
338340 if ($ mime !== 'application/json ' ) {
339341 // Recursively convert objects into associative arrays
340342 // Conversion not required for JSONFormatter
343+ /** @var array<string, mixed>|string|null $data */
341344 $ data = json_decode (json_encode ($ data ), true );
342345 }
343346
@@ -353,7 +356,7 @@ protected function format($data = null)
353356 */
354357 protected function setResponseFormat (?string $ format = null )
355358 {
356- $ this ->format = ( $ format === null ) ? null : strtolower ($ format );
359+ $ this ->format = $ format === null ? null : strtolower ($ format );
357360
358361 return $ this ;
359362 }
0 commit comments