-
Notifications
You must be signed in to change notification settings - Fork 5
/
NoDisallowedPatternsRule.php
55 lines (50 loc) · 1.79 KB
/
NoDisallowedPatternsRule.php
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
<?php
namespace DTL\GherkinLint\Rule;
use DTL\GherkinLint\Model\FeatureDiagnostic;
use DTL\GherkinLint\Model\FeatureDiagnosticSeverity;
use DTL\GherkinLint\Model\ParsedFeature;
use DTL\GherkinLint\Model\Range;
use DTL\GherkinLint\Model\Rule;
use DTL\GherkinLint\Model\RuleConfig;
use DTL\GherkinLint\Model\RuleDescription;
use DTL\GherkinLint\Model\RuleExample;
use Generator;
class NoDisallowedPatternsRule implements Rule
{
public function analyse(ParsedFeature $feature, RuleConfig $config): Generator
{
assert($config instanceof NoDisallowedPatternsConfig);
foreach ($config->patterns as $pattern) {
if (preg_match_all($pattern, $feature->source(), $matches, PREG_OFFSET_CAPTURE) === false) {
continue;
}
foreach ($matches[0] as [$match, $offset]) {
yield new FeatureDiagnostic(
Range::fromByteOffsets($feature->source(), $offset, $offset + strlen($match)),
FeatureDiagnosticSeverity::WARNING,
sprintf('Text "%s" is not allowed by pattern "%s"', $match, $pattern)
);
}
}
}
public function describe(): RuleDescription
{
return new RuleDescription(
'no-restricted-patterns',
'Dissallow text matching any of the given patterns',
NoDisallowedPatternsConfig::class,
[
new RuleExample(
valid: false,
title: 'Disallow the term "Client"',
example: <<<'EOT'
Feature: Client
EOT,
config: new NoDisallowedPatternsConfig([
'/client/i',
]),
)
]
);
}
}