-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathViolations.php
More file actions
163 lines (136 loc) · 4.66 KB
/
Violations.php
File metadata and controls
163 lines (136 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
namespace Arkitect\Rules;
use Arkitect\Exceptions\IndexNotFoundException;
/**
* @template-implements \IteratorAggregate<Violation>
*/
class Violations implements \IteratorAggregate, \Countable, \JsonSerializable
{
/**
* @var array<Violation>
*/
private array $violations;
public function __construct()
{
$this->violations = [];
}
public static function fromJson(string $json): self
{
$json = json_decode($json, true);
$instance = new self();
$instance->violations = array_map(static fn (array $json): Violation => Violation::fromJson($json), $json['violations']);
return $instance;
}
public function add(Violation $violation): void
{
$this->violations[] = $violation;
}
public function merge(self $other): void
{
$this->violations = array_merge($this->violations, $other->toArray());
}
public function get(int $index): Violation
{
if (!\array_key_exists($index, $this->violations)) {
throw new IndexNotFoundException($index);
}
return $this->violations[$index];
}
public function getIterator(): \Traversable
{
foreach ($this->violations as $violation) {
yield $violation;
}
}
public function count(): int
{
return \count($this->violations);
}
public function groupedByFqcn(): array
{
return array_reduce($this->violations, static function (array $accumulator, Violation $element) {
$accumulator[$element->getFqcn()][] = $element;
return $accumulator;
}, []);
}
public function toArray(): array
{
return $this->violations;
}
/**
* @param Violations $violations Known violations from the baseline
* @param bool $ignoreBaselineLinenumbers If set to true, violations from the baseline are ignored for the same file even if the line number is different
*/
public function remove(self $violations, bool $ignoreBaselineLinenumbers = false): void
{
if (!$ignoreBaselineLinenumbers) {
$this->violations = array_values(array_udiff(
$this->violations,
$violations->violations,
[__CLASS__, 'compareViolations']
));
return;
}
$baselineViolations = $violations->violations;
foreach ($this->violations as $idx => $violation) {
foreach ($baselineViolations as $baseIdx => $baselineViolation) {
if (
$baselineViolation->getFqcn() === $violation->getFqcn()
&& self::extractViolationKey($baselineViolation->getError()) === self::extractViolationKey($violation->getError())
) {
unset($this->violations[$idx], $baselineViolations[$baseIdx]);
continue 2;
}
}
}
$this->violations = array_values($this->violations);
}
public function sort(): void
{
usort($this->violations, static fn (Violation $v1, Violation $v2): int => $v1 <=> $v2);
}
public function jsonSerialize(): array
{
return get_object_vars($this);
}
/**
* Comparison method that respects all fields in the violation.
*
* Uses the stable violation key (the part before ', but ') for comparison,
* so that changes to rule configuration (e.g. allowed namespaces) do not
* invalidate existing baseline entries.
*/
public static function compareViolations(Violation $a, Violation $b): int
{
return [
$a->getFqcn(),
$a->getLine(),
$a->getFilePath(),
self::extractViolationKey($a->getError()),
] <=> [
$b->getFqcn(),
$b->getLine(),
$b->getFilePath(),
self::extractViolationKey($b->getError()),
];
}
/**
* Extracts the stable violation-specific part from an error message.
*
* ViolationMessage produces two formats:
* - withDescription: "$violation, but $ruleDescription" → returns $violation
* - selfExplanatory: "$ruleDescription" (no ", but ") → returns the full string
*
* The rule description may include configuration-dependent values (like namespace lists)
* that change when the rule config is updated. The violation part is always stable.
*/
private static function extractViolationKey(string $error): string
{
$pos = strpos($error, ', but ');
if (false !== $pos) {
return substr($error, 0, $pos);
}
return $error;
}
}