1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Domain\BaseLiner; |
6
|
|
|
|
7
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\LineNumber; |
8
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\PreviousLocation; |
9
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\RelativeFileName; |
10
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Severity; |
11
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\Type; |
12
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ArrayParseException; |
13
|
|
|
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\ArrayUtils; |
14
|
|
|
|
15
|
|
|
class BaseLineAnalysisResult |
16
|
|
|
{ |
17
|
|
|
private const LINE_NUMBER = 'lineNumber'; |
18
|
|
|
private const FILE_NAME = 'fileName'; |
19
|
|
|
private const TYPE = 'type'; |
20
|
|
|
private const MESSAGE = 'message'; |
21
|
|
|
private const SEVERITY = 'severity'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var RelativeFileName |
25
|
|
|
*/ |
26
|
|
|
private $fileName; |
27
|
|
|
/** |
28
|
|
|
* @var LineNumber |
29
|
|
|
*/ |
30
|
|
|
private $lineNumber; |
31
|
|
|
/** |
32
|
|
|
* @var Type |
33
|
|
|
*/ |
34
|
|
|
private $type; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $message; |
39
|
|
|
/** |
40
|
|
|
* @var Severity |
41
|
|
|
*/ |
42
|
|
|
private $severity; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @psalm-param array<mixed> $array |
46
|
|
|
* |
47
|
|
|
* @throws ArrayParseException |
48
|
|
|
*/ |
49
|
|
|
public static function fromArray(array $array): self |
50
|
|
|
{ |
51
|
|
|
$lineNumber = new LineNumber(ArrayUtils::getIntValue($array, self::LINE_NUMBER)); |
52
|
|
|
$fileName = new RelativeFileName(ArrayUtils::getStringValue($array, self::FILE_NAME)); |
53
|
|
|
$type = new Type(ArrayUtils::getStringValue($array, self::TYPE)); |
54
|
|
|
$severity = Severity::fromStringOrNull(ArrayUtils::getOptionalStringValue($array, self::SEVERITY)); |
55
|
|
|
$message = ArrayUtils::getStringValue($array, self::MESSAGE); |
56
|
|
|
|
57
|
|
|
return new self($fileName, $lineNumber, $type, $message, $severity); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function make( |
61
|
|
|
RelativeFileName $fileName, |
62
|
|
|
LineNumber $lineNumber, |
63
|
|
|
Type $type, |
64
|
|
|
string $message, |
65
|
|
|
Severity $severity |
66
|
|
|
): self { |
67
|
|
|
return new self($fileName, $lineNumber, $type, $message, $severity); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function __construct( |
71
|
|
|
RelativeFileName $fileName, |
72
|
|
|
LineNumber $lineNumber, |
73
|
|
|
Type $type, |
74
|
|
|
string $message, |
75
|
|
|
Severity $severity |
76
|
|
|
) { |
77
|
|
|
$this->fileName = $fileName; |
78
|
|
|
$this->lineNumber = $lineNumber; |
79
|
|
|
$this->type = $type; |
80
|
|
|
$this->message = $message; |
81
|
|
|
$this->severity = $severity; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getFileName(): RelativeFileName |
85
|
|
|
{ |
86
|
|
|
return $this->fileName; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getLineNumber(): LineNumber |
90
|
|
|
{ |
91
|
|
|
return $this->lineNumber; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getType(): Type |
95
|
|
|
{ |
96
|
|
|
return $this->type; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getMessage(): string |
100
|
|
|
{ |
101
|
|
|
return $this->message; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getSeverity(): Severity |
105
|
|
|
{ |
106
|
|
|
return $this->severity; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @psalm-return array<string,string|int> |
111
|
|
|
*/ |
112
|
|
|
public function asArray(): array |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
self::LINE_NUMBER => $this->getLineNumber()->getLineNumber(), |
116
|
|
|
self::FILE_NAME => $this->getFileName()->getFileName(), |
117
|
|
|
self::TYPE => $this->type->getType(), |
118
|
|
|
self::MESSAGE => $this->message, |
119
|
|
|
self::SEVERITY => $this->severity->getSeverity(), |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return true if this matches given FileName, LineNumber and type. |
125
|
|
|
*/ |
126
|
|
|
public function isMatch(PreviousLocation $location, Type $type): bool |
127
|
|
|
{ |
128
|
|
|
return |
129
|
|
|
$this->fileName->isEqual($location->getRelativeFileName()) && |
130
|
|
|
$this->lineNumber->isEqual($location->getLineNumber()) && |
131
|
|
|
$this->type->isEqual($type); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|