Completed
Push — master ( ac5162...dac384 )
by Pol
06:21
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
  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...
Bug introduced by
The method getSubsetWeight() does not seem to exist on object<drupol\phpartition\BasePartitionAlgorithm>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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...
Bug introduced by
The method getSubsetWeight() does not seem to exist on object<drupol\phpartition\BasePartitionAlgorithm>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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 SubsetItem[] $items
43
   */
44
  public function addItemsToSubset(array $items = array()) {
45
    foreach ($items as $item) {
46
      $this->addItemToSubset($item);
47
    }
48
  }
49
50
  /**
51
   * @param \drupol\phpartition\SubsetItem $item
52
   */
53
  public function addItemToSubset(SubsetItem $item) {
54
    $this->top();
55
    $subset = $this->extract();
56
    $subset->addItem($item);
57
    $this->insert($subset);
58
  }
59
60
  /**
61
   * @return Subset[]
62
   */
63 View Code Duplication
  public function getSubsets() {
0 ignored issues
show
Duplication introduced by
This method 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...
64
    $data = array();
65
    $clone = clone $this;
66
67
    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
      $data[] = $clone->current();
69
    }
70
71
    return $data;
72
  }
73
74
  /**
75
   * @return array
76
   */
77 View Code Duplication
  public function getSubsetsAndItemsAsArray() {
0 ignored issues
show
Duplication introduced by
This method 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...
78
    $data = array();
79
80
    foreach ($this->getSubsets() as $subset) {
81
      $data[] = $subset->getRawItems();
82
      }
0 ignored issues
show
Coding Style introduced by
Closing brace indented incorrectly; expected 4 spaces, found 6
Loading history...
83
84
    return array_values(array_filter($data));
85
  }
86
87
  /**
88
   * @param \drupol\phpartition\BasePartitionAlgorithm $algo
89
   */
90
  public function setAlgo(BasePartitionAlgorithm $algo) {
91
    $this->algo = $algo;
92
  }
93
94
  /**
95
   * @return BasePartitionAlgorithm
96
   */
97
  public function getAlgo() {
98
    return $this->algo;
99
  }
100
101
}
102