Passed
Branch pretty-dump (f26c5c)
by Baptiste
02:47
created

Echoer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 0 21 4
A supports() 0 3 1
1
<?php declare(strict_types=1);
2
namespace Behapi\Debug\Dumper\Response;
3
4
use Psr\Http\Message\MessageInterface;
5
use Psr\Http\Message\ResponseInterface;
6
7
use Behapi\Debug\Dumper;
8
use Behapi\Debug\Dumper\UnsupportedMessage;
9
10
final class Echoer implements Dumper
11
{
12
    // 1 - key
13
    // 2 - value
14
    private const TEMPLATE = "\033[36m| \033[1m%s : \033[0;36m%s\033[0m\n";
15
16
    public function dump(MessageInterface $message, array $headers): void
17
    {
18
        if (!$this->supports($message)) {
19
            throw new UnsupportedMessage($message, ResponseInterface::class);
20
        }
21
22
        echo "\n";
23
24
        printf(self::TEMPLATE, 'Response status', "{$message->getStatusCode()} {$message->getReasonPhrase()}");
2 ignored issues
show
Bug introduced by
The method getReasonPhrase() does not exist on Psr\Http\Message\MessageInterface. It seems like you code against a sub-type of Psr\Http\Message\MessageInterface such as Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        printf(self::TEMPLATE, 'Response status', "{$message->getStatusCode()} {$message->/** @scrutinizer ignore-call */ getReasonPhrase()}");
Loading history...
Bug introduced by
The method getStatusCode() does not exist on Psr\Http\Message\MessageInterface. It seems like you code against a sub-type of Psr\Http\Message\MessageInterface such as Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        printf(self::TEMPLATE, 'Response status', "{$message->/** @scrutinizer ignore-call */ getStatusCode()} {$message->getReasonPhrase()}");
Loading history...
25
26
        foreach ($headers as $header) {
27
            printf(self::TEMPLATE, "Response {$header}", $message->getHeaderLine($header));
28
        }
29
30
        $body = (string) $message->getBody();
31
32
        if (!empty($body)) {
33
            echo "\n{$body}";
34
        }
35
36
        echo "\n";
37
    }
38
39
    public function supports(MessageInterface $message): bool
40
    {
41
        return $message instanceof ResponseInterface;
42
    }
43
}
44