|
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
|
|
|
|