ModelHydrator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 13.04 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 3
loc 23
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 3 20 4

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
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace FAPI\Fortnox\Hydrator;
11
12
use FAPI\Fortnox\Exception\HydrationException;
13
use FAPI\Fortnox\Model\CreatableFromArray;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * Hydrate an HTTP response to domain object.
18
 *
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class ModelHydrator implements Hydrator
22
{
23
    public function hydrate(ResponseInterface $response, string $class)
24
    {
25
        $body = $response->getBody()->__toString();
26 View Code Duplication
        if (0 !== \mb_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...
27
            throw new HydrationException('The ModelHydrator cannot hydrate response with Content-Type:'.$response->getHeaderLine('Content-Type'));
28
        }
29
30
        $data = \json_decode($body, true);
31
        if (\JSON_ERROR_NONE !== \json_last_error()) {
32
            throw new HydrationException(\sprintf('Error (%d) when trying to json_decode response', \json_last_error()));
33
        }
34
35
        if (\is_subclass_of($class, CreatableFromArray::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \FAPI\Fortnox\Model\CreatableFromArray::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
36
            $object = \call_user_func($class.'::createFromArray', $data);
37
        } else {
38
            $object = new $class($data);
39
        }
40
41
        return $object;
42
    }
43
}
44