diff --git a/composer.json b/composer.json index 0bac3b1..8e9dd91 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/Service/CodeValidator/PhpValidator.php b/src/Service/CodeValidator/PhpValidator.php index 3a7a0a1..4476edb 100644 --- a/src/Service/CodeValidator/PhpValidator.php +++ b/src/Service/CodeValidator/PhpValidator.php @@ -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] ?? '', '