Skip to content

Commit b733903

Browse files
Merge pull request #905 from WordPress/update-i18n-variable-text-severity
Update i18n error codes severity
2 parents 1b32e0f + cb49631 commit b733903

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

includes/Checker/Checks/General/I18n_Usage_Check.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function get_documentation_url(): string {
9797
* @param string $docs URL for further information about the message.
9898
* @param int $severity Severity level. Default is 5.
9999
*
100+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
100101
* @SuppressWarnings(PHPMD.NPathComplexity)
101102
*/
102103
protected function add_result_message_for_file( Check_Result $result, $error, $message, $code, $file, $line = 0, $column = 0, string $docs = '', $severity = 5 ) {
@@ -132,11 +133,8 @@ protected function add_result_message_for_file( Check_Result $result, $error, $m
132133

133134
// Update severity.
134135
switch ( $code ) {
135-
case 'WordPress.WP.I18n.InterpolatedVariableDomain':
136136
case 'WordPress.WP.I18n.MissingArgText':
137137
case 'WordPress.WP.I18n.NoEmptyStrings':
138-
case 'WordPress.WP.I18n.NonSingularStringLiteralContext':
139-
case 'WordPress.WP.I18n.NonSingularStringLiteralDomain':
140138
case 'WordPress.WP.I18n.TooManyFunctionArgs':
141139
$severity = 7;
142140
break;
@@ -145,6 +143,11 @@ protected function add_result_message_for_file( Check_Result $result, $error, $m
145143
break;
146144
}
147145

146+
// Update severity for error code variations. Eg: WordPress.WP.I18n.NonSingularStringLiteralXXX.
147+
if ( str_starts_with( $code, 'WordPress.WP.I18n.InterpolatedVariable' ) || str_starts_with( $code, 'WordPress.WP.I18n.NonSingularStringLiteral' ) ) {
148+
$severity = 7;
149+
}
150+
148151
if ( 'WordPress.WP.I18n.TextDomainMismatch' === $code ) {
149152
$restricted_textdomains = $this->get_restricted_textdomains();
150153

tests/phpunit/testdata/plugins/test-plugin-i18n-usage-errors/load.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@
3434

3535
esc_html__( 'Hello World!', 'textdomain' ); // Restricted textdomain. Severity should be 7.
3636
esc_html__( 'Hello World!', 'woocommerce' ); // Severity should be default 5.
37+
38+
// Non singular string literals.
39+
echo esc_html__( $test, 'test-plugin-i18n-usage-errors' );
40+
echo _n( $single, $plural, $number, 'test-plugin-i18n-usage-errors' );
41+
echo _n_noop( $single, $plural, 'test-plugin-i18n-usage-errors' );
42+
echo _x( $text, $context, 'test-plugin-i18n-usage-errors' );
43+
44+
// Interpolated variables.
45+
echo esc_html__( "${text}", 'test-plugin-i18n-usage-errors' );
46+
echo _n( "${single}", "${plural}", $number, 'test-plugin-i18n-usage-errors' );
47+
echo _n_noop( "${single}", "${plural}", 'test-plugin-i18n-usage-errors' );
48+
echo _x( "${text}", "${context}", 'test-plugin-i18n-usage-errors' );

tests/phpunit/tests/Checker/Checks/I18n_Usage_Check_Tests.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@ public function test_run_with_errors() {
3939
// Mismatched textdomain but not restricted and with severity 5.
4040
$this->assertCount( 1, wp_list_filter( $errors['load.php'][36][29], array( 'code' => 'WordPress.WP.I18n.TextDomainMismatch' ) ) );
4141
$this->assertSame( 5, $errors['load.php'][36][29][0]['severity'] );
42+
43+
// Non singular string literal errors.
44+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][40][10], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralSingle' ) ) );
45+
$this->assertSame( 7, $errors['load.php'][40][10][0]['severity'] );
46+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][40][19], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralPlural' ) ) );
47+
$this->assertSame( 7, $errors['load.php'][40][19][0]['severity'] );
48+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][41][15], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralSingular' ) ) );
49+
$this->assertSame( 7, $errors['load.php'][41][15][0]['severity'] );
50+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][41][24], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralPlural' ) ) );
51+
$this->assertSame( 7, $errors['load.php'][41][24][0]['severity'] );
52+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][42][10], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralText' ) ) );
53+
$this->assertSame( 7, $errors['load.php'][42][10][0]['severity'] );
54+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][42][17], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralContext' ) ) );
55+
$this->assertSame( 7, $errors['load.php'][42][17][0]['severity'] );
56+
57+
// Interpolated variable errors.
58+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][45][18], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableText' ) ) );
59+
$this->assertSame( 7, $errors['load.php'][45][18][0]['severity'] );
60+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][46][10], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableSingle' ) ) );
61+
$this->assertSame( 7, $errors['load.php'][46][10][0]['severity'] );
62+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][46][23], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariablePlural' ) ) );
63+
$this->assertSame( 7, $errors['load.php'][46][23][0]['severity'] );
64+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][47][15], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableSingular' ) ) );
65+
$this->assertSame( 7, $errors['load.php'][47][15][0]['severity'] );
66+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][47][28], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariablePlural' ) ) );
67+
$this->assertSame( 7, $errors['load.php'][47][28][0]['severity'] );
68+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][48][10], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableText' ) ) );
69+
$this->assertSame( 7, $errors['load.php'][48][10][0]['severity'] );
70+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][48][21], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableContext' ) ) );
71+
$this->assertSame( 7, $errors['load.php'][48][21][0]['severity'] );
4272
}
4373

4474
public function test_run_without_errors() {

0 commit comments

Comments
 (0)