These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace drupol\phpartition; |
||
4 | |||
5 | class Subset implements \Countable { |
||
6 | |||
7 | /** |
||
8 | * @var BasePartitionAlgorithm |
||
9 | */ |
||
10 | protected $algo; |
||
11 | |||
12 | /** |
||
13 | * @var SubsetItem[] |
||
14 | */ |
||
15 | protected $items; |
||
16 | |||
17 | /** |
||
18 | * @return int |
||
19 | */ |
||
20 | 2 | public function count() { |
|
21 | 2 | return count($this->getItems()); |
|
22 | } |
||
23 | |||
24 | /** |
||
25 | * @param \drupol\phpartition\SubsetItem $item |
||
26 | */ |
||
27 | 6 | public function addItem(SubsetItem $item) { |
|
28 | 6 | $this->items[$item->getKey()] = $item; |
|
29 | 6 | } |
|
30 | |||
31 | /** |
||
32 | * @param SubsetItem[] $items |
||
33 | */ |
||
34 | 2 | public function addItems(array $items = array()) { |
|
35 | 2 | foreach ($items as $item) { |
|
36 | 2 | $this->addItem($item); |
|
37 | 2 | } |
|
38 | 2 | } |
|
39 | |||
40 | /** |
||
41 | * @return SubsetItem[] |
||
42 | */ |
||
43 | 6 | public function getItems() { |
|
44 | 6 | return (array) $this->items; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param array $items |
||
49 | */ |
||
50 | 6 | public function setItems(array $items = array()) { |
|
51 | 6 | $this->items = $items; |
|
52 | 6 | } |
|
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | public function getItemsValues() { |
||
58 | $data = array(); |
||
59 | |||
60 | foreach ($this->getItems() as $item) { |
||
61 | $data[$item->getKey()] = $item->getValue(); |
||
62 | } |
||
63 | |||
64 | return $data; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return array |
||
69 | */ |
||
70 | 6 | public function getRawItems() { |
|
71 | 6 | $data = array(); |
|
72 | |||
73 | 6 | foreach ($this->getItems() as $item) { |
|
74 | 6 | $data[] = $item->getItem(); |
|
75 | 6 | } |
|
76 | |||
77 | 6 | return $data; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return int |
||
82 | */ |
||
83 | 2 | public function getWeight() { |
|
84 | 2 | $sum = 0; |
|
85 | |||
86 | 2 | foreach ((array) $this->items as $item) { |
|
87 | 2 | $sum += $item->getValue(); |
|
88 | 2 | } |
|
89 | |||
90 | 2 | return $sum; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param \drupol\phpartition\BasePartitionAlgorithm $algo |
||
95 | */ |
||
96 | 6 | public function setAlgo(BasePartitionAlgorithm $algo) { |
|
97 | 6 | $this->algo = $algo; |
|
98 | 6 | } |
|
99 | |||
100 | /** |
||
101 | * @return BasePartitionAlgorithm |
||
102 | */ |
||
103 | 6 | public function getAlgo() { |
|
104 | 6 | return $this->algo; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param SubsetItem[] $items |
||
109 | */ |
||
110 | public function substract(array $items = array()) { |
||
111 | $this->setItems(array_diff($this->getItems(), $items)); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * |
||
116 | */ |
||
117 | 6 | public function clear() { |
|
118 | 6 | $this->setItems(); |
|
119 | 6 | } |
|
120 | |||
121 | /** |
||
122 | * @param $key |
||
123 | * |
||
124 | * @return \drupol\phpartition\SubsetItem |
||
125 | */ |
||
126 | public function getItem($key) { |
||
127 | return $this->items[$key]; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param SubsetItem[] $items |
||
132 | */ |
||
133 | 2 | public function deleteItems(array $items = array()) { |
|
134 | 2 | foreach ($items as $item) { |
|
135 | 2 | $this->delete($item); |
|
136 | 2 | } |
|
137 | 2 | } |
|
138 | |||
139 | /** |
||
140 | * @param \drupol\phpartition\SubsetItem $item |
||
141 | */ |
||
142 | 2 | public function delete(SubsetItem $item) { |
|
143 | 2 | foreach ($this->items as $key => $value) { |
|
144 | 2 | if ($value->getKey() == $item->getKey()) { |
|
145 | 2 | unset($this->items[$value->getKey()]); |
|
146 | 2 | } |
|
147 | 2 | } |
|
148 | 2 | } |
|
149 | |||
150 | /** |
||
151 | * @param string $order |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | 4 | public function sortByValue($order = 'ASC') { |
|
156 | 4 | $data = $this->getItems(); |
|
157 | |||
158 | 4 | if ('ASC' == $order) { |
|
159 | usort($data, function ($a, $b) { |
||
0 ignored issues
–
show
|
|||
160 | 2 | return $a->getValue() > $b->getValue(); |
|
161 | 2 | }); |
|
162 | 2 | } |
|
163 | |||
164 | 4 | if ('DESC' == $order) { |
|
165 | 2 | usort($data, function ($a, $b) { |
|
166 | 2 | return $a->getValue() < $b->getValue(); |
|
167 | 2 | }); |
|
168 | 2 | } |
|
169 | |||
170 | 4 | $this->setItems($data); |
|
171 | |||
172 | 4 | return $this; |
|
173 | } |
||
174 | |||
175 | } |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.