-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAbstractSniff.php
More file actions
99 lines (86 loc) · 2.87 KB
/
Copy pathAbstractSniff.php
File metadata and controls
99 lines (86 loc) · 2.87 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
<?php
declare(strict_types=1);
namespace DocbookCS\Sniff;
use DocbookCS\Runner\EntityExpansionMarker;
use DocbookCS\Source\File;
use DocbookCS\Violation\Severity;
use DocbookCS\Violation\SourceRange;
use DocbookCS\Violation\Violation;
/**
* @template TFixerData = mixed
* @implements SniffInterface<TFixerData>
*/
abstract class AbstractSniff implements SniffInterface
{
protected Severity $severity = Severity::ERROR;
/** @var array<string, string> */
protected array $properties = [];
/** @throws \InvalidArgumentException if a configured severity is invalid */
public function setProperty(string $name, string $value): void
{
if ($name !== 'severity') {
$this->properties[$name] = $value;
return;
}
if (null !== $severity = Severity::tryFrom($value)) {
$this->severity = $severity;
return;
}
throw new \InvalidArgumentException(sprintf('Invalid severity "%s" config for %s.', $value, static::getCode()));
}
protected function getProperty(string $name, string $default = ''): string
{
return $this->properties[$name] ?? $default;
}
protected function isSourceBacked(\DOMNode $node): bool
{
return !EntityExpansionMarker::contains($node);
}
/**
* The offsets point at the opening "<" and closing "<" in the source.
*
* @return array{SourceRange, SourceRange}
* @throws \InvalidArgumentException if a generated source range is inconsistent
* @throws \OutOfBoundsException if a tag offset lies outside the source
*/
protected function elementNameRanges(File $file, int $beginOffset, int $untilOffset, string $elementName): array
{
$openingNameOffset = $beginOffset + 1;
$closingNameOffset = $untilOffset + 2;
$elementNameLength = strlen($elementName);
return [
SourceRange::fromFile(
$file,
$openingNameOffset,
$openingNameOffset + $elementNameLength,
),
SourceRange::fromFile(
$file,
$closingNameOffset,
$closingNameOffset + $elementNameLength,
),
];
}
/**
* @param non-empty-list<SourceRange> $affectedRanges
* @param TFixerData $fixerData
*
* @return Violation<TFixerData>
* @throws \InvalidArgumentException if the affected ranges are inconsistent
*/
protected function createViolation(
string $filePath,
string $message,
array $affectedRanges,
mixed $fixerData = null,
): Violation {
return new Violation(
sniffCode: static::getCode(),
filePath: $filePath,
message: $message,
affectedRanges: $affectedRanges,
fixerData: $fixerData,
severity: $this->severity,
);
}
}