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
|
|
|
* @var float |
23
|
|
|
*/ |
24
|
|
|
private $crapScoreThreshold; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param float $meaningfulCrapChange The minimum crap CHANGE that triggers a message for MODIFIED methods |
28
|
|
|
* @param float $crapScoreThreshold The minimum crap score that triggers a message for all methods |
29
|
|
|
* @param int $maxReturnedMethods |
30
|
|
|
*/ |
31
|
|
|
public function __construct(float $meaningfulCrapChange, float $crapScoreThreshold, int $maxReturnedMethods) |
32
|
|
|
{ |
33
|
|
|
$this->meaningfulCrapChange = $meaningfulCrapChange; |
34
|
|
|
$this->maxReturnedMethods = $maxReturnedMethods; |
35
|
|
|
$this->crapScoreThreshold = $crapScoreThreshold; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param CrapMethodFetcherInterface $newCloverFile |
40
|
|
|
* @param CrapMethodFetcherInterface $oldCloverFile |
41
|
|
|
* @return Difference[] |
42
|
|
|
*/ |
43
|
|
|
public function getMeaningfulDifferences(CrapMethodFetcherInterface $newCloverFile, CrapMethodFetcherInterface $oldCloverFile) |
44
|
|
|
{ |
45
|
|
|
$newMethods = $newCloverFile->getMethods(); |
46
|
|
|
$oldMethods = $oldCloverFile->getMethods(); |
47
|
|
|
|
48
|
|
|
// Let's keep only methods that are in both files: |
49
|
|
|
$inCommonMethods = array_intersect(array_keys($newMethods), array_keys($oldMethods)); |
50
|
|
|
|
51
|
|
|
// New methods in the new file: |
52
|
|
|
$createdMethods = array_diff(array_keys($newMethods), $inCommonMethods); |
53
|
|
|
|
54
|
|
|
$differences = []; |
55
|
|
|
|
56
|
|
|
foreach ($inCommonMethods as $methodName) { |
57
|
|
|
$change = abs($newMethods[$methodName]->getCrap() - $oldMethods[$methodName]->getCrap()); |
58
|
|
|
if ($change > $this->meaningfulCrapChange && ($newMethods[$methodName]->getCrap() > $this->crapScoreThreshold || $oldMethods[$methodName]->getCrap() > $this->crapScoreThreshold)) { |
59
|
|
|
$differences[] = new Difference($newMethods[$methodName], $oldMethods[$methodName]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($createdMethods as $methodName) { |
64
|
|
|
$method = $newMethods[$methodName]; |
65
|
|
|
if ($method->getCrap() > $this->crapScoreThreshold) { |
66
|
|
|
$differences[] = new Difference($method, null); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Now, let's order the differences by crap order. |
71
|
|
|
usort($differences, function(Difference $d1, Difference $d2) { |
72
|
|
|
return $d2->getCrapScore() <=> $d1->getCrapScore(); |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
// Now, let's limit the number of returned differences |
76
|
|
|
$differences = array_slice(array_values($differences), 0, $this->maxReturnedMethods); |
77
|
|
|
|
78
|
|
|
return $differences; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|