Skip to content

Commit e781538

Browse files
authored
[Feature] Match evaluator (#26)
* Add MatchEvaluator * Add test * catch regex exception * up test * up version
1 parent 88aeadb commit e781538

File tree

12 files changed

+120
-4
lines changed

12 files changed

+120
-4
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "library",
55
"require": {
66
"php": "^8.1",
7-
"artarts36/str": "^2.0.13",
7+
"artarts36/str": "^2.0.14",
88
"symfony/console": "^4.0 | ^5.0 | ^6.0",
99
"psr/http-client-implementation": "^1.0",
1010
"guzzlehttp/psr7": "^2",

mr-linter-config-schema.json

+24
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@
257257
"items": {
258258
"type": "string"
259259
}
260+
},
261+
"match": {
262+
"description": "Check if a string match a regex.",
263+
"type": "string"
260264
}
261265
}
262266
},
@@ -313,6 +317,10 @@
313317
"items": {
314318
"type": "string"
315319
}
320+
},
321+
"match": {
322+
"description": "Check if a string match a regex.",
323+
"type": "string"
316324
}
317325
}
318326
},
@@ -356,6 +364,10 @@
356364
"items": {
357365
"type": "string"
358366
}
367+
},
368+
"match": {
369+
"description": "Check if a string match a regex.",
370+
"type": "string"
359371
}
360372
}
361373
},
@@ -425,6 +437,10 @@
425437
"items": {
426438
"type": "string"
427439
}
440+
},
441+
"match": {
442+
"description": "Check if a string match a regex.",
443+
"type": "string"
428444
}
429445
}
430446
},
@@ -481,6 +497,10 @@
481497
"items": {
482498
"type": "string"
483499
}
500+
},
501+
"match": {
502+
"description": "Check if a string match a regex.",
503+
"type": "string"
484504
}
485505
}
486506
},
@@ -537,6 +557,10 @@
537557
"items": {
538558
"type": "string"
539559
}
560+
},
561+
"match": {
562+
"description": "Check if a string match a regex.",
563+
"type": "string"
540564
}
541565
}
542566
},

src/Condition/Evaluator/DefaultEvaluators.php

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ final class DefaultEvaluators
3232
CountEqualsEvaluator::NAME => CountEqualsEvaluator::class,
3333
CountNotEqualsEvaluator::NAME => CountNotEqualsEvaluator::class,
3434
CountEqualsAnyEvaluator::NAME => CountEqualsAnyEvaluator::class,
35+
MatchEvaluator::NAME => MatchEvaluator::class,
3536
];
3637

3738
/**
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Condition\Evaluator;
4+
5+
use ArtARTs36\MergeRequestLinter\Contracts\Condition\EvaluatingSubject;
6+
use ArtARTs36\MergeRequestLinter\Exception\InvalidEvaluatorValueException;
7+
use ArtARTs36\Str\Exceptions\InvalidRegexException;
8+
use ArtARTs36\Str\Facade\Str;
9+
10+
/**
11+
* Check if a string match a regex.
12+
*/
13+
class MatchEvaluator extends StringEvaluator
14+
{
15+
public const NAME = 'match';
16+
17+
protected function doEvaluate(EvaluatingSubject $subject): bool
18+
{
19+
try {
20+
return ! empty(Str::match($subject->string(), $this->value));
21+
} catch (InvalidRegexException $e) {
22+
throw new InvalidEvaluatorValueException($e->getMessage(), previous: $e);
23+
}
24+
}
25+
}

src/Console/Application/Application.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class Application extends \Symfony\Component\Console\Application
1414
{
15-
public const VERSION = '0.7.8';
15+
public const VERSION = '0.7.9';
1616

1717
public function __construct(
1818
private readonly MetricManager $metrics,

src/Contracts/Condition/ConditionEvaluator.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ArtARTs36\MergeRequestLinter\Contracts\Condition;
44

55
use ArtARTs36\MergeRequestLinter\Exception\ComparedIncompatibilityTypesException;
6+
use ArtARTs36\MergeRequestLinter\Exception\InvalidEvaluatorValueException;
67
use ArtARTs36\MergeRequestLinter\Exception\PropertyNotExists;
78

89
/**
@@ -14,6 +15,7 @@ interface ConditionEvaluator
1415
* Evaluate.
1516
* @throws ComparedIncompatibilityTypesException
1617
* @throws PropertyNotExists
18+
* @throws InvalidEvaluatorValueException
1719
*/
1820
public function evaluate(EvaluatingSubject $subject): bool;
1921
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Exception;
4+
5+
class InvalidEvaluatorValueException extends MergeRequestLinterException
6+
{
7+
//
8+
}

src/Linter/Linter.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace ArtARTs36\MergeRequestLinter\Linter;
44

55
use ArtARTs36\MergeRequestLinter\Contracts\Linter\LintEventSubscriber;
6+
use ArtARTs36\MergeRequestLinter\Contracts\Rule\Rule;
7+
use ArtARTs36\MergeRequestLinter\Exception\InvalidEvaluatorValueException;
68
use ArtARTs36\MergeRequestLinter\Exception\StopLintException;
79
use ArtARTs36\MergeRequestLinter\Note\ExceptionNote;
810
use ArtARTs36\MergeRequestLinter\Note\LintNote;
@@ -25,17 +27,20 @@ public function run(MergeRequest $request): Notes
2527

2628
$notes = [];
2729

30+
/** @var Rule $rule */
2831
foreach ($this->rules as $rule) {
2932
try {
3033
array_push($notes, ...$rule->lint($request));
3134

3235
$this->eventSubscriber->success($rule->getName());
3336
} catch (StopLintException $e) {
34-
$notes[] = new LintNote('Lint stopped. Reason: '. $e->getMessage());
37+
$notes[] = new LintNote(sprintf('[%s] Lint stopped. Reason: %s', $rule->getName(), $e->getMessage()));
3538

3639
$this->eventSubscriber->stopOn($rule->getName());
3740

3841
break;
42+
} catch (InvalidEvaluatorValueException $e) {
43+
$notes[] = new LintNote(sprintf('[%s] Invalid condition value: %s', $rule->getName(), $e->getMessage()));
3944
} catch (\Throwable $e) {
4045
$notes[] = new ExceptionNote($e);
4146

src/Request/Data/Author.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\EqualsEvaluator;
1010
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\LengthMaxEvaluator;
1111
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\LengthMinOperator;
12+
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\MatchEvaluator;
1213
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\NotEndsEvaluator;
1314
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\NotEqualsEvaluator;
1415
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\NotStartsEvaluator;
@@ -28,6 +29,7 @@ public function __construct(
2829
ContainsEvaluator::class,
2930
NotEqualsEvaluator::class,
3031
EqualsAnyEvaluator::class,
32+
MatchEvaluator::class,
3133
])]
3234
public string $login,
3335
) {

src/Request/Data/MergeRequest.php

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\HasEvaluator;
1717
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\LengthMaxEvaluator;
1818
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\LengthMinOperator;
19+
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\MatchEvaluator;
1920
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\NotEndsEvaluator;
2021
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\NotEqualsEvaluator;
2122
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\NotHasEvaluator;
@@ -44,6 +45,7 @@ public function __construct(
4445
ContainsEvaluator::class,
4546
NotEqualsEvaluator::class,
4647
EqualsAnyEvaluator::class,
48+
MatchEvaluator::class,
4749
])]
4850
public Str $title,
4951
#[SupportsConditionEvaluator([
@@ -57,6 +59,7 @@ public function __construct(
5759
ContainsEvaluator::class,
5860
NotEqualsEvaluator::class,
5961
EqualsAnyEvaluator::class,
62+
MatchEvaluator::class,
6063
])]
6164
public Str $description,
6265
#[Generic(Generic::OF_STRING)]
@@ -69,6 +72,7 @@ public function __construct(
6972
HasEvaluator::class,
7073
NotHasEvaluator::class,
7174
HasAnyEvaluator::class,
75+
MatchEvaluator::class,
7276
])]
7377
public Set $labels,
7478
#[SupportsConditionEvaluator([
@@ -85,6 +89,7 @@ public function __construct(
8589
ContainsEvaluator::class,
8690
NotEqualsEvaluator::class,
8791
EqualsAnyEvaluator::class,
92+
MatchEvaluator::class,
8893
])]
8994
public Str $sourceBranch,
9095
#[SupportsConditionEvaluator([
@@ -98,6 +103,7 @@ public function __construct(
98103
ContainsEvaluator::class,
99104
NotEqualsEvaluator::class,
100105
EqualsAnyEvaluator::class,
106+
MatchEvaluator::class,
101107
])]
102108
public Str $targetBranch,
103109
public Author $author,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Tests\Unit\Condition\Evaluator;
4+
5+
use ArtARTs36\MergeRequestLinter\Condition\Evaluator\MatchEvaluator;
6+
use ArtARTs36\MergeRequestLinter\Exception\InvalidEvaluatorValueException;
7+
use ArtARTs36\MergeRequestLinter\Tests\Mocks\MockEvaluatingSubject;
8+
use ArtARTs36\MergeRequestLinter\Tests\TestCase;
9+
10+
class MatchEvaluatorTest extends TestCase
11+
{
12+
public function providerForTestEvaluate(): array
13+
{
14+
return [
15+
['-- ab', '/ab/i', true],
16+
['-- a1b', '/ab/', false],
17+
];
18+
}
19+
20+
/**
21+
* @covers \ArtARTs36\MergeRequestLinter\Condition\Evaluator\NotStartsEvaluator::evaluate
22+
* @covers \ArtARTs36\MergeRequestLinter\Condition\Evaluator\NotStartsEvaluator::doEvaluate
23+
* @dataProvider providerForTestEvaluate
24+
*/
25+
public function testEvaluate(mixed $propertyValue, string $value, bool $expected): void
26+
{
27+
$evaluator = new MatchEvaluator($value);
28+
29+
self::assertTrue($expected === $evaluator->evaluate(new MockEvaluatingSubject($propertyValue)));
30+
}
31+
32+
/**
33+
* @covers \ArtARTs36\MergeRequestLinter\Condition\Evaluator\MatchEvaluator::doEvaluate
34+
*/
35+
public function testEvaluateOnInvalidRegexValueException(): void
36+
{
37+
self::expectException(InvalidEvaluatorValueException::class);
38+
39+
$evaluator = new MatchEvaluator('test');
40+
41+
$evaluator->evaluate(new MockEvaluatingSubject('string'));
42+
}
43+
}

tests/Unit/Linter/LinterTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getDefinition(): RuleDefinition
4242
},
4343
]), new NullLintEventSubscriber());
4444

45-
self::assertEquals('Lint stopped. Reason: Test-stop', $linter->run($this->makeMergeRequest())->first());
45+
self::assertStringContainsString('Lint stopped. Reason: Test-stop', $linter->run($this->makeMergeRequest())->first());
4646
}
4747

4848
/**

0 commit comments

Comments
 (0)