Passed
Push — master ( 9ff768...b736eb )
by Laurens
02:33
created

CategoryParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 29.73 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 11
loc 37
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromResponse() 11 11 2
A parseArray() 0 10 2
A createFromArray() 0 11 1

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
namespace LauLamanApps\IzettleApi\Client\Product;
6
7
use DateTime;
8
use LauLamanApps\IzettleApi\API\Product\Category;
9
use LauLamanApps\IzettleApi\API\Product\CategoryCollection;
10
use Psr\Http\Message\ResponseInterface;
11
use Ramsey\Uuid\Uuid;
12
13
final class CategoryParser
14
{
15 2 View Code Duplication
    public static function createFromResponse(ResponseInterface $response): array
16
    {
17 2
        $categories = [];
18 2
        $data = json_decode($response->getBody()->getContents(), true);
19
20 2
        foreach ($data as $category) {
21 1
            $categories[] = self::createFromArray($category);
22
        }
23
24 2
        return $categories;
25
    }
26
27 4
    public static function parseArray($categories)
28
    {
29 4
        $collection = new CategoryCollection();
30
31 4
        foreach ($categories as $category) {
32 2
            $collection->add(self::createFromArray($category));
33
        }
34
35 4
        return $collection;
36
    }
37
38 3
    private static function createFromArray(array $category): Category
39
    {
40 3
        return Category::create(
41 3
            Uuid::fromString($category['uuid']),
42 3
            $category['name'],
43 3
            $category['etag'],
44 3
            new DateTime($category['updated']),
45 3
            Uuid::fromString($category['updatedBy']),
46 3
            new DateTime($category['created'])
47
        );
48
    }
49
}
50