8000 Misc fixes discovered when parsing every error in Symfony docs (#18) · Nyholm/code-block-checker@39a7a8d · GitHub
[go: up one dir, main page]

Skip to content

Commit 39a7a8d

Browse files
authored
Misc fixes discovered when parsing every error in Symfony docs (symfony-tools#18)
* Only run config/packages * Append .rst * Add missing twig filters * Use better line on twig error * More twig extensions * Support for interfaces and static methods * Added tests * Added test for PHP * Bugfix * Add line number for Yaml * cs
1 parent 886c6ad commit 39a7a8d

File tree

8 files changed

+85
-6
lines changed

8 files changed

+85
-6
lines changed

src/Command/CheckDocsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130130
$this->io->error(sprintf('Build completed with %s errors', $issueCount));
131131
} elseif ('github' === $format) {
132132
foreach ($issues as $issue) {
133-
$this->io->writeln(sprintf('::error file=%s.rst,line=%s::[%s] %s', $issue->getFile(), $issue->getLine(), $issue->getType(), str_replace(PHP_EOL, ' ', $issue->getText())));
133+
$this->io->writeln(sprintf('::error file=%s,line=%s::[%s] %s', $issue->getFile(), $issue->getLine(), $issue->getType(), str_replace(PHP_EOL, ' ', $issue->getText())));
134134
}
135135
}
136136

src/Issue/Issue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function getType(): string
5656

5757
public function getFile(): string
5858
{
59-
return $this->file;
59+
return $this->file.'.rst';
6060
}
6161

6262
public function getLine(): int

src/Service/CodeNodeRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function runNodes(array $nodes, string $applicationDirectory): IssueColle
3131
private function processNode(CodeNode $node, IssueCollection $issues, string $applicationDirectory): void
3232
{
3333
$file = $this->getFile($node);
34-
if ('config/' !== substr($file, 0, 7)) {
34+
if ('config/packages/' !== substr($file, 0, 16)) {
3535
return;
3636
}
3737

src/Service/CodeValidator/PhpValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function validate(CodeNode $node, IssueCollection $issues): void
1818

1919
$file = sys_get_temp_dir().'/'.uniqid('doc_builder', true).'.php';
2020
$contents = $node->getValue();
21-
if (!preg_match('#class [a-zA-Z]+#s', $contents) && preg_match('#(public|protected|private) (\$[a-z]+|function)#s', $contents)) {
21+
if (!preg_match('#(class|interface) [a-zA-Z]+#s', $contents) && preg_match('#(public|protected|private)( static)? (\$[a-z]+|function)#s', $contents)) {
2222
$contents = 'class Foobar {'.$contents.'}';
2323
}
2424

src/Service/CodeValidator/TwigValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function validate(CodeNode $node, IssueCollection $issues): void
3131
// We cannot parse the TokenStream because we dont have all extensions loaded.
3232
$this->twig->parse($tokens);
3333
} catch (SyntaxError $e) {
34-
$issues->addIssue(new Issue($node, $e->getMessage(), 'Invalid syntax', $node->getEnvironment()->getCurrentFileName(), 0));
34+
$issues->addIssue(new Issue($node, $e->getRawMessage(), 'Twig', $node->getEnvironment()->getCurrentFileName(), $e->getTemplateLine()));
3535
}
3636
}
3737
}

src/Service/CodeValidator/YamlValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function validate(CodeNode $node, IssueCollection $issues): void
2525
return;
2626
}
2727

28-
$issues->addIssue(new Issue($node, $e->getMessage(), 'Invalid syntax', $node->getEnvironment()->getCurrentFileName(), 0));
28+
$issues->addIssue(new Issue($node, $e->getMessage(), 'Invalid syntax', $node->getEnvironment()->getCurrentFileName(), $e->getParsedLine()));
2929
}
3030
}
3131
}

src/Twig/DummyExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public function getFunctions()
5555
new TwigFunction('workflow_marked_places'),
5656
new TwigFunction('workflow_metadata'),
5757
new TwigFunction('workflow_transition_blockers'),
58+
new TwigFunction('encore_entry_link_tags'),
59+
new TwigFunction('encore_entry_script_tags'),
5860
];
5961
}
6062

@@ -77,6 +79,9 @@ public function getFilters()
7779
new TwigFilter('yaml_dump'),
7880
new TwigFilter('trans'),
7981
new TwigFilter('transchoice'),
82+
new TwigFilter('inline_css'),
83+
new TwigFilter('markdown_to_html'),
84+
new TwigFilter('inky_to_html'),
8085
];
8186
}
8287
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Symfony\CodeBlockChecker\Tests\Service\Validator;
4+
5+
use Doctrine\RST\Configuration;
6+
use Doctrine\RST\Environment;
7+
use Doctrine\RST\Nodes\CodeNode;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\CodeBlockChecker\Issue\IssueCollection;
10+
use Symfony\CodeBlockChecker\Service\CodeValidator\PhpValidator;
11+
use Symfony\CodeBlockChecker\Service\CodeValidator\Validator;
12+
13+
class PhpValidatorTest extends TestCase
14+
{
15+
private Validator $validator;
16+
private Environment $environment;
17+
18+
protected function setUp(): void
19+
{
20+
$this->environment = new Environment(new Configuration());
21+
$this->validator = new PhpValidator();
22+
}
23+
24+
/**
25+
* @dataProvider getCodeExamples
26+
*/
27+
public function testCodeExamples(int $errors, string $code)
28+
{
29+
$node = new CodeNode(explode(PHP_EOL, $code));
30+
$node->setEnvironment($this->environment);
31+
$node->setLanguage('php');
32+
$issues = new IssueCollection();
33+
$this->validator->validate($node, $issues);
34+
$this->assertCount($errors, $issues);
35+
}
36+
37+
public function getCodeExamples(): iterable
38+
{
39+
yield [0, '
40+
namespace Symfony\Component\HttpKernel;
41+
42+
use Symfony\Component\HttpFoundation\Request;
43+
44+
interface HttpKernelInterface
45+
{
46+
// ...
47+
48+
/**
49+
* @return Response A Response instance
50+
*/
51+
public function handle(
52+
Request $request,
53+
$type = self::MASTER_REQUEST,
54+
$catch = true
55+
);
56+
}
57+
'];
58+
yield [0, '
59+
public static function validate($object, ExecutionContextInterface $context, $payload)
60+
{
61+
// somehow you have an array of "fake names"
62+
$fakeNames = [/* ... */];
63+
64+
// check if the name is actually a fake name
65+
if (in_array($object->getFirstName(), $fakeNames)) {
66+
$context->buildViolation("This name sounds totally fake!")
67+
->atPath("firstName")
68+
->addViolation()
69+
;
70+
}
71+
}
72+
'];
73+
}
74+
}

0 commit comments

Comments
 (0)
0