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

Greedy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
cbo 4
dl 0
loc 29
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 5 1
A getSubsetWeight() 0 9 2
1
<?php
2
3
namespace drupol\phpartition\Algorithm;
4
5
use drupol\phpartition\BasePartitionAlgorithm;
6
use drupol\phpartition\PartitionAlgorithmInterface;
7
use drupol\phpartition\Subset;
8
9
/**
10
 * Class Greedy.
11
 *
12
 * @package drupol\phpartition\Algorithm
13
 */
14
class Greedy extends BasePartitionAlgorithm implements PartitionAlgorithmInterface {
15
16
  /**
17
   * The greedy algorithm needs the input data to be sorted (desc).
18
   *
19
   * @return array
20
   */
21 2
  public function getResult() {
22 2
    $this->dataset->sortByValue('DESC');
23
24 2
    return parent::getResult();
25
  }
26
27
  /**
28
   * @param \drupol\phpartition\Subset $subset
29
   *
30
   * @return int|mixed
31
   */
32 2
  public function getSubsetWeight(Subset $subset) {
33 2
    $sum = 0;
34
35 2
    foreach($subset->getItems() as $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
36 2
      $sum += $item->getValue();
37 2
    }
38
39 2
    return $sum;
40
  }
41
42
}