Completed
Push — develop ( 68a985...a1f82e )
by Adam
10s
created

ArrayHydrator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 5
loc 30
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 5 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace IBM\Watson\Common\Hydrator;
4
5
use IBM\Watson\Common\Exception\HydrationException;
6
use Psr\Http\Message\ResponseInterface;
7
8
final class ArrayHydrator implements HydratorInterface
9
{
10
    /**
11
     * Hydrate API response to array
12
     *
13
     * @param \Psr\Http\Message\ResponseInterface $response
14
     *
15
     * @return mixed
16
     * @throws \IBM\Watson\Common\Exception\HydrationException
17
     */
18 9
    public function hydrate(ResponseInterface $response, $class = null)
19
    {
20 9
        $body = $response->getBody()->__toString();
21 9 View Code Duplication
        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22 3
            $message = 'The ArrayHydrator cannot hydrate response with Content-Type: ';
23
24 3
            throw new HydrationException($message . $response->getHeaderLine('Content-Type'));
25
        }
26
27 6
        $content = \json_decode($body, true);
28 6
        if (JSON_ERROR_NONE !== \json_last_error()) {
29 3
            throw new HydrationException(sprintf(
30 3
                'Error (%d) when trying to json_decode response',
31 3
                \json_last_error()
32 2
            ));
33
        }
34
35 3
        return $content;
36
    }
37
}
38