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

Formatter::formatException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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
    /**
49
     * {@inheritdoc}
50
     */
51
    public function formatRequest(RequestInterface $request)
52
    {
53
        return $this->formatter->formatRequest($request);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function formatResponse(ResponseInterface $response)
60
    {
61
        return $this->formatter->formatResponse($response);
62
    }
63
}
64