Skip to content

Commit 851df88

Browse files
committed
New Rule: EnsureAttributeBetweenBackticksInContent
1 parent 51ebc17 commit 851df88

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

docs/rules.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [deprecated_directive_major_version](#deprecated_directive_major_version)
2020
* [deprecated_directive_min_version](#deprecated_directive_min_version)
2121
* [deprecated_directive_should_have_version](#deprecated_directive_should_have_version)
22+
* [ensure_attribute_between_backticks_in_content](#ensure_attribute_between_backticks_in_content)
2223
* [ensure_bash_prompt_before_composer_command](#ensure_bash_prompt_before_composer_command)
2324
* [ensure_class_constant](#ensure_class_constant)
2425
* [ensure_correct_format_for_phpfunction](#ensure_correct_format_for_phpfunction)
@@ -403,6 +404,22 @@ Name | Required
403404
- Rule class: [App\Rule\DeprecatedDirectiveShouldHaveVersion](https://github.com/OskarStark/doctor-rst/blob/develop/src/Rule/DeprecatedDirectiveShouldHaveVersion.php)
404405
- Test class: [App\Tests\Rule\DeprecatedDirectiveShouldHaveVersionTest](https://github.com/OskarStark/doctor-rst/blob/develop/tests/Rule/DeprecatedDirectiveShouldHaveVersionTest.php)
405406

407+
## `ensure_attribute_between_backticks_in_content`
408+
409+
> _Make sure to use backticks around attribute in content._
410+
411+
##### Valid Examples :+1:
412+
413+
```rst
414+
Use ``#[Route]`` to define route
415+
```
416+
417+
##### Invalid Examples :-1:
418+
419+
```rst
420+
Use #[Route] to define route
421+
```
422+
406423
## `ensure_bash_prompt_before_composer_command`
407424

408425
> _Make sure Composer command in a terminal/bash code block is prefixed with a $._
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of DOCtor-RST.
7+
*
8+
* (c) Oskar Stark <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace App\Rule;
15+
16+
use App\Attribute\Rule\Description;
17+
use App\Attribute\Rule\InvalidExample;
18+
use App\Attribute\Rule\ValidExample;
19+
use App\Rst\RstParser;
20+
use App\Value\Lines;
21+
use App\Value\NullViolation;
22+
use App\Value\Violation;
23+
use App\Value\ViolationInterface;
24+
25+
#[Description('Make sure to use backticks around attributes in content')]
26+
#[InvalidExample('Use #[Route] to define route')]
27+
#[ValidExample('Use ``#[Route]`` to define route')]
28+
class EnsureAttributeBetweenBackticksInContent extends AbstractRule implements LineContentRule
29+
{
30+
public function check(Lines $lines, int $number, string $filename): ViolationInterface
31+
{
32+
$lines->seek($number);
33+
$line = $lines->current();
34+
35+
if (RstParser::codeBlockDirectiveIsTypeOf($line, RstParser::CODE_BLOCK_PHP)
36+
|| RstParser::codeBlockDirectiveIsTypeOf($line, RstParser::CODE_BLOCK_PHP_ANNOTATIONS, true)
37+
|| RstParser::codeBlockDirectiveIsTypeOf($line, RstParser::CODE_BLOCK_PHP_ATTRIBUTES, true)
38+
|| RstParser::codeBlockDirectiveIsTypeOf($line, RstParser::CODE_BLOCK_PHP_SYMFONY, true)
39+
|| RstParser::codeBlockDirectiveIsTypeOf($line, RstParser::CODE_BLOCK_PHP_STANDALONE, true)
40+
|| RstParser::codeBlockDirectiveIsTypeOf($line, RstParser::CODE_BLOCK_HTML_PHP, true)
41+
) {
42+
return NullViolation::create();
43+
}
44+
45+
if ($line->raw()->match('/(?<!`)#\[[^\]]*\](?!`)/')) {
46+
return Violation::from(
47+
\sprintf('Please ensure to use backticks "%s"', $line->raw()->toString()),
48+
$filename,
49+
$number + 1,
50+
$line,
51+
);
52+
}
53+
54+
return NullViolation::create();
55+
}
56+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of DOCtor-RST.
7+
*
8+
* (c) Oskar Stark <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace App\Tests\Rule;
15+
16+
use App\Rule\EnsureAttributeBetweenBackticksInContent;
17+
use App\Rule\PhpOpenTagInCodeBlockPhpDirective;
18+
use App\Tests\RstSample;
19+
use App\Tests\UnitTestCase;
20+
use App\Value\NullViolation;
21+
use App\Value\Violation;
22+
use App\Value\ViolationInterface;
23+
use PHPUnit\Framework\Attributes\DataProvider;
24+
use PHPUnit\Framework\Attributes\Test;
25+
26+
final class EnsureAttributeBetweenBackticksInContentTest extends UnitTestCase
27+
{
28+
#[Test]
29+
#[DataProvider('checkProvider')]
30+
public function check(ViolationInterface $expected, RstSample $sample): void
31+
{
32+
self::assertEquals(
33+
$expected,
34+
(new EnsureAttributeBetweenBackticksInContent())->check($sample->lines, $sample->lineNumber, 'filename'),
35+
);
36+
}
37+
38+
public static function checkProvider(): iterable
39+
{
40+
foreach (self::phpCodeBlocks() as $codeBlock) {
41+
yield \sprintf('No violation for code-block "%s"', $codeBlock) => [
42+
NullViolation::create(),
43+
new RstSample([
44+
$codeBlock,
45+
'#[AsEventListener]'
46+
]),
47+
];
48+
}
49+
50+
yield \sprintf('Has violation without backticks') => [
51+
Violation::from(
52+
\sprintf('Please ensure to use backticks "use #[MapEntity] attributes"'),
53+
'filename',
54+
1,
55+
'use #[MapEntity] attributes',
56+
),
57+
new RstSample('use #[MapEntity] attributes')
58+
];
59+
60+
yield \sprintf('Has no violation') => [
61+
NullViolation::create(),
62+
new RstSample('use ``#[MapEntity]`` attributes')
63+
];
64+
}
65+
}

0 commit comments

Comments
 (0)