Completed
Push — master ( 137b9b...8d97a2 )
by Pol
03:29
created

Dataset::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace drupol\phpartition;
4
5
class Dataset implements \Countable {
6
7
  /**
8
   * @var SubsetItem[]
9
   */
10
  protected $items;
11
12
  /**
13
   * @return int
14
   */
15 2
  public function count() {
16 2
    return count($this->getItems());
17
  }
18
19
  /**
20
   * @return SubsetItem[]
21
   */
22 6
  public function getItems() {
23 6
    return $this->items;
24
  }
25
26
  /**
27
   * @param SubsetItem[] $items
28
   *
29
   * @return \drupol\phpartition\SubsetItem[]
0 ignored issues
show
Documentation introduced by
Should the return type not be SubsetItem[]|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
30
   */
31 6
  public function setItems(array $items = array()) {
32 6
    $this->items = $items;
33 6
  }
34
35
  /**
36
   * @param \drupol\phpartition\SubsetItem $item
37
   */
38 6
  public function add(SubsetItem $item) {
39 6
    $this->items[$item->getKey()] = $item;
40 6
  }
41
42
  /**
43
   * @return int
44
   */
45 2
  public function getSum() {
46 2
    $sum = 0;
47
48 2
    foreach($this->getItems() as $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
49 2
      $sum += $item->getValue();
50 2
    }
51
52 2
    return $sum;
53
  }
54
55
  /**
56
   * @return array
57
   */
58
  public function getItemsValues() {
59
    $data = array();
60
61
    foreach($this->getItems() as $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
62
      $data[$item->getKey()] = $item->getValue();
63
    }
64
65
    return $data;
66
  }
67
68
  /**
69
   * @param SubsetItem[] $items
70
   */
71
  public function substract(array $items = array()) {
72
    $this->setItems(array_diff($this->getItems(), $items));
73
  }
74
75
  /**
76
   *
77
   */
78 6
  public function clear() {
79 6
    $this->setItems();
80 6
  }
81
82
  /**
83
   * @param $key
84
   *
85
   * @return \drupol\phpartition\SubsetItem
86
   */
87
  public function getItem($key) {
88
    return $this->items[$key];
89
  }
90
91
  /**
92
   * @param SubsetItem[] $items
93
   */
94 2
  public function deleteItems(array $items = array()) {
95 2
    foreach($items as $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
96 2
      $this->delete($item);
97 2
    }
98 2
  }
99
100
  /**
101
   * @param \drupol\phpartition\SubsetItem $item
102
   */
103 2
  public function delete(SubsetItem $item) {
104 2
    foreach($this->items as $key => $value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
105 2
      if ($value->getKey() == $item->getKey()) {
106 2
        unset($this->items[$value->getKey()]);
107 2
      }
108 2
    }
109 2
  }
110
111
  /**
112
   * @param string $order
113
   *
114
   * @return $this
115
   */
116 4
  public function sortByValue($order = 'ASC') {
117 4
    $data = $this->getItems();
118
119 4
    if ('ASC' == $order) {
120
      usort($data, function($a, $b) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

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.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

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.

Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
121 2
        return $a->getValue() > $b->getValue();
122 2
      });
123 2
    }
124
125 4
    if ('DESC' == $order) {
126 2
      usort($data, function($a, $b) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
127 2
        return $a->getValue() < $b->getValue();
128 2
      });
129 2
    }
130
131 4
    $this->setItems($data);
132
133 4
    return $this;
134
  }
135
136
}