ArrayHydrator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 13.64 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 3 13 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
declare(strict_types=1);
4
/*
5
 * This software may be modified and distributed under the terms
6
 * of the MIT license. See the LICENSE file for details.
7
 */
8
9
namespace Billogram\Hydrator;
10
11
use Billogram\Exception\HydrationException;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 * Hydrate an HTTP response to array.
16
 *
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
final class ArrayHydrator implements Hydrator
20
{
21
    /**
22
     * @param ResponseInterface $response
23
     * @param string            $class
24
     *
25
     * @return array
26
     */
27
    public function hydrate(ResponseInterface $response, string $class): array
28
    {
29
        $body = $response->getBody()->__toString();
30 View Code Duplication
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
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...
31
            throw new HydrationException('The ArrayHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type'));
32
        }
33
        $content = json_decode($body, true);
34
        if (JSON_ERROR_NONE !== json_last_error()) {
35
            throw new HydrationException(sprintf('Error (%d) when trying to json_decode response', json_last_error()));
36
        }
37
38
        return $content;
39
    }
40
}
41