-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileNameRule.php
106 lines (98 loc) · 3.44 KB
/
FileNameRule.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
<?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;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\String\UnicodeString;
class FileNameRule implements Rule
{
public function analyse(ParsedFeature $feature, RuleConfig $config): Generator
{
$document = $feature->document();
assert($config instanceof FileNameConfig);
$fullPath = $document->uri;
if (null === $fullPath) {
return;
}
$path = new UnicodeString(Path::getFilenameWithoutExtension($fullPath));
$converted = match ($config->style) {
FileNameConfig::PASCAL_CASE => ucfirst($path->camel()->__toString()),
FileNameConfig::CAMEL_CASE => $path->camel()->__toString(),
FileNameConfig::SNAKE_CASE => $path->snake()->__toString(),
FileNameConfig::KEBAB_CASE => $path->snake()->replace('_', '-')->__toString(),
};
if ($converted === $path->__toString()) {
return;
}
yield new FeatureDiagnostic(
Range::fromInts(1, 1, 1, 1),
FeatureDiagnosticSeverity::WARNING,
sprintf('Filename "%s" should be "%s"', $fullPath, $config->style)
);
}
public function describe(): RuleDescription
{
return new RuleDescription(
'filename',
'Filenames must conform to the specified stype',
FileNameConfig::class,
examples: [
new RuleExample(
title: 'Snake case',
valid: true,
example: <<<'EOT'
Feature: Some feature
EOT
,
config: new FileNameConfig(
style: 'snake_case'
),
filename: 'this_is_fine.feature',
),
new RuleExample(
title: 'Pascal case',
valid: true,
example: <<<'EOT'
Feature: Some feature
EOT
,
config: new FileNameConfig(
style: 'PascalCase'
),
filename: 'ThisIsFine.feature',
),
new RuleExample(
title: 'Kebab Case',
valid: true,
example: <<<'EOT'
Feature: Some feature
EOT
,
config: new FileNameConfig(
style: 'kebab-case'
),
filename: 'this-is-fine.feature',
),
new RuleExample(
title: 'Camel case',
valid: true,
example: <<<'EOT'
Feature: Some feature
EOT
,
config: new FileNameConfig(
style: 'camelCase'
),
filename: 'thisIsFine.feature',
)
]
);
}
}