Skip to content

Commit 7934f6d

Browse files
authored
Bunch of fixes for PHP and Twig parsing (#44)
* fix PHP class inlining * fix TwigValidator with missing tokens/filters/tests * fix replacing ellipsis
1 parent b42940b commit 7934f6d

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"symfony/flex": "^1.3.1",
1919
"symfony/framework-bundle": "^5.2",
2020
"symfony/process": "^5.2",
21+
"symfony/twig-bridge": "^5.4",
2122
"symfony/yaml": "^5.2",
2223
"twig/twig": "^3.3"
2324
},

src/Service/CodeValidator/PhpValidator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ private function getParser(): Parser
4141
private function getContents(CodeNode $node, &$linesPrepended = null): string
4242
{
4343
$contents = $node->getValue();
44-
if (!preg_match('#(class|interface) [a-zA-Z]+#s', $contents) && preg_match('#(public|protected|private)( static)? (\$[a-z]+|function)#s', $contents)) {
45-
$contents = 'class Foobar {'.$contents.'}';
44+
if (
45+
!preg_match('#(class|interface) [a-zA-Z]+#s', $contents)
46+
&& !preg_match('#= new class#s', $contents)
47+
&& preg_match('#(public|protected|private)( static)? (\$[a-z]+|function).*#s', $contents, $matches)
48+
) {
49+
// keep "uses" and other code before the class definition
50+
$contents = substr($contents, 0, strpos($contents, $matches[1])).PHP_EOL.'class Foobar {'.$matches[0].'}';
4651
}
4752

4853
// Allow us to use "..." as a placeholder
49-
$contents = str_replace(['...,', '...)', '...;', '...]'], ['null,', 'null)', 'null;', 'null]'], $contents);
54+
$contents = str_replace(['...,', '...)', '...;', '...]', '... }'], ['null,', 'null)', 'null;', 'null]', '$a = null; }'], $contents);
5055

5156
$lines = explode("\n", $contents);
5257
if (!str_contains($lines[0] ?? '', '<?php') && !str_contains($lines[1] ?? '', '<?php') && !str_contains($lines[2] ?? '', '<?php')) {

src/Twig/DummyExtension.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace SymfonyTools\CodeBlockChecker\Twig;
44

5+
use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
6+
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
7+
use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
8+
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
9+
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
510
use Twig\Extension\AbstractExtension;
611
use Twig\TwigFilter;
712
use Twig\TwigFunction;
13+
use Twig\TwigTest;
814

915
/**
1016
* This extension will contain filters and functions that exists in Symfony. This
@@ -91,6 +97,27 @@ public function getFilters()
9197
new TwigFilter('markdown_to_html'),
9298
new TwigFilter('inky_to_html'),
9399
new TwigFilter('serialize'),
100+
new TwigFilter('price'),
101+
new TwigFilter('greet'),
102+
];
103+
}
104+
105+
public function getTests()
106+
{
107+
return [
108+
new TwigTest('rootform'),
109+
new TwigTest('selectedchoice'),
110+
];
111+
}
112+
113+
public function getTokenParsers()
114+
{
115+
return [
116+
new DumpTokenParser(),
117+
new FormThemeTokenParser(),
118+
new StopwatchTokenParser(false),
119+
new TransTokenParser(),
120+
new TransDefaultDomainTokenParser(),
94121
];
95122
}
96123
}

0 commit comments

Comments
 (0)