Completed
Pull Request — master (#128)
by Fabien
10:28 queued 27s
created

Formatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A formatException() 0 12 3
A formatRequest() 0 4 1
A formatResponse() 0 4 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