diff --git a/phpunit/code/preprocessor/namespace_ending_comment_unbracketed.php b/phpunit/code/preprocessor/namespace_ending_comment_unbracketed.php new file mode 100644 index 00000000..7ad8b333 --- /dev/null +++ b/phpunit/code/preprocessor/namespace_ending_comment_unbracketed.php @@ -0,0 +1,14 @@ +assertSame('App\\VERSION', $constants['_const_var_App__VERSION']->name); } + public function testNamespaceEndingCommentWithUnbracketedSyntaxIsIgnored(): void + { + global $translator; + $file = __DIR__ . '/../code/preprocessor/namespace_ending_comment_unbracketed.php'; + $previousTranslator = $translator ?? null; + $translator = $this->compiler; + + try { + $this->compiler->addFiles([$file]); + $this->compiler->prepareFile($file); + $this->compiler->convertFile($file); + } finally { + $translator = $previousTranslator; + } + + $constants = $this->getProperty('constants'); + $this->assertArrayHasKey('_const_var_NamespaceEndingComment__VALUE', $constants); + } + public function testSortFilesUsesImplementsAndTraitDependencies(): void { $classFile = realpath(__DIR__ . '/../code/preprocessor/deps_class_implements.php'); diff --git a/src/Preprocessor.php b/src/Preprocessor.php index 2d311e00..9e0d5d11 100644 --- a/src/Preprocessor.php +++ b/src/Preprocessor.php @@ -321,6 +321,8 @@ class Preprocessor extends CompilerBase case 'Stmt_Interface': $this->parseInterface($v2); break; + case 'Stmt_Nop': + break; default: $this->foundStrayCode($v2); break; diff --git a/src/Translator.php b/src/Translator.php index 14916aec..402568ea 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -2514,6 +2514,8 @@ CODE; case 'Stmt_Interface': $this->validateInterfaceOverrideAttributes($v2); break; + case 'Stmt_Nop': + break; default: abort($v2); break; diff --git a/tests/compiler/namespace/namespace-ending-comment.phpt b/tests/compiler/namespace/namespace-ending-comment.phpt new file mode 100644 index 00000000..e937a711 --- /dev/null +++ b/tests/compiler/namespace/namespace-ending-comment.phpt @@ -0,0 +1,22 @@ +--TEST-- +A namespace block ending with a comment must not be treated as stray code +--FILE-- + +--EXPECT-- +string(4) "done"