Code Duplication    Length = 29-31 lines in 2 locations

app/Ship/core/Exceptions/heimdal-package/src/Formatters/UnprocessableEntityHttpExceptionFormatter.php 1 location

@@ 11-41 (lines=31) @@
8
9
class UnprocessableEntityHttpExceptionFormatter extends BaseFormatter
10
{
11
    public function format(JsonResponse $response, Exception $e, array $reporterResponses)
12
    {
13
        $response->setStatusCode(422);
14
        
15
        // Laravel validation errors will return JSON string
16
        $decoded = json_decode($e->getMessage(), true);
17
        // Message was not valid JSON
18
        // This occurs when we throw UnprocessableEntityHttpExceptions
19
        if (json_last_error() !== JSON_ERROR_NONE) {
20
            // Mimick the structure of Laravel validation errors
21
            $decoded = [[$e->getMessage()]];
22
        }
23
24
        // Laravel errors are formatted as {"field": [/*errors as strings*/]}
25
        $data = array_reduce($decoded, function ($carry, $item) use ($e) {
26
            return array_merge($carry, array_map(function ($current) use ($e) {
27
                return [
28
                    'status' => '422',
29
                    'code' => $e->getCode(),
30
                    'title' => 'Validation error',
31
                    'detail' => $current
32
                ];
33
            }, $item));
34
        }, []);
35
36
        $response->setData([
37
            'errors' => $data
38
        ]);
39
40
        return $response;
41
    }
42
}
43

app/Ship/Exceptions/Formatters/UnprocessableEntityHttpExceptionFormatter.php 1 location

@@ 31-59 (lines=29) @@
28
     *
29
     * @return  array
30
     */
31
    public function responseData(Exception $exception, JsonResponse $response)
32
    {
33
        // Laravel validation errors will return JSON string
34
        $decoded = json_decode($exception->getMessage(), true);
35
        // Message was not valid JSON
36
        // This occurs when we throw UnprocessableEntityHttpExceptions
37
        if (json_last_error() !== JSON_ERROR_NONE) {
38
            // Mimic the structure of Laravel validation errors
39
            $decoded = [[$exception->getMessage()]];
40
        }
41
42
        // Laravel errors are formatted as {"field": [/*errors as strings*/]}
43
        $responseData = array_reduce($decoded, function ($carry, $item) use ($exception) {
44
            return array_merge($carry, array_map(function ($current) use ($exception) {
45
                return [
46
                    'status' => self::STATUS_CODE,
47
                    'code'   => $exception->getCode(),
48
                    'title'  => 'Validation error',
49
                    'detail' => $current
50
                ];
51
            }, $item));
52
        }, []);
53
54
        return [
55
            'code'    => $exception->getCode(),
56
            'message' => $exception->getMessage(),
57
            'errors' => $responseData,
58
        ];
59
    }
60
61
62
    /**