Skip to content

Commit 61019dd

Browse files
Copilotswissspidy
andcommitted
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>
1 parent 30cf2ac commit 61019dd

File tree

4 files changed

+23
-64
lines changed

4 files changed

+23
-64
lines changed

src/FileDataExtractor.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ class FileDataExtractor {
1717
*
1818
* @param string $file Path to the file.
1919
* @param array $headers List of headers, in the format array('HeaderKey' => 'Header Name').
20-
* @param bool $with_line_nums Whether to include line numbers in the returned data. Default false.
2120
*
22-
* @return array Array of file headers in `HeaderKey => Header Value` format, or
23-
* `HeaderKey => ['value' => Header Value, 'line' => Line Number]` when $with_line_nums is true.
21+
* @return array Array of file headers in `HeaderKey => ['value' => Header Value, 'line' => Line Number]` format.
2422
*/
25-
public static function get_file_data( $file, $headers, $with_line_nums = false ) {
23+
public static function get_file_data( $file, $headers ) {
2624
// We don't need to write to the file, so just open for reading.
2725
$fp = fopen( $file, 'rb' );
2826

@@ -35,39 +33,33 @@ public static function get_file_data( $file, $headers, $with_line_nums = false )
3533
// Make sure we catch CR-only line endings.
3634
$file_data = str_replace( "\r", "\n", $file_data );
3735

38-
return static::get_file_data_from_string( $file_data, $headers, $with_line_nums );
36+
return static::get_file_data_from_string( $file_data, $headers );
3937
}
4038

4139
/**
4240
* Retrieves metadata from a string.
4341
*
4442
* @param string $text String to look for metadata in.
4543
* @param array $headers List of headers.
46-
* @param bool $with_line_nums Whether to include line numbers in the returned data. Default false.
4744
*
48-
* @return array Array of file headers in `HeaderKey => Header Value` format, or
49-
* `HeaderKey => ['value' => Header Value, 'line' => Line Number]` when $with_line_nums is true.
45+
* @return array Array of file headers in `HeaderKey => ['value' => Header Value, 'line' => Line Number]` format.
5046
*/
51-
public static function get_file_data_from_string( $text, $headers, $with_line_nums = false ) {
47+
public static function get_file_data_from_string( $text, $headers ) {
5248
foreach ( $headers as $field => $regex ) {
5349
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $text, $match, PREG_OFFSET_CAPTURE ) && $match[1][0] ) {
5450
$value = static::_cleanup_header_comment( $match[1][0] );
5551

56-
if ( $with_line_nums ) {
57-
// Calculate line number from the offset
58-
$line_num = substr_count( $text, "\n", 0, $match[0][1] ) + 1;
59-
$headers[ $field ] = [
60-
'value' => $value,
61-
'line' => $line_num,
62-
];
63-
} else {
64-
$headers[ $field ] = $value;
65-
}
52+
// Calculate line number from the offset
53+
$line_num = substr_count( $text, "\n", 0, $match[0][1] ) + 1;
54+
$headers[ $field ] = [
55+
'value' => $value,
56+
'line' => $line_num,
57+
];
6658
} else {
67-
$headers[ $field ] = $with_line_nums ? [
59+
$headers[ $field ] = [
6860
'value' => '',
6961
'line' => 0,
70-
] : '';
62+
];
7163
}
7264
}
7365

src/IterableCodeExtractor.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr
6767
}
6868

6969
if ( ! empty( $options['wpExtractTemplates'] ) ) {
70-
$headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ], true );
70+
$headers = FileDataExtractor::get_file_data_from_string( $text, [ 'Template Name' => 'Template Name' ] );
7171

7272
if ( ! empty( $headers['Template Name']['value'] ) ) {
7373
$translation = new Translation( '', $headers['Template Name']['value'] );
@@ -89,8 +89,7 @@ public static function fromFile( $file_or_files, Translations $translations, arr
8989
[
9090
'Title' => 'Title',
9191
'Description' => 'Description',
92-
],
93-
true
92+
]
9493
);
9594

9695
if ( ! empty( $headers['Title']['value'] ) ) {

src/MakePotCommand.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,7 @@ protected function get_main_file_data() {
476476
array_combine(
477477
$this->get_file_headers( 'theme' ),
478478
$this->get_file_headers( 'theme' )
479-
),
480-
true
479+
)
481480
);
482481

483482
// Stop when it contains a valid Theme Name header.
@@ -499,8 +498,7 @@ protected function get_main_file_data() {
499498
array_combine(
500499
$this->get_file_headers( 'theme' ),
501500
$this->get_file_headers( 'theme' )
502-
),
503-
true
501+
)
504502
);
505503

506504
// Stop when it contains a valid Theme Name header.
@@ -522,8 +520,7 @@ protected function get_main_file_data() {
522520
array_combine(
523521
$this->get_file_headers( 'plugin' ),
524522
$this->get_file_headers( 'plugin' )
525-
),
526-
true
523+
)
527524
);
528525

529526
// Stop when we find a file with a valid Plugin Name header.

tests/FileDataExtractorTest.php

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,6 @@
66
use WP_CLI\Tests\TestCase;
77

88
class FileDataExtractorTest extends TestCase {
9-
public function test_extracts_headers_without_line_numbers() {
10-
$text = <<<'TEXT'
11-
<?php
12-
/**
13-
* Plugin Name: My Plugin
14-
* Description: A test plugin
15-
* Version: 1.0.0
16-
*/
17-
TEXT;
18-
19-
$headers = FileDataExtractor::get_file_data_from_string(
20-
$text,
21-
[
22-
'Plugin Name' => 'Plugin Name',
23-
'Description' => 'Description',
24-
'Version' => 'Version',
25-
]
26-
);
27-
28-
$this->assertEquals( 'My Plugin', $headers['Plugin Name'] );
29-
$this->assertEquals( 'A test plugin', $headers['Description'] );
30-
$this->assertEquals( '1.0.0', $headers['Version'] );
31-
}
32-
339
public function test_extracts_headers_with_line_numbers() {
3410
$text = <<<'TEXT'
3511
<?php
@@ -46,8 +22,7 @@ public function test_extracts_headers_with_line_numbers() {
4622
'Plugin Name' => 'Plugin Name',
4723
'Description' => 'Description',
4824
'Version' => 'Version',
49-
],
50-
true
25+
]
5126
);
5227

5328
$this->assertIsArray( $headers['Plugin Name'] );
@@ -72,8 +47,7 @@ public function test_line_numbers_with_different_line_endings() {
7247
[
7348
'Plugin Name' => 'Plugin Name',
7449
'Description' => 'Description',
75-
],
76-
true
50+
]
7751
);
7852

7953
$this->assertEquals( 'Test Plugin', $headers['Plugin Name']['value'] );
@@ -90,8 +64,7 @@ public function test_empty_headers_with_line_numbers() {
9064
$text,
9165
[
9266
'Plugin Name' => 'Plugin Name',
93-
],
94-
true
67+
]
9568
);
9669

9770
$this->assertIsArray( $headers['Plugin Name'] );
@@ -116,8 +89,7 @@ public function test_theme_headers_with_line_numbers() {
11689
'Description' => 'Description',
11790
'Author' => 'Author',
11891
'Version' => 'Version',
119-
],
120-
true
92+
]
12193
);
12294

12395
$this->assertEquals( 'My Theme', $headers['Theme Name']['value'] );
@@ -145,8 +117,7 @@ public function test_header_with_trailing_comment() {
145117
$text,
146118
[
147119
'Plugin Name' => 'Plugin Name',
148-
],
149-
true
120+
]
150121
);
151122

152123
$this->assertEquals( 'My Plugin', $headers['Plugin Name']['value'] );

0 commit comments

Comments
 (0)