Skip to content

Bunch of fixes for PHP and Twig parsing #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "^5.2",
"symfony/process": "^5.2",
"symfony/twig-bridge": "^5.4",
"symfony/yaml": "^5.2",
"twig/twig": "^3.3"
},
Expand Down
11 changes: 8 additions & 3 deletions src/Service/CodeValidator/PhpValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ private function getParser(): Parser
private function getContents(CodeNode $node, &$linesPrepended = null): string
{
$contents = $node->getValue();
if (!preg_match('#(class|interface) [a-zA-Z]+#s', $contents) && preg_match('#(public|protected|private)( static)? (\$[a-z]+|function)#s', $contents)) {
$contents = 'class Foobar {'.$contents.'}';
if (
!preg_match('#(class|interface) [a-zA-Z]+#s', $contents)
&& !preg_match('#= new class#s', $contents)
&& preg_match('#(public|protected|private)( static)? (\$[a-z]+|function).*#s', $contents, $matches)
) {
// keep "uses" and other code before the class definition
$contents = substr($contents, 0, strpos($contents, $matches[1])).PHP_EOL.'class Foobar {'.$matches[0].'}';
}

// Allow us to use "..." as a placeholder
$contents = str_replace(['...,', '...)', '...;', '...]'], ['null,', 'null)', 'null;', 'null]'], $contents);
$contents = str_replace(['...,', '...)', '...;', '...]', '... }'], ['null,', 'null)', 'null;', 'null]', '$a = null; }'], $contents);

$lines = explode("\n", $contents);
if (!str_contains($lines[0] ?? '', '<?php') && !str_contains($lines[1] ?? '', '<?php') && !str_contains($lines[2] ?? '', '<?php')) {
Expand Down
27 changes: 27 additions & 0 deletions src/Twig/DummyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

namespace SymfonyTools\CodeBlockChecker\Twig;

use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;

/**
* This extension will contain filters and functions that exists in Symfony. This
Expand Down Expand Up @@ -91,6 +97,27 @@ public function getFilters()
new TwigFilter('markdown_to_html'),
new TwigFilter('inky_to_html'),
new TwigFilter('serialize'),
new TwigFilter('price'),
new TwigFilter('greet'),
];
}

public function getTests()
{
return [
new TwigTest('rootform'),
new TwigTest('selectedchoice'),
];
}

public function getTokenParsers()
{
return [
new DumpTokenParser(),
new FormThemeTokenParser(),
new StopwatchTokenParser(false),
new TransTokenParser(),
new TransDefaultDomainTokenParser(),
];
}
}