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

SubsetContainer::addItemsToSubset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
namespace drupol\phpartition;
4
5
class SubsetContainer extends \SplHeap {
6
7
  /**
8
   * @var BasePartitionAlgorithm
9
   */
10
  protected $algo;
11
12
  /**
13
   * Override compare method.
14
   *
15
   * @param Subset $a
16
   * @param Subset $b
17
   *
18
   * @return int
19
   */
20 6
  public function compare($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...
21 6
    $al = $a->getAlgo()->getSubsetWeight($a);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $al. 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...
22 6
    $bl = $b->getAlgo()->getSubsetWeight($b);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $bl. 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...
23
24 6
    if ($al == $bl) {
25 6
      return 0;
26
    }
27 6
    return ($al > $bl) ? -1 : +1;
28
  }
29
30
  /**
31
   * @param $partition
32
   */
33 6
  public function setPartition($partition) {
34 6
    for($i=0; $i<$partition; $i++) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOR keyword; 0 found
Loading history...
35 6
      $subset = new Subset();
36 6
      $subset->setAlgo($this->getAlgo());
37 6
      $this->insert($subset);
38 6
    }
39 6
  }
40
41
  /**
42
   * @param SubsetItem[] $items
43
   */
44
  public function addItemsToSubset(array $items = array()) {
45
    foreach($items as $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
46
      $this->addItemToSubset($item);
47
    }
48
  }
49
50
  /**
51
   * @param \drupol\phpartition\SubsetItem $item
52
   */
53 4
  public function addItemToSubset(SubsetItem $item) {
54 4
    $this->top();
55 4
    $subset = $this->extract();
56 4
    $subset->addItem($item);
57 4
    $this->insert($subset);
58 4
  }
59
60
  /**
61
   * @return Subset[]
62
   */
63 6
  public function getSubsets() {
64 6
    $data = array();
65 6
    $clone = clone $this;
66
67 6
    for($clone->top(); $clone->valid(); $clone->next()) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOR keyword; 0 found
Loading history...
68 6
      $data[] = $clone->current();
69 6
    }
70
71 6
    return $data;
72
  }
73
74
  /**
75
   * @return array
76
   */
77 6
  public function getSubsetsAndItemsAsArray() {
78 6
    $data = array();
79
80 6
    foreach($this->getSubsets() as $subset) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
81 6
      $data[] = $subset->getRawItems();
82 6
      }
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 4 spaces, found 6
Loading history...
83
84 6
    return array_values(array_filter($data));
85
  }
86
87
  /**
88
   * @param \drupol\phpartition\BasePartitionAlgorithm $algo
89
   */
90 6
  public function setAlgo(BasePartitionAlgorithm $algo) {
91 6
    $this->algo = $algo;
92 6
  }
93
94
  /**
95
   * @return BasePartitionAlgorithm
96
   */
97 6
  public function getAlgo() {
98 6
    return $this->algo;
99
  }
100
101
}
102