DiffService::getMeaningfulDifferences()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 9
nop 2
1
<?php
2
3
4
namespace TheCodingMachine\WashingMachine\Clover;
5
6
use TheCodingMachine\WashingMachine\Clover\Analysis\Difference;
7
8
/**
9
 * Service in charge of analyzing the differences between 2 clover files.
10
 */
11
class DiffService
12
{
13
    /**
14
     * @var float
15
     */
16
    private $meaningfulCrapChange;
17
    /**
18
     * @var int
19
     */
20
    private $maxReturnedMethods;
21
22
    public function __construct(float $meaningfulCrapChange, int $maxReturnedMethods)
23
    {
24
25
        $this->meaningfulCrapChange = $meaningfulCrapChange;
26
        $this->maxReturnedMethods = $maxReturnedMethods;
27
    }
28
29
    /**
30
     * @param CrapMethodFetcherInterface $newCloverFile
31
     * @param CrapMethodFetcherInterface $oldCloverFile
32
     * @return Difference[]
33
     */
34
    public function getMeaningfulDifferences(CrapMethodFetcherInterface $newCloverFile, CrapMethodFetcherInterface $oldCloverFile)
35
    {
36
        $newMethods = $newCloverFile->getMethods();
37
        $oldMethods = $oldCloverFile->getMethods();
38
39
        // Let's keep only methods that are in both files:
40
        $inCommonMethods = array_intersect(array_keys($newMethods), array_keys($oldMethods));
41
42
        // New methods in the new file:
43
        $createdMethods = array_diff(array_keys($newMethods), $inCommonMethods);
44
45
        $differences = [];
46
47
        foreach ($inCommonMethods as $methodName) {
48
            $change = abs($newMethods[$methodName]->getCrap() - $oldMethods[$methodName]->getCrap());
49
            if ($change > $this->meaningfulCrapChange) {
50
                $differences[] = new Difference($newMethods[$methodName], $oldMethods[$methodName]);
51
            }
52
        }
53
54
        foreach ($createdMethods as $methodName) {
55
            $method = $newMethods[$methodName];
56
            if ($method->getCrap() > $this->meaningfulCrapChange) {
57
                $differences[] = new Difference($method, null);
58
            }
59
        }
60
61
        // Now, let's order the differences by crap order.
62
        usort($differences, function(Difference $d1, Difference $d2) {
63
           return $d2->getCrapScore() <=> $d1->getCrapScore();
64
        });
65
66
        // Now, let's limit the number of returned differences
67
        $differences = array_slice(array_values($differences), 0, $this->maxReturnedMethods);
68
69
        return $differences;
70
    }
71
}
72