Completed
Push — master ( 6a431f...d95f5b )
by Pol
11:02 queued 09:07
created

Greedy::getFormattedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
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 getFormattedData() {
22 2
    $data = parent::getFormattedData();
23
24 2
    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 2
      return $a->getValue() < $b->getValue();
26 2
    });
27
28 2
    return $data;
29
  }
30
31
  /**
32
   * @param \drupol\phpartition\Subset $subset
33
   *
34
   * @return int|mixed
35
   */
36 2
  public function getSubsetWeight(Subset $subset) {
37 2
    $sum = 0;
38
39 2
    foreach($subset->getItems() as $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
40 2
      $sum += $item->getValue();
41
    }
42
43 2
    return $sum;
44
  }
45
46
}