Completed
Pull Request — master (#128)
by Fabien
08:06
created

Formatter::formatResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Exception;
6
use Http\Client\Exception\HttpException;
7
use Http\Client\Exception\TransferException;
8
use Http\Message\Formatter as FormatterInterface;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
class Formatter implements FormatterInterface
13
{
14
    /**
15
     * @var Formatter
16
     */
17
    private $formatter;
18
19
    /**
20
     * @param FormatterInterface $formatter
21
     */
22
    public function __construct(FormatterInterface $formatter)
23
    {
24
        $this->formatter = $formatter;
0 ignored issues
show
Documentation Bug introduced by
$formatter is of type object<Http\Message\Formatter>, but the property $formatter was declared to be of type object<Http\HttplugBundle\Collector\Formatter>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
25
    }
26
27
    /**
28
     * Formats an exception.
29
     *
30
     * @param Exception $exception
31
     *
32
     * @return string
33
     */
34
    public function formatException(Exception $exception)
35
    {
36
        if ($exception instanceof HttpException) {
37
            return $this->formatter->formatResponse($exception->getResponse());
38
        }
39
40
        if ($exception instanceof TransferException) {
41
            return $exception->getMessage();
42
        }
43
44
        return sprintf('Unexpected exception of type "%s"', get_class($exception));
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function formatRequest(RequestInterface $request)
51
    {
52
        return $this->formatter->formatRequest($request);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function formatResponse(ResponseInterface $response)
59
    {
60
        return $this->formatter->formatResponse($response);
61
    }
62
}
63