From 12306e801db6eca606b3317f190fbf0c593884dd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 3 Nov 2025 13:46:06 +0000 Subject: [PATCH 1/6] Initial plan From 2a9ad82e3ee2bb51f73e4ff958ee58a6c167c395 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 3 Nov 2025 14:00:34 +0000 Subject: [PATCH 2/6] Add line number references to file header extraction Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/FileDataExtractor.php | 31 +++++-- src/IterableCodeExtractor.php | 38 +++++--- src/MakePotCommand.php | 63 +++++++------ tests/FileDataExtractorTest.php | 155 ++++++++++++++++++++++++++++++++ 4 files changed, 241 insertions(+), 46 deletions(-) create mode 100644 tests/FileDataExtractorTest.php diff --git a/src/FileDataExtractor.php b/src/FileDataExtractor.php index 0d97c864..5a3e08c5 100644 --- a/src/FileDataExtractor.php +++ b/src/FileDataExtractor.php @@ -17,10 +17,12 @@ class FileDataExtractor { * * @param string $file Path to the file. * @param array $headers List of headers, in the format array('HeaderKey' => 'Header Name'). + * @param bool $with_line_nums Whether to include line numbers in the returned data. Default false. * - * @return array Array of file headers in `HeaderKey => Header Value` format. + * @return array Array of file headers in `HeaderKey => Header Value` format, or + * `HeaderKey => ['value' => Header Value, 'line' => Line Number]` when $with_line_nums is true. */ - public static function get_file_data( $file, $headers ) { + public static function get_file_data( $file, $headers, $with_line_nums = false ) { // We don't need to write to the file, so just open for reading. $fp = fopen( $file, 'rb' ); @@ -33,7 +35,7 @@ public static function get_file_data( $file, $headers ) { // Make sure we catch CR-only line endings. $file_data = str_replace( "\r", "\n", $file_data ); - return static::get_file_data_from_string( $file_data, $headers ); + return static::get_file_data_from_string( $file_data, $headers, $with_line_nums ); } /** @@ -41,15 +43,28 @@ public static function get_file_data( $file, $headers ) { * * @param string $text String to look for metadata in. * @param array $headers List of headers. + * @param bool $with_line_nums Whether to include line numbers in the returned data. Default false. * - * @return array Array of file headers in `HeaderKey => Header Value` format. + * @return array Array of file headers in `HeaderKey => Header Value` format, or + * `HeaderKey => ['value' => Header Value, 'line' => Line Number]` when $with_line_nums is true. */ - public static function get_file_data_from_string( $text, $headers ) { + public static function get_file_data_from_string( $text, $headers, $with_line_nums = false ) { foreach ( $headers as $field => $regex ) { - if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $text, $match ) && $match[1] ) { - $headers[ $field ] = static::_cleanup_header_comment( $match[1] ); + if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $text, $match, PREG_OFFSET_CAPTURE ) && $match[1][0] ) { + $value = static::_cleanup_header_comment( $match[1][0] ); + + if ( $with_line_nums ) { + // Calculate line number from the offset + $line_num = substr_count( $text, "\n", 0, $match[0][1] ) + 1; + $headers[ $field ] = [ + 'value' => $value, + 'line' => $line_num, + ]; + } else { + $headers[ $field ] = $value; + } } else { - $headers[ $field ] = ''; + $headers[ $field ] = $with_line_nums ? [ 'value' => '', 'line' => 0 ] : ''; } } diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index 51b3e53b..effeccaa 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -67,11 +67,16 @@ public static function fromFile( $file_or_files, Translations $translations, arr } if ( ! empty( $options['wpExtractTemplates'] ) ) { - $headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ] ); + $headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ], true ); - if ( ! empty( $headers['Template Name'] ) ) { - $translation = new Translation( '', $headers['Template Name'] ); + if ( ! empty( $headers['Template Name']['value'] ) ) { + $translation = new Translation( '', $headers['Template Name']['value'] ); $translation->addExtractedComment( 'Template Name of the theme' ); + + // Add file reference with line number if available + if ( ! empty( $headers['Template Name']['line'] ) ) { + $translation->addReference( $options['file'], $headers['Template Name']['line'] ); + } $translations[] = $translation; } @@ -84,19 +89,32 @@ public static function fromFile( $file_or_files, Translations $translations, arr [ 'Title' => 'Title', 'Description' => 'Description', - ] + ], + true ); - if ( ! empty( $headers['Title'] ) ) { - $translation = new Translation( 'Pattern title', $headers['Title'] ); - $translation->addReference( $options['file'] ); + if ( ! empty( $headers['Title']['value'] ) ) { + $translation = new Translation( 'Pattern title', $headers['Title']['value'] ); + + // Add file reference with line number if available + if ( ! empty( $headers['Title']['line'] ) ) { + $translation->addReference( $options['file'], $headers['Title']['line'] ); + } else { + $translation->addReference( $options['file'] ); + } $translations[] = $translation; } - if ( ! empty( $headers['Description'] ) ) { - $translation = new Translation( 'Pattern description', $headers['Description'] ); - $translation->addReference( $options['file'] ); + if ( ! empty( $headers['Description']['value'] ) ) { + $translation = new Translation( 'Pattern description', $headers['Description']['value'] ); + + // Add file reference with line number if available + if ( ! empty( $headers['Description']['line'] ) ) { + $translation->addReference( $options['file'], $headers['Description']['line'] ); + } else { + $translation->addReference( $options['file'] ); + } $translations[] = $translation; } diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index 8688096e..b16b193c 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -350,8 +350,8 @@ public function handle_arguments( $args, $assoc_args ) { if ( ! $ignore_domain ) { $this->domain = $this->slug; - if ( ! empty( $this->main_file_data['Text Domain'] ) ) { - $this->domain = $this->main_file_data['Text Domain']; + if ( ! empty( $this->main_file_data['Text Domain']['value'] ) ) { + $this->domain = $this->main_file_data['Text Domain']['value']; } $this->domain = Utils\get_flag_value( $assoc_args, 'domain', $this->domain ); @@ -362,12 +362,12 @@ public function handle_arguments( $args, $assoc_args ) { // Determine destination. $this->destination = "{$this->source}/{$this->slug}.pot"; - if ( ! empty( $this->main_file_data['Domain Path'] ) ) { + if ( ! empty( $this->main_file_data['Domain Path']['value'] ) ) { // Domain Path inside source folder. $this->destination = sprintf( '%s/%s/%s.pot', $this->source, - $this->unslashit( $this->main_file_data['Domain Path'] ), + $this->unslashit( $this->main_file_data['Domain Path']['value'] ), $this->slug ); } @@ -476,11 +476,12 @@ protected function get_main_file_data() { array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) - ) + ), + true ); // Stop when it contains a valid Theme Name header. - if ( ! empty( $theme_data['Theme Name'] ) ) { + if ( ! empty( $theme_data['Theme Name']['value'] ) ) { WP_CLI::log( 'Theme stylesheet detected.' ); WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $file->getRealPath() ), 'make-pot' ); @@ -498,11 +499,12 @@ protected function get_main_file_data() { array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) - ) + ), + true ); // Stop when it contains a valid Theme Name header. - if ( ! empty( $theme_data['Theme Name'] ) ) { + if ( ! empty( $theme_data['Theme Name']['value'] ) ) { WP_CLI::log( 'Theme stylesheet detected.' ); WP_CLI::debug( sprintf( 'Theme stylesheet: %s', $file->getRealPath() . '/style.css' ), 'make-pot' ); @@ -520,11 +522,12 @@ protected function get_main_file_data() { array_combine( $this->get_file_headers( 'plugin' ), $this->get_file_headers( 'plugin' ) - ) + ), + true ); // Stop when we find a file with a valid Plugin Name header. - if ( ! empty( $plugin_data['Plugin Name'] ) ) { + if ( ! empty( $plugin_data['Plugin Name']['value'] ) ) { WP_CLI::log( 'Plugin file detected.' ); WP_CLI::debug( sprintf( 'Plugin file: %s', $file->getRealPath() ), 'make-pot' ); @@ -616,11 +619,11 @@ protected function extract_strings() { // Set entries from main file data. foreach ( $this->main_file_data as $header => $data ) { - if ( empty( $data ) ) { + if ( empty( $data['value'] ) ) { continue; } - $translation = new Translation( '', $data ); + $translation = new Translation( '', $data['value'] ); if ( $is_theme ) { $translation->addExtractedComment( sprintf( '%s of the theme', $header ) ); @@ -629,9 +632,13 @@ protected function extract_strings() { } if ( $this->main_file_path && $this->location ) { - $translation->addReference( - ltrim( str_replace( Utils\normalize_path( "$this->source/" ), '', Utils\normalize_path( $this->main_file_path ) ), '/' ) - ); + $file_reference = ltrim( str_replace( Utils\normalize_path( "$this->source/" ), '', Utils\normalize_path( $this->main_file_path ) ), '/' ); + // Add line number if available + if ( ! empty( $data['line'] ) ) { + $translation->addReference( $file_reference, $data['line'] ); + } else { + $translation->addReference( $file_reference ); + } } $translations[] = $translation; @@ -920,38 +927,38 @@ protected function get_file_comment() { } if ( isset( $this->main_file_data['Theme Name'] ) ) { - if ( ! empty( $this->main_file_data['License'] ) ) { + if ( ! empty( $this->main_file_data['License']['value'] ) ) { return sprintf( "Copyright (C) %1\$s %2\$s\nThis file is distributed under the %3\$s.", date( 'Y' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - $this->main_file_data['Author'], - $this->main_file_data['License'] + $this->main_file_data['Author']['value'], + $this->main_file_data['License']['value'] ); } return sprintf( "Copyright (C) %1\$s %2\$s\nThis file is distributed under the same license as the %3\$s theme.", date( 'Y' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - $this->main_file_data['Author'], - $this->main_file_data['Theme Name'] + $this->main_file_data['Author']['value'], + $this->main_file_data['Theme Name']['value'] ); } if ( isset( $this->main_file_data['Plugin Name'] ) ) { - if ( ! empty( $this->main_file_data['License'] ) ) { + if ( ! empty( $this->main_file_data['License']['value'] ) ) { return sprintf( "Copyright (C) %1\$s %2\$s\nThis file is distributed under the %3\$s.", date( 'Y' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - $this->main_file_data['Author'], - $this->main_file_data['License'] + $this->main_file_data['Author']['value'], + $this->main_file_data['License']['value'] ); } return sprintf( "Copyright (C) %1\$s %2\$s\nThis file is distributed under the same license as the %3\$s plugin.", date( 'Y' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date - $this->main_file_data['Author'], - $this->main_file_data['Plugin Name'] + $this->main_file_data['Author']['value'], + $this->main_file_data['Plugin Name']['value'] ); } @@ -969,14 +976,14 @@ protected function set_default_headers( $translations ) { $bugs_address = null; if ( ! $version && isset( $this->main_file_data['Version'] ) ) { - $version = $this->main_file_data['Version']; + $version = $this->main_file_data['Version']['value']; } if ( isset( $this->main_file_data['Theme Name'] ) ) { - $name = $this->main_file_data['Theme Name']; + $name = $this->main_file_data['Theme Name']['value']; $bugs_address = sprintf( 'https://wordpress.org/support/theme/%s', $this->slug ); } elseif ( isset( $this->main_file_data['Plugin Name'] ) ) { - $name = $this->main_file_data['Plugin Name']; + $name = $this->main_file_data['Plugin Name']['value']; $bugs_address = sprintf( 'https://wordpress.org/support/plugin/%s', $this->slug ); } diff --git a/tests/FileDataExtractorTest.php b/tests/FileDataExtractorTest.php new file mode 100644 index 00000000..2e52343b --- /dev/null +++ b/tests/FileDataExtractorTest.php @@ -0,0 +1,155 @@ + 'Plugin Name', + 'Description' => 'Description', + 'Version' => 'Version', + ] + ); + + $this->assertEquals( 'My Plugin', $headers['Plugin Name'] ); + $this->assertEquals( 'A test plugin', $headers['Description'] ); + $this->assertEquals( '1.0.0', $headers['Version'] ); + } + + public function test_extracts_headers_with_line_numbers() { + $text = <<<'TEXT' + 'Plugin Name', + 'Description' => 'Description', + 'Version' => 'Version', + ], + true + ); + + $this->assertIsArray( $headers['Plugin Name'] ); + $this->assertEquals( 'My Plugin', $headers['Plugin Name']['value'] ); + $this->assertEquals( 3, $headers['Plugin Name']['line'] ); + + $this->assertIsArray( $headers['Description'] ); + $this->assertEquals( 'A test plugin', $headers['Description']['value'] ); + $this->assertEquals( 4, $headers['Description']['line'] ); + + $this->assertIsArray( $headers['Version'] ); + $this->assertEquals( '1.0.0', $headers['Version']['value'] ); + $this->assertEquals( 5, $headers['Version']['line'] ); + } + + public function test_line_numbers_with_different_line_endings() { + // Test with different line positions + $text = " 'Plugin Name', + 'Description' => 'Description', + ], + true + ); + + $this->assertEquals( 'Test Plugin', $headers['Plugin Name']['value'] ); + $this->assertEquals( 5, $headers['Plugin Name']['line'] ); + + $this->assertEquals( 'Description here', $headers['Description']['value'] ); + $this->assertEquals( 6, $headers['Description']['line'] ); + } + + public function test_empty_headers_with_line_numbers() { + $text = ' 'Plugin Name', + ], + true + ); + + $this->assertIsArray( $headers['Plugin Name'] ); + $this->assertEquals( '', $headers['Plugin Name']['value'] ); + $this->assertEquals( 0, $headers['Plugin Name']['line'] ); + } + + public function test_theme_headers_with_line_numbers() { + $text = <<<'TEXT' +/* +Theme Name: My Theme +Description: A beautiful theme +Author: John Doe +Version: 2.0.0 +*/ +TEXT; + + $headers = FileDataExtractor::get_file_data_from_string( + $text, + [ + 'Theme Name' => 'Theme Name', + 'Description' => 'Description', + 'Author' => 'Author', + 'Version' => 'Version', + ], + true + ); + + $this->assertEquals( 'My Theme', $headers['Theme Name']['value'] ); + $this->assertEquals( 2, $headers['Theme Name']['line'] ); + + $this->assertEquals( 'A beautiful theme', $headers['Description']['value'] ); + $this->assertEquals( 3, $headers['Description']['line'] ); + + $this->assertEquals( 'John Doe', $headers['Author']['value'] ); + $this->assertEquals( 4, $headers['Author']['line'] ); + + $this->assertEquals( '2.0.0', $headers['Version']['value'] ); + $this->assertEquals( 5, $headers['Version']['line'] ); + } + + public function test_header_with_trailing_comment() { + $text = <<<'TEXT' + 'Plugin Name', + ], + true + ); + + $this->assertEquals( 'My Plugin', $headers['Plugin Name']['value'] ); + $this->assertEquals( 3, $headers['Plugin Name']['line'] ); + } +} From 7715af13ded0bc09c55dd2e518e73340a47d2a19 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 3 Nov 2025 14:05:58 +0000 Subject: [PATCH 3/6] Update behat test to expect line numbers in file header references Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/makepot.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index c292be88..c9c4b047 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -92,7 +92,7 @@ Feature: Generate a POT file of a WordPress project """ And the foo-plugin.pot file should contain: """ - #: foo-plugin.php + #: foo-plugin.php:5 """ And the foo-plugin.pot file should contain: """ From f3c4ced4d952acc56b80c38b91ee6ffb6543a4a3 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 3 Nov 2025 18:30:22 +0100 Subject: [PATCH 4/6] Lint fix --- src/FileDataExtractor.php | 9 ++++++--- src/IterableCodeExtractor.php | 6 +++--- tests/FileDataExtractorTest.php | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/FileDataExtractor.php b/src/FileDataExtractor.php index 5a3e08c5..d9b0bd0e 100644 --- a/src/FileDataExtractor.php +++ b/src/FileDataExtractor.php @@ -52,10 +52,10 @@ public static function get_file_data_from_string( $text, $headers, $with_line_nu foreach ( $headers as $field => $regex ) { if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $text, $match, PREG_OFFSET_CAPTURE ) && $match[1][0] ) { $value = static::_cleanup_header_comment( $match[1][0] ); - + if ( $with_line_nums ) { // Calculate line number from the offset - $line_num = substr_count( $text, "\n", 0, $match[0][1] ) + 1; + $line_num = substr_count( $text, "\n", 0, $match[0][1] ) + 1; $headers[ $field ] = [ 'value' => $value, 'line' => $line_num, @@ -64,7 +64,10 @@ public static function get_file_data_from_string( $text, $headers, $with_line_nu $headers[ $field ] = $value; } } else { - $headers[ $field ] = $with_line_nums ? [ 'value' => '', 'line' => 0 ] : ''; + $headers[ $field ] = $with_line_nums ? [ + 'value' => '', + 'line' => 0, + ] : ''; } } diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index effeccaa..f361b512 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -72,7 +72,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr if ( ! empty( $headers['Template Name']['value'] ) ) { $translation = new Translation( '', $headers['Template Name']['value'] ); $translation->addExtractedComment( 'Template Name of the theme' ); - + // Add file reference with line number if available if ( ! empty( $headers['Template Name']['line'] ) ) { $translation->addReference( $options['file'], $headers['Template Name']['line'] ); @@ -95,7 +95,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr if ( ! empty( $headers['Title']['value'] ) ) { $translation = new Translation( 'Pattern title', $headers['Title']['value'] ); - + // Add file reference with line number if available if ( ! empty( $headers['Title']['line'] ) ) { $translation->addReference( $options['file'], $headers['Title']['line'] ); @@ -108,7 +108,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr if ( ! empty( $headers['Description']['value'] ) ) { $translation = new Translation( 'Pattern description', $headers['Description']['value'] ); - + // Add file reference with line number if available if ( ! empty( $headers['Description']['line'] ) ) { $translation->addReference( $options['file'], $headers['Description']['line'] ); diff --git a/tests/FileDataExtractorTest.php b/tests/FileDataExtractorTest.php index 2e52343b..6e8e80dc 100644 --- a/tests/FileDataExtractorTest.php +++ b/tests/FileDataExtractorTest.php @@ -128,7 +128,7 @@ public function test_theme_headers_with_line_numbers() { $this->assertEquals( 'John Doe', $headers['Author']['value'] ); $this->assertEquals( 4, $headers['Author']['line'] ); - + $this->assertEquals( '2.0.0', $headers['Version']['value'] ); $this->assertEquals( 5, $headers['Version']['line'] ); } From 30cf2ac6af537290b679be92abe1d8f1f1fa8b2c Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 3 Nov 2025 18:31:10 +0100 Subject: [PATCH 5/6] Fix test --- features/makepot.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/makepot.feature b/features/makepot.feature index c9c4b047..6221be59 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -3890,12 +3890,12 @@ Feature: Generate a POT file of a WordPress project And the foo-theme/foo-theme.pot file should exist And the foo-theme/foo-theme.pot file should contain: """ - #: patterns/my-pattern.php + #: patterns/my-pattern.php:3 msgctxt "Pattern title" msgid "My pattern title." msgstr "" - #: patterns/my-pattern.php + #: patterns/my-pattern.php:4 msgctxt "Pattern description" msgid "My pattern description." msgstr "" From 61019ddf1c1c560285dcbdc077d5a653d4ff7458 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Dec 2025 12:56:14 +0000 Subject: [PATCH 6/6] Simplify implementation to always collect line numbers Remove the optional $with_line_nums parameter as suggested in review. The function now always returns the array format with 'value' and 'line' keys, simplifying the API and reducing conditional logic. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- src/FileDataExtractor.php | 34 +++++++++++----------------- src/IterableCodeExtractor.php | 5 ++--- src/MakePotCommand.php | 9 +++----- tests/FileDataExtractorTest.php | 39 +++++---------------------------- 4 files changed, 23 insertions(+), 64 deletions(-) diff --git a/src/FileDataExtractor.php b/src/FileDataExtractor.php index d9b0bd0e..f724a6bf 100644 --- a/src/FileDataExtractor.php +++ b/src/FileDataExtractor.php @@ -17,12 +17,10 @@ class FileDataExtractor { * * @param string $file Path to the file. * @param array $headers List of headers, in the format array('HeaderKey' => 'Header Name'). - * @param bool $with_line_nums Whether to include line numbers in the returned data. Default false. * - * @return array Array of file headers in `HeaderKey => Header Value` format, or - * `HeaderKey => ['value' => Header Value, 'line' => Line Number]` when $with_line_nums is true. + * @return array Array of file headers in `HeaderKey => ['value' => Header Value, 'line' => Line Number]` format. */ - public static function get_file_data( $file, $headers, $with_line_nums = false ) { + public static function get_file_data( $file, $headers ) { // We don't need to write to the file, so just open for reading. $fp = fopen( $file, 'rb' ); @@ -35,7 +33,7 @@ public static function get_file_data( $file, $headers, $with_line_nums = false ) // Make sure we catch CR-only line endings. $file_data = str_replace( "\r", "\n", $file_data ); - return static::get_file_data_from_string( $file_data, $headers, $with_line_nums ); + return static::get_file_data_from_string( $file_data, $headers ); } /** @@ -43,31 +41,25 @@ public static function get_file_data( $file, $headers, $with_line_nums = false ) * * @param string $text String to look for metadata in. * @param array $headers List of headers. - * @param bool $with_line_nums Whether to include line numbers in the returned data. Default false. * - * @return array Array of file headers in `HeaderKey => Header Value` format, or - * `HeaderKey => ['value' => Header Value, 'line' => Line Number]` when $with_line_nums is true. + * @return array Array of file headers in `HeaderKey => ['value' => Header Value, 'line' => Line Number]` format. */ - public static function get_file_data_from_string( $text, $headers, $with_line_nums = false ) { + public static function get_file_data_from_string( $text, $headers ) { foreach ( $headers as $field => $regex ) { if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $text, $match, PREG_OFFSET_CAPTURE ) && $match[1][0] ) { $value = static::_cleanup_header_comment( $match[1][0] ); - if ( $with_line_nums ) { - // Calculate line number from the offset - $line_num = substr_count( $text, "\n", 0, $match[0][1] ) + 1; - $headers[ $field ] = [ - 'value' => $value, - 'line' => $line_num, - ]; - } else { - $headers[ $field ] = $value; - } + // Calculate line number from the offset + $line_num = substr_count( $text, "\n", 0, $match[0][1] ) + 1; + $headers[ $field ] = [ + 'value' => $value, + 'line' => $line_num, + ]; } else { - $headers[ $field ] = $with_line_nums ? [ + $headers[ $field ] = [ 'value' => '', 'line' => 0, - ] : ''; + ]; } } diff --git a/src/IterableCodeExtractor.php b/src/IterableCodeExtractor.php index f361b512..112264ed 100644 --- a/src/IterableCodeExtractor.php +++ b/src/IterableCodeExtractor.php @@ -67,7 +67,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr } if ( ! empty( $options['wpExtractTemplates'] ) ) { - $headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ], true ); + $headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ] ); if ( ! empty( $headers['Template Name']['value'] ) ) { $translation = new Translation( '', $headers['Template Name']['value'] ); @@ -89,8 +89,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr [ 'Title' => 'Title', 'Description' => 'Description', - ], - true + ] ); if ( ! empty( $headers['Title']['value'] ) ) { diff --git a/src/MakePotCommand.php b/src/MakePotCommand.php index b16b193c..3d14245d 100644 --- a/src/MakePotCommand.php +++ b/src/MakePotCommand.php @@ -476,8 +476,7 @@ protected function get_main_file_data() { array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) - ), - true + ) ); // Stop when it contains a valid Theme Name header. @@ -499,8 +498,7 @@ protected function get_main_file_data() { array_combine( $this->get_file_headers( 'theme' ), $this->get_file_headers( 'theme' ) - ), - true + ) ); // Stop when it contains a valid Theme Name header. @@ -522,8 +520,7 @@ protected function get_main_file_data() { array_combine( $this->get_file_headers( 'plugin' ), $this->get_file_headers( 'plugin' ) - ), - true + ) ); // Stop when we find a file with a valid Plugin Name header. diff --git a/tests/FileDataExtractorTest.php b/tests/FileDataExtractorTest.php index 6e8e80dc..f2663eb4 100644 --- a/tests/FileDataExtractorTest.php +++ b/tests/FileDataExtractorTest.php @@ -6,30 +6,6 @@ use WP_CLI\Tests\TestCase; class FileDataExtractorTest extends TestCase { - public function test_extracts_headers_without_line_numbers() { - $text = <<<'TEXT' - 'Plugin Name', - 'Description' => 'Description', - 'Version' => 'Version', - ] - ); - - $this->assertEquals( 'My Plugin', $headers['Plugin Name'] ); - $this->assertEquals( 'A test plugin', $headers['Description'] ); - $this->assertEquals( '1.0.0', $headers['Version'] ); - } - public function test_extracts_headers_with_line_numbers() { $text = <<<'TEXT' 'Plugin Name', 'Description' => 'Description', 'Version' => 'Version', - ], - true + ] ); $this->assertIsArray( $headers['Plugin Name'] ); @@ -72,8 +47,7 @@ public function test_line_numbers_with_different_line_endings() { [ 'Plugin Name' => 'Plugin Name', 'Description' => 'Description', - ], - true + ] ); $this->assertEquals( 'Test Plugin', $headers['Plugin Name']['value'] ); @@ -90,8 +64,7 @@ public function test_empty_headers_with_line_numbers() { $text, [ 'Plugin Name' => 'Plugin Name', - ], - true + ] ); $this->assertIsArray( $headers['Plugin Name'] ); @@ -116,8 +89,7 @@ public function test_theme_headers_with_line_numbers() { 'Description' => 'Description', 'Author' => 'Author', 'Version' => 'Version', - ], - true + ] ); $this->assertEquals( 'My Theme', $headers['Theme Name']['value'] ); @@ -145,8 +117,7 @@ public function test_header_with_trailing_comment() { $text, [ 'Plugin Name' => 'Plugin Name', - ], - true + ] ); $this->assertEquals( 'My Plugin', $headers['Plugin Name']['value'] );