forked from dantleech/gherkin-lint-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoConsecutiveEmptyLinesRule.php
107 lines (87 loc) · 2.81 KB
/
NoConsecutiveEmptyLinesRule.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
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
<?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 NoConsecutiveEmptyLinesRule implements Rule
{
public function analyse(ParsedFeature $feature, RuleConfig $config): Generator
{
$lines = preg_split('{\r\n|\n|\r}', $feature->source());
if (false === $lines) {
return;
}
$emptyLines = [];
foreach ($lines as $lineNo => $line) {
$lineNo = $lineNo + 1;
if (trim($line) === '') {
$emptyLines[] = $lineNo;
}
}
$prev = null;
$start = null;
foreach ($emptyLines as $lineNo) {
if (null === $prev) {
$prev = $lineNo;
continue;
}
if ($start && $prev + 1 !== $lineNo) {
yield new FeatureDiagnostic(
Range::fromInts($start, 1, $prev, 1),
FeatureDiagnosticSeverity::WARNING,
'Consecutive empty lines are dissallowed'
);
$start = null;
$prev = $lineNo;
continue;
}
if (null === $start && $prev + 1 === $lineNo) {
$start = $prev + 1;
}
$prev = $lineNo;
}
if ($start && $prev) {
yield new FeatureDiagnostic(
Range::fromInts($start, 1, $prev, 1),
FeatureDiagnosticSeverity::WARNING,
'Consecutive empty lines are dissallowed'
);
}
}
public function describe(): RuleDescription
{
return new RuleDescription(
'no-consecutive-empty-lines',
'Do not permit consecutive empty lines',
null,
[
new RuleExample(
valid: true,
title: 'No consecutive empty lines',
example: <<<'EOT'
Feature: Foo
Scenario: One
Scenario: Two
Scenario: Three
EOT
),
new RuleExample(
valid: false,
title: 'Consecutive empty lines',
example: <<<'EOT'
Feature: Foo
Scenario: One
Scenario: Two
Scenario: Three
EOT
),
]
);
}
}