Completed
Push — master ( 8c892e...6a431f )
by Pol
11:58
created

SubsetContainer::addItemToSubset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
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
  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
    $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
    $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
    if ($al == $bl) {
25
      return 0;
26
    }
27
    return ($al > $bl) ? -1 : +1;
28
  }
29
30
  /**
31
   * @param $partition
32
   */
33
  public function setPartition($partition) {
34
    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
      $subset = new Subset();
36
      $subset->setAlgo($this->getAlgo());
37
      $this->insert($subset);
38
    }
39
  }
40
41
  /**
42
   * @param \drupol\phpartition\SubsetItem $item
43
   */
44
  public function addItemToSubset(SubsetItem $item) {
45
    $this->top();
46
    $subset = $this->extract();
47
    $subset->addItem($item);
48
    $this->insert($subset);
49
  }
50
51
  /**
52
   * @return Subset[]
53
   */
54
  public function getSubsets() {
55
    $data = array();
56
    $clone = clone $this;
57
58
    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...
59
      $data[] = $clone->current();
60
    }
61
62
    return $data;
63
  }
64
65
  /**
66
   * @return array
67
   */
68
  public function getSubsetsAndItemsAsArray() {
69
    $data = array();
70
71
    foreach($this->getSubsets() as $subset) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
72
      $data[] = $subset->getRawItems();
73
      }
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 4 spaces, found 6
Loading history...
74
75
    return $data;
76
  }
77
78
  /**
79
   * @param \drupol\phpartition\BasePartitionAlgorithm $algo
80
   */
81
  public function setAlgo(BasePartitionAlgorithm $algo) {
82
    $this->algo = $algo;
83
  }
84
85
  /**
86
   * @return BasePartitionAlgorithm
87
   */
88
  public function getAlgo() {
89
    return $this->algo;
90
  }
91
92
}
93