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

Greedy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
cbo 3
dl 0
loc 33
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormattedData() 0 9 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
  public function getFormattedData() {
22
    $data = parent::getFormattedData();
23
24
    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...
25
      return $a->getValue() < $b->getValue();
26
    });
27
28
    return $data;
29
  }
30
31
  /**
32
   * @param \drupol\phpartition\Subset $subset
33
   *
34
   * @return int|mixed
35
   */
36
  public function getSubsetWeight(Subset $subset) {
37
    $sum = 0;
38
39
    foreach($subset->getItems() as $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
40
      $sum += $item->getValue();
41
    }
42
43
    return $sum;
44
  }
45
46
}