JsonResponseHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 12.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 9
loc 73
ccs 20
cts 25
cp 0.8
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setJsonApi() 0 5 1
A addTraceToOutput() 9 9 2
A handle() 0 24 3
A contentType() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace Whoops\Handler;
8
9
use Whoops\Exception\Formatter;
10
11
/**
12
 * Catches an exception and converts it to a JSON
13
 * response. Additionally can also return exception
14
 * frames for consumption by an API.
15
 */
16
class JsonResponseHandler extends Handler
17
{
18
    /**
19
     * @var bool
20
     */
21
    private $returnFrames = false;
22
23
    /**
24
     * @var bool
25
     */
26
    private $jsonApi = false;
27
28
    /**
29
     * Returns errors[[]] instead of error[] to be in compliance with the json:api spec
30
     * @param bool $jsonApi Default is false
31
     * @return static
32
     */
33
    public function setJsonApi($jsonApi = false)
34
    {
35
        $this->jsonApi = (bool) $jsonApi;
36
        return $this;
37
    }
38
39
    /**
40
     * @param  bool|null  $returnFrames
41
     * @return bool|static
42
     */
43 3 View Code Duplication
    public function addTraceToOutput($returnFrames = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45 3
        if (func_num_args() == 0) {
46 3
            return $this->returnFrames;
47
        }
48
49 3
        $this->returnFrames = (bool) $returnFrames;
50 3
        return $this;
51
    }
52
53
    /**
54
     * @return int
55
     */
56 3
    public function handle()
57
    {
58 3
        if ($this->jsonApi === true) {
59
            $response = [
60
                'errors' => [
61 1
                    Formatter::formatExceptionAsDataArray(
62 1
                        $this->getInspector(),
63 1
                        $this->addTraceToOutput()
0 ignored issues
show
Bug introduced by
It seems like $this->addTraceToOutput() targeting Whoops\Handler\JsonRespo...ler::addTraceToOutput() can also be of type this<Whoops\Handler\JsonResponseHandler>; however, Whoops\Exception\Formatt...tExceptionAsDataArray() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
64 1
                    ),
65
                ]
66 1
            ];
67 1
        } else {
68
            $response = [
69 2
                'error' => Formatter::formatExceptionAsDataArray(
70 2
                    $this->getInspector(),
71 2
                    $this->addTraceToOutput()
0 ignored issues
show
Bug introduced by
It seems like $this->addTraceToOutput() targeting Whoops\Handler\JsonRespo...ler::addTraceToOutput() can also be of type this<Whoops\Handler\JsonResponseHandler>; however, Whoops\Exception\Formatt...tExceptionAsDataArray() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
72 2
                ),
73 2
            ];
74
        }
75
76 3
        echo json_encode($response, defined('JSON_PARTIAL_OUTPUT_ON_ERROR') ? JSON_PARTIAL_OUTPUT_ON_ERROR : 0);
77
78 3
        return Handler::QUIT;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function contentType()
85
    {
86
        return 'application/json';
87
    }
88
}
89