Issues (14)

src/Traits/APIResponses.php (1 issue)

1
<?php
2
3
namespace InnoFlash\LaraStart\Traits;
4
5
use Illuminate\Routing\ResponseFactory;
6
7
trait APIResponses
8
{
9
    /**
10
     * The json response for success responses.
11
     *
12
     * @param $message
13
     * @param  array  $data
14
     * @param  int  $statusCode
15
     *
16
     * @return mixed
17
     */
18
    public function successResponse($message, $data = [], int $statusCode = 200)
19
    {
20
        $responseData = [
21
            'success' => true,
22
            'message' => $message,
23
        ];
24
25
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
26
            $responseData['data'] = $data;
27
        }
28
29
        return ResponseFactory::successResponse($message, $responseData, $statusCode);
30
    }
31
}
32