From c42a24ac5f98dfa31136be7693e43b4453357129 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 23 Nov 2026 15:19:09 +0000 Subject: [PATCH] Add regression test for comments in arrays A comment on the line before an array item, or before the value of a nested array item, was not extracted until the comment association fix in #20. This adds the example from the issue as a test. Closes #19 --- tests/PhpScannerTest.php | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/PhpScannerTest.php b/tests/PhpScannerTest.php index d00ed7a..6f0ce95 100644 --- a/tests/PhpScannerTest.php +++ b/tests/PhpScannerTest.php @@ -56,6 +56,55 @@ public function testPhpCodeScanner() $this->assertSame(['php-format'], $translation->getFlags()->toArray()); } + /** + * @see https://github.com/php-gettext/PHP-Scanner/issues/19 + */ + public function testCommentsInArrays() + { + $code = <<<'PHP' + /* PREV: Comment 2 */ __('String 2'), + 'noscript' => [ + /* PREV: Comment 3 */ + Flash::INFO => [__('String 3')] + ], + 'breadcrumb' => [ + ['text' => /* PREV: Comment 4 */ d__('admin', 'String 4'), 'link' => get_homepage_url()], + ], + 'metatags' => new Meta([ + /* PREV: Comment 5 */ + 'description' => __('String 5'), + ]), +]); +PHP; + + $scanner = new PhpScanner( + Translations::create('default'), + Translations::create('admin') + ); + $scanner->setFunctions(['__' => 'gettext', 'd__' => 'dgettext']); + $scanner->ignoreInvalidFunctions(true); + $scanner->setDefaultDomain('default'); + $scanner->extractCommentsStartingWith('PREV:'); + $scanner->scanString($code, 'file.php'); + + ['default' => $default, 'admin' => $admin] = $scanner->getTranslations(); + + foreach ([1, 2, 3, 5] as $number) { + $translation = $default->find(null, "String $number"); + $this->assertNotNull($translation); + $this->assertSame(["PREV: Comment $number"], $translation->getExtractedComments()->toArray()); + } + + $translation = $admin->find(null, 'String 4'); + $this->assertNotNull($translation); + $this->assertSame(['PREV: Comment 4'], $translation->getExtractedComments()->toArray()); + } + public function testInvalidFunction() { $this->expectException(Exception::class);