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