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{
@@ -69,8 +73,7 @@ trait ResponseTrait
6973 * Either 'json' or 'xml'. If null is set, it will be determined through
7074 * content negotiation.
7175 *
72- * @var string|null
73- * @phpstan-var 'html'|'json'|'xml'|null
76+ * @var 'html'|'json'|'xml'|null
7477 */
7578 protected $ format = 'json ' ;
7679
@@ -85,7 +88,7 @@ trait ResponseTrait
8588 * Provides a single, simple method to return an API response, formatted
8689 * to match the requested format, with proper content-type and status code.
8790 *
88- * @param array|string|null $data
91+ * @param array<string, mixed> |string|null $data
8992 *
9093 * @return ResponseInterface
9194 */
@@ -119,9 +122,9 @@ protected function respond($data = null, ?int $status = null, string $message =
119122 /**
120123 * Used for generic failures that no custom methods exist for.
121124 *
122- * @param array |string $messages
123- * @param int $status HTTP status code
124- * @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
125128 *
126129 * @return ResponseInterface
127130 */
@@ -147,7 +150,7 @@ protected function fail($messages, int $status = 400, ?string $code = null, stri
147150 /**
148151 * Used after successfully creating a new resource.
149152 *
150- * @param array|string|null $data
153+ * @param array<string, mixed> |string|null $data
151154 *
152155 * @return ResponseInterface
153156 */
@@ -159,7 +162,7 @@ protected function respondCreated($data = null, string $message = '')
159162 /**
160163 * Used after a resource has been successfully deleted.
161164 *
162- * @param array|string|null $data
165+ * @param array<string, mixed> |string|null $data
163166 *
164167 * @return ResponseInterface
165168 */
@@ -171,7 +174,7 @@ protected function respondDeleted($data = null, string $message = '')
171174 /**
172175 * Used after a resource has been successfully updated.
173176 *
174- * @param array|string|null $data
177+ * @param array<string, mixed> |string|null $data
175178 *
176179 * @return ResponseInterface
177180 */
@@ -288,15 +291,17 @@ protected function failServerError(string $description = 'Internal Server Error'
288291 * Handles formatting a response. Currently, makes some heavy assumptions
289292 * and needs updating! :)
290293 *
291- * @param array|string|null $data
294+ * @param array<string, mixed> |string|null $data
292295 *
293296 * @return string|null
294297 */
295298 protected function format ($ data = null )
296299 {
300+ /** @var Format $format */
297301 $ format = service ('format ' );
298302
299- $ mime = ($ this ->format === null ) ? $ format ->getConfig ()->supportedResponseFormats [0 ]
303+ $ mime = $ this ->format === null
304+ ? $ format ->getConfig ()->supportedResponseFormats [0 ]
300305 : "application/ {$ this ->format }" ;
301306
302307 // Determine correct response type through content negotiation if not explicitly declared
@@ -314,14 +319,10 @@ protected function format($data = null)
314319 $ this ->response ->setContentType ($ mime );
315320
316321 // if we don't have a formatter, make one
317- if (! isset ($ this ->formatter )) {
318- // if no formatter, use the default
319- $ this ->formatter = $ format ->getFormatter ($ mime );
320- }
322+ $ this ->formatter ??= $ format ->getFormatter ($ mime );
321323
322324 $ asHtml = $ this ->stringAsHtml ?? false ;
323325
324- // Returns as HTML.
325326 if (
326327 ($ mime === 'application/json ' && $ asHtml && is_string ($ data ))
327328 || ($ mime !== 'application/json ' && is_string ($ data ))
@@ -339,6 +340,7 @@ protected function format($data = null)
339340 if ($ mime !== 'application/json ' ) {
340341 // Recursively convert objects into associative arrays
341342 // Conversion not required for JSONFormatter
343+ /** @var array<string, mixed>|string|null $data */
342344 $ data = json_decode (json_encode ($ data ), true );
343345 }
344346
@@ -348,14 +350,13 @@ protected function format($data = null)
348350 /**
349351 * Sets the format the response should be in.
350352 *
351- * @param string|null $format Response format
352- * @phpstan-param 'json'|'xml' $format
353+ * @param 'json'|'xml' $format Response format
353354 *
354355 * @return $this
355356 */
356357 protected function setResponseFormat (?string $ format = null )
357358 {
358- $ this ->format = ( $ format === null ) ? null : strtolower ($ format );
359+ $ this ->format = $ format === null ? null : strtolower ($ format );
359360
360361 return $ this ;
361362 }
0 commit comments