Completed
Push — master ( 4ca44a...67b898 )
by Denis
117:55 queued 114:41
created

JsonResponseHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.5%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 5
c 3
b 1
f 2
lcom 1
cbo 3
dl 9
loc 42
ccs 14
cts 16
cp 0.875
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addTraceToOutput() 9 9 2
A handle() 0 17 3

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
     * @param  bool|null  $returnFrames
25
     * @return bool|$this
26
     */
27 2 View Code Duplication
    public function addTraceToOutput($returnFrames = null)
28
    {
29 2
        if (func_num_args() == 0) {
30 2
            return $this->returnFrames;
31
        }
32
33 2
        $this->returnFrames = (bool) $returnFrames;
34 2
        return $this;
35
    }
36
37
    /**
38
     * @return int
39
     */
40 2
    public function handle()
41
    {
42
        $response = array(
43 2
            'error' => Formatter::formatExceptionAsDataArray(
44 2
                $this->getInspector(),
45 2
                $this->addTraceToOutput()
46 2
            ),
47 2
        );
48
49 2
        if (\Whoops\Util\Misc::canSendHeaders()) {
50
            header('Content-Type: application/json');
51
        }
52
53 2
        echo json_encode($response, defined('JSON_PARTIAL_OUTPUT_ON_ERROR') ? JSON_PARTIAL_OUTPUT_ON_ERROR : 0);
54
55 2
        return Handler::QUIT;
56
    }
57
}
58