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

Greedy::getFormattedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 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
}