ProductCollection   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 107
loc 107
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 2
B createFromArray() 32 32 7
A getPage() 4 4 1
A getLimit() 4 4 1
A getPages() 4 4 1
A getTotal() 4 4 1
A getItems() 4 4 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
/*
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\Sylius\Model\Product;
11
12
use FAPI\Sylius\Model\CreatableFromArray;
13
14
/**
15
 * @author Kasim Taskin <[email protected]>
16
 */
17 View Code Duplication
final class ProductCollection implements CreatableFromArray
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
18
{
19
    /**
20
     * @var int
21
     */
22
    private $page;
23
24
    /**
25
     * @var int
26
     */
27
    private $limit;
28
29
    /**
30
     * @var int
31
     */
32
    private $pages;
33
34
    /**
35
     * @var int
36
     */
37
    private $total;
38
39
    /**
40
     * @var Product[]
41
     */
42
    private $items;
43
44
    private function __construct(
45
        int $page,
46
        int $limit,
47
        int $pages,
48
        int $total,
49
        array $items
50
    ) {
51
        $this->page = $page;
52
        $this->limit = $limit;
53
        $this->pages = $pages;
54
        $this->total = $total;
55
        if ([] !== $items) {
56
            $this->items = $items;
57
        }
58
    }
59
60
    /**
61
     * @return ProductCollection
62
     */
63
    public static function createFromArray(array $data): self
64
    {
65
        $page = -1;
66
        if (isset($data['page'])) {
67
            $page = $data['page'];
68
        }
69
70
        $limit = -1;
71
        if (isset($data['limit'])) {
72
            $limit = $data['limit'];
73
        }
74
75
        $pages = -1;
76
        if (isset($data['pages'])) {
77
            $pages = $data['pages'];
78
        }
79
80
        $total = -1;
81
        if (isset($data['total'])) {
82
            $total = $data['total'];
83
        }
84
85
        /** @var Product[] $items */
86
        $items = [];
87
        if (isset($data['_embedded'], $data['_embedded']['items'])) {
88
            foreach ($data['_embedded']['items'] as $item) {
89
                $items[] = Product::createFromArray($item);
90
            }
91
        }
92
93
        return new self($page, $limit, $pages, $total, $items);
94
    }
95
96
    public function getPage(): int
97
    {
98
        return $this->page;
99
    }
100
101
    public function getLimit(): int
102
    {
103
        return $this->limit;
104
    }
105
106
    public function getPages(): int
107
    {
108
        return $this->pages;
109
    }
110
111
    public function getTotal(): int
112
    {
113
        return $this->total;
114
    }
115
116
    /**
117
     * @return Product[]
118
     */
119
    public function getItems(): array
120
    {
121
        return $this->items;
122
    }
123
}
124