1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Static Analysis Results Baseliner (sarb). |
5
|
|
|
* |
6
|
|
|
* (c) Dave Liddament |
7
|
|
|
* |
8
|
|
|
* For the full copyright and licence information please view the LICENSE file distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\GitDiffHistoryAnalyser; |
14
|
|
|
|
15
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\LineNumber; |
16
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\PreviousLocation; |
17
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\RelativeFileName; |
18
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryAnalyser; |
19
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\UnifiedDiffParser\FileMutations; |
20
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\UnifiedDiffParser\NewFileName; |
21
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Plugins\GitDiffHistoryAnalyser\internal\OriginalLineNumberCalculator; |
22
|
|
|
|
23
|
|
|
final class DiffHistoryAnalyser implements HistoryAnalyser |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* DiffHistoryAnalyser constructor. |
27
|
|
|
*/ |
28
|
|
|
public function __construct( |
29
|
|
|
private FileMutations $fileMutations, |
30
|
|
|
) { |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns the location of the line number in the baseline (if it exists). |
35
|
|
|
*/ |
36
|
|
|
public function getPreviousLocation(RelativeFileName $fileName, LineNumber $lineNumber): PreviousLocation |
37
|
|
|
{ |
38
|
|
|
$newFileName = new NewFileName($fileName->getFileName()); |
39
|
|
|
|
40
|
|
|
$fileMutation = $this->fileMutations->getFileMutation($newFileName); |
41
|
|
|
|
42
|
|
|
// If not in file mutations then no change to code |
43
|
|
|
if (null === $fileMutation) { |
44
|
|
|
return PreviousLocation::fromFileNameAndLineNumber($fileName, $lineNumber); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// If file added then this is not in the baseline. |
48
|
|
|
if ($fileMutation->isAddedFile()) { |
49
|
|
|
return PreviousLocation::noPreviousLocation(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$originalLineNumber = OriginalLineNumberCalculator::calculateOriginalLineNumber( |
53
|
|
|
$fileMutation, |
54
|
|
|
$lineNumber->getLineNumber(), |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
if (null === $originalLineNumber) { |
58
|
|
|
return PreviousLocation::noPreviousLocation(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return PreviousLocation::fromFileNameAndLineNumber( |
62
|
|
|
$fileMutation->getOriginalFileName(), |
63
|
|
|
new LineNumber($originalLineNumber), |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|