Skip to content

Commit c4aee4b

Browse files
committed
fix PHP class inlining
1 parent 7384502 commit c4aee4b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Service/CodeValidator/PhpValidator.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ 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

0 commit comments

Comments
 (0)