Skip to content

Commit 3ad9e64

Browse files
committed
Improve validator
1 parent 1922107 commit 3ad9e64

6 files changed

Lines changed: 72 additions & 32 deletions

File tree

‎src/Parser.php‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use FFI\Parser\Exception\ValidationException;
1010
use FFI\Parser\Node\Declaration\RootDeclarationInterface;
1111
use FFI\Parser\Parser\ParserExecutor;
12-
use FFI\Parser\Validator\ValidatorInterface;
12+
use FFI\Parser\Validator\ValidatorRuleInterface;
1313
use Phplrt\Contracts\Source\Exception\SourceExceptionInterface;
1414
use Phplrt\Contracts\Source\ReadableInterface;
1515
use Phplrt\Contracts\Source\SourceFactoryInterface;
@@ -38,16 +38,42 @@ final class Parser
3838
* @param SourceFactoryInterface|null $sources creates the sources out of
3939
* the values given to {@see parse()} and {@see validate()}, the
4040
* default one accepts strings, files and streams
41-
* @param iterable<mixed, ValidatorInterface>|null $validators checks of the
41+
* @param iterable<mixed, ValidatorRuleInterface>|null $rules checks of the
4242
* syntax tree run by {@see parse()}
4343
*/
4444
public function __construct(
4545
?SourceFactoryInterface $sources = null,
46-
?iterable $validators = null,
46+
?iterable $rules = null,
4747
) {
48-
$this->parser = new ParserExecutor();
49-
$this->sources = $sources ?? SourceFactory::createDefault();
50-
$this->validator = $validators === null ? Validator::createDefault() : new Validator($validators);
48+
$this->parser = self::createParser();
49+
$this->sources = self::createSourceFactory($sources);
50+
$this->validator = self::createValidator($rules);
51+
}
52+
53+
/**
54+
* @param iterable<mixed, ValidatorRuleInterface>|null $rules
55+
*/
56+
private static function createValidator(?iterable $rules): Validator
57+
{
58+
if ($rules === null) {
59+
return Validator::createDefault();
60+
}
61+
62+
return new Validator($rules);
63+
}
64+
65+
private static function createSourceFactory(?SourceFactoryInterface $sources): SourceFactoryInterface
66+
{
67+
if ($sources === null) {
68+
return SourceFactory::createDefault();
69+
}
70+
71+
return $sources;
72+
}
73+
74+
private static function createParser(): ParserExecutor
75+
{
76+
return new ParserExecutor();
5177
}
5278

5379
/**

‎src/Validator.php‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use FFI\Parser\Node\Declaration\RootDeclarationInterface;
88
use FFI\Parser\Validator\TreeWalker;
9-
use FFI\Parser\Validator\ValidatorInterface;
9+
use FFI\Parser\Validator\ValidatorRuleInterface;
1010
use FFI\Parser\Validator\Violation;
1111

1212
/**
@@ -19,18 +19,31 @@
1919
final class Validator
2020
{
2121
/**
22-
* @var list<ValidatorInterface>
22+
* @var list<ValidatorRuleInterface>
2323
*/
2424
private readonly array $validators;
2525

2626
/**
27-
* @param iterable<mixed, ValidatorInterface> $validators
27+
* @param iterable<mixed, ValidatorRuleInterface> $rules
2828
*/
29-
public function __construct(iterable $validators = [])
29+
public function __construct(iterable $rules = [])
3030
{
31-
$this->validators = \is_array($validators)
32-
? \array_values($validators)
33-
: \iterator_to_array($validators, false);
31+
$this->validators = self::formatValidatorRules($rules);
32+
}
33+
34+
/**
35+
* TODO Since PHP 8.2 should be replaced to `iterator_to_array`
36+
*
37+
* @param iterable<mixed, ValidatorRuleInterface> $rules
38+
* @return list<ValidatorRuleInterface>
39+
*/
40+
private static function formatValidatorRules(iterable $rules): array
41+
{
42+
return match (true) {
43+
$rules instanceof \Traversable => \iterator_to_array($rules, false),
44+
\array_is_list($rules) => $rules,
45+
default => \array_values($rules),
46+
};
3447
}
3548

3649
/**
@@ -55,7 +68,8 @@ public function validate(array $declarations): array
5568
return [];
5669
}
5770

58-
$violations = (new TreeWalker($this->validators, $declarations))->walk();
71+
$violations = (new TreeWalker($this->validators, $declarations))
72+
->walk();
5973

6074
\usort($violations, static fn(Violation $a, Violation $b): int => $a->offset <=> $b->offset);
6175

‎src/Validator/TreeWalker.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class TreeWalker
3030

3131
public function __construct(
3232
/**
33-
* @var non-empty-list<ValidatorInterface>
33+
* @var non-empty-list<ValidatorRuleInterface>
3434
*/
3535
private readonly array $validators,
3636
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* it every node of the tree, each one once, and the validator picks the
1515
* kinds of nodes it checks.
1616
*/
17-
interface ValidatorInterface
17+
interface ValidatorRuleInterface
1818
{
1919
/**
2020
* Checks a single node. An empty result means the node breaks no rule

‎tests/ParserTest.php‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use FFI\Parser\Parser;
1414
use FFI\Parser\Parser\ParserExecutor;
1515
use FFI\Parser\Validator\ValidationContext;
16-
use FFI\Parser\Validator\ValidatorInterface;
16+
use FFI\Parser\Validator\ValidatorRuleInterface;
1717
use FFI\Parser\Validator\Violation;
1818
use Phplrt\Contracts\Lexer\Exception\RuntimeExceptionInterface as LexerRuntimeExceptionInterface;
1919
use Phplrt\Contracts\Source\Exception\SourceExceptionInterface;
@@ -295,7 +295,7 @@ public function testDeeplyNestedHeaderIsParsedQuickly(string $code, bool $isVali
295295

296296
public function testViolationsAreReportedByValidationException(): void
297297
{
298-
$parser = new Parser(validators: [self::createIntTypeValidator()]);
298+
$parser = new Parser(rules: [self::createIntTypeValidator()]);
299299

300300
try {
301301
$parser->parse("int a;\nint b;");
@@ -314,7 +314,7 @@ public function testValidatorsAreGivenTheParsedDeclarations(): void
314314
{
315315
$validator = self::createRecordingValidator();
316316

317-
$declarations = (new Parser(validators: [$validator]))->parse('char a; long b;');
317+
$declarations = (new Parser(rules: [$validator]))->parse('char a; long b;');
318318

319319
self::assertNotSame([], $validator->contexts);
320320
self::assertSame($declarations, $validator->contexts[0]->declarations);
@@ -324,7 +324,7 @@ public function testValidatorsAreNotRunBySyntaxCheck(): void
324324
{
325325
$validator = self::createRecordingValidator();
326326

327-
self::assertTrue((new Parser(validators: [$validator]))->validate('int a;'));
327+
self::assertTrue((new Parser(rules: [$validator]))->validate('int a;'));
328328
self::assertSame([], $validator->contexts);
329329
}
330330

@@ -333,7 +333,7 @@ public function testValidatorsAreNotRunOnSyntaxError(): void
333333
$validator = self::createRecordingValidator();
334334

335335
try {
336-
(new Parser(validators: [$validator]))->parse('int a');
336+
(new Parser(rules: [$validator]))->parse('int a');
337337
} catch (SyntaxErrorException) {
338338
self::assertSame([], $validator->contexts);
339339

@@ -343,9 +343,9 @@ public function testValidatorsAreNotRunOnSyntaxError(): void
343343
self::fail('SyntaxErrorException has not been thrown');
344344
}
345345

346-
private static function createIntTypeValidator(): ValidatorInterface
346+
private static function createIntTypeValidator(): ValidatorRuleInterface
347347
{
348-
return new class implements ValidatorInterface {
348+
return new class implements ValidatorRuleInterface {
349349
public function validate(NodeInterface $node, ValidationContext $context): iterable
350350
{
351351
if ($node instanceof IntType) {
@@ -356,11 +356,11 @@ public function validate(NodeInterface $node, ValidationContext $context): itera
356356
}
357357

358358
/**
359-
* @return ValidatorInterface&object{contexts: list<ValidationContext>}
359+
* @return ValidatorRuleInterface&object{contexts: list<ValidationContext>}
360360
*/
361-
private static function createRecordingValidator(): ValidatorInterface
361+
private static function createRecordingValidator(): ValidatorRuleInterface
362362
{
363-
return new class implements ValidatorInterface {
363+
return new class implements ValidatorRuleInterface {
364364
/**
365365
* @var list<ValidationContext>
366366
*/
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use FFI\Parser\Node\Type\BuiltinType\IntType;
1111
use FFI\Parser\Validator;
1212
use FFI\Parser\Validator\ValidationContext;
13-
use FFI\Parser\Validator\ValidatorInterface;
13+
use FFI\Parser\Validator\ValidatorRuleInterface;
1414
use FFI\Parser\Validator\Violation;
1515
use PHPUnit\Framework\Attributes\CoversClass;
1616

@@ -104,15 +104,15 @@ public function testDeclarationsWithoutValidatorsHaveNoViolations(): void
104104
}
105105

106106
/**
107-
* @return ValidatorInterface&object{
107+
* @return ValidatorRuleInterface&object{
108108
* visits: list<non-empty-string>,
109109
* nodes: list<NodeInterface>,
110110
* contexts: list<ValidationContext>,
111111
* }
112112
*/
113-
private static function createRecordingValidator(): ValidatorInterface
113+
private static function createRecordingValidator(): ValidatorRuleInterface
114114
{
115-
return new class implements ValidatorInterface {
115+
return new class implements ValidatorRuleInterface {
116116
/**
117117
* @var list<non-empty-string>
118118
*/
@@ -143,9 +143,9 @@ public function validate(NodeInterface $node, ValidationContext $context): itera
143143
/**
144144
* @param class-string<NodeInterface> $class
145145
*/
146-
private static function createRejectingValidator(string $class): ValidatorInterface
146+
private static function createRejectingValidator(string $class): ValidatorRuleInterface
147147
{
148-
return new class ($class) implements ValidatorInterface {
148+
return new class ($class) implements ValidatorRuleInterface {
149149
/**
150150
* @param class-string<NodeInterface> $class
151151
*/

0 commit comments

Comments
 (0)