Passed
Push — feature/unit-tests ( 3fd24a...669eab )
by Yuichi
02:24
created

JsonStream   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 23
ccs 6
cts 11
cp 0.5455
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 16 3
1
<?php
2
3
namespace CybozuHttp\Middleware;
4
5
use GuzzleHttp\Psr7\StreamDecoratorTrait;
6
use Psr\Http\Message\StreamInterface;
7
8
/**
9
 * @author ochi51 <[email protected]>
10
 */
11
class JsonStream implements StreamInterface, \JsonSerializable
12
{
13
    use StreamDecoratorTrait;
14
15
    private $stream = null;
0 ignored issues
show
introduced by
The private property $stream is not used, and could be removed.
Loading history...
16
17
    // TODO: Specify return type as mixed when PHP 7.x support is dropped
18 1
    #[\ReturnTypeWillChange]
19
    public function jsonSerialize()
20
    {
21 1
        $contents = (string) $this->getContents();
22 1
        if ($contents === '') {
23
            return null;
24
        }
25 1
        $decodedContents = json_decode($contents, true);
26 1
        if (json_last_error() !== JSON_ERROR_NONE) {
27
            throw new \RuntimeException(
28
                'Error trying to decode response: ' .
29
                json_last_error_msg()
30
            );
31
        }
32
33 1
        return $decodedContents;
34
    }
35
}