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\ResultsParsers\PhpmdJsonResultsParser; |
14
|
|
|
|
15
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\AbsoluteFileName; |
16
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\LineNumber; |
17
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Location; |
18
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot; |
19
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Type; |
20
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\File\InvalidFileFormatException; |
21
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResult; |
22
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResults; |
23
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResultsBuilder; |
24
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\Identifier; |
25
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\ResultsParser; |
26
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ArrayParseException; |
27
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ArrayUtils; |
28
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\JsonParseException; |
29
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\JsonUtils; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Handles PHPMD JSON output. |
33
|
|
|
*/ |
34
|
|
|
class PhpmdJsonResultsParser implements ResultsParser |
35
|
|
|
{ |
36
|
|
|
public function convertFromString(string $resultsAsString, ProjectRoot $projectRoot): AnalysisResults |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
$asArray = JsonUtils::toArray($resultsAsString); |
40
|
|
|
} catch (JsonParseException $e) { |
41
|
|
|
throw new InvalidFileFormatException('Not a valid JSON format'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->convertFromArray($asArray, $projectRoot); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getIdentifier(): Identifier |
48
|
|
|
{ |
49
|
|
|
return new PhpmdJsonIdentifier(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function showTypeGuessingWarning(): bool |
53
|
|
|
{ |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Converts from an array. |
59
|
|
|
* |
60
|
|
|
* @psalm-param array<mixed> $analysisResultsAsArray |
61
|
|
|
* |
62
|
|
|
* @throws InvalidFileFormatException |
63
|
|
|
*/ |
64
|
|
|
private function convertFromArray(array $analysisResultsAsArray, ProjectRoot $projectRoot): AnalysisResults |
65
|
|
|
{ |
66
|
|
|
$analysisResultsBuilder = new AnalysisResultsBuilder(); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$filesWithProblems = ArrayUtils::getArrayValue($analysisResultsAsArray, 'files'); |
70
|
|
|
} catch (ArrayParseException $e) { |
71
|
|
|
throw new InvalidFileFormatException("Missing 'files' key at root level of JSON structure"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
/** @psalm-suppress MixedAssignment */ |
76
|
|
|
foreach ($filesWithProblems as $fileWithProblems) { |
77
|
|
|
ArrayUtils::assertArray($fileWithProblems); |
78
|
|
|
$absoluteFileNameAsString = ArrayUtils::getStringValue($fileWithProblems, 'file'); |
79
|
|
|
$absoluteFileName = new AbsoluteFileName($absoluteFileNameAsString); |
80
|
|
|
|
81
|
|
|
$violations = ArrayUtils::getArrayValue($fileWithProblems, 'violations'); |
82
|
|
|
|
83
|
|
|
$this->processViolationsInFile($analysisResultsBuilder, $absoluteFileName, $projectRoot, $violations); |
84
|
|
|
} |
85
|
|
|
} catch (ArrayParseException $e) { |
86
|
|
|
throw new InvalidFileFormatException("Invalid file format: {$e->getMessage()}"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $analysisResultsBuilder->build(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @psalm-param array<mixed> $violations |
94
|
|
|
* |
95
|
|
|
* @throws InvalidFileFormatException |
96
|
|
|
*/ |
97
|
|
|
private function processViolationsInFile( |
98
|
|
|
AnalysisResultsBuilder $analysisResultsBuilder, |
99
|
|
|
AbsoluteFileName $absoluteFileName, |
100
|
|
|
ProjectRoot $projectRoot, |
101
|
|
|
array $violations |
102
|
|
|
): void { |
103
|
|
|
$violationCount = 1; |
104
|
|
|
/** @psalm-suppress MixedAssignment */ |
105
|
|
|
foreach ($violations as $violation) { |
106
|
|
|
try { |
107
|
|
|
ArrayUtils::assertArray($violation); |
108
|
|
|
$analysisResult = $this->processViolation($absoluteFileName, $projectRoot, $violation); |
109
|
|
|
$analysisResultsBuilder->addAnalysisResult($analysisResult); |
110
|
|
|
++$violationCount; |
111
|
|
|
} catch (ArrayParseException | JsonParseException $e) { |
112
|
|
|
throw new InvalidFileFormatException("Can not process violation {$violationCount} for file {$absoluteFileName->getFileName()}"); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @psalm-param array<mixed> $violation |
119
|
|
|
* |
120
|
|
|
* @throws ArrayParseException |
121
|
|
|
* @throws JsonParseException |
122
|
|
|
*/ |
123
|
|
|
private function processViolation(AbsoluteFileName $aboluteFileName, ProjectRoot $projectRoot, array $violation): AnalysisResult |
124
|
|
|
{ |
125
|
|
|
$typeAsString = ArrayUtils::getStringValue($violation, 'rule'); |
126
|
|
|
$type = new Type($typeAsString); |
127
|
|
|
|
128
|
|
|
$message = ArrayUtils::getStringValue($violation, 'description'); |
129
|
|
|
|
130
|
|
|
$lineAsInt = ArrayUtils::getIntValue($violation, 'beginLine'); |
131
|
|
|
|
132
|
|
|
$location = Location::fromAbsoluteFileName( |
133
|
|
|
$aboluteFileName, |
134
|
|
|
$projectRoot, |
135
|
|
|
new LineNumber($lineAsInt) |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
return new AnalysisResult( |
139
|
|
|
$location, |
140
|
|
|
$type, |
141
|
|
|
$message, |
142
|
|
|
JsonUtils::toString($violation) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|