Completed
Pull Request — master (#128)
by Fabien
18:17 queued 08:23
created

Formatter::formatRequest()   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
/**
13
 * @author Fabien Bourigault <[email protected]>
14
 *
15
 * @internal
16
 */
17
class Formatter implements FormatterInterface
18
{
19
    /**
20
     * @var Formatter
21
     */
22
    private $formatter;
23
24
    /**
25
     * @param FormatterInterface $formatter
26
     */
27
    public function __construct(FormatterInterface $formatter)
28
    {
29
        $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...
30
    }
31
32
    /**
33
     * Formats an exception.
34
     *
35
     * @param Exception $exception
36
     *
37
     * @return string
38
     */
39
    public function formatException(Exception $exception)
40
    {
41
        if ($exception instanceof HttpException) {
42
            return $this->formatter->formatResponse($exception->getResponse());
43
        }
44
45
        if ($exception instanceof TransferException) {
46
            return $exception->getMessage();
47
        }
48
49
        return sprintf('Unexpected exception of type "%s"', get_class($exception));
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function formatRequest(RequestInterface $request)
56
    {
57
        return $this->formatter->formatRequest($request);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function formatResponse(ResponseInterface $response)
64
    {
65
        return $this->formatter->formatResponse($response);
66
    }
67
}
68