Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions includes/Checker/Checks/General/I18n_Usage_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function get_documentation_url(): string {
* @param string $docs URL for further information about the message.
* @param int $severity Severity level. Default is 5.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function add_result_message_for_file( Check_Result $result, $error, $message, $code, $file, $line = 0, $column = 0, string $docs = '', $severity = 5 ) {
Expand Down Expand Up @@ -132,11 +133,8 @@ protected function add_result_message_for_file( Check_Result $result, $error, $m

// Update severity.
switch ( $code ) {
case 'WordPress.WP.I18n.InterpolatedVariableDomain':
case 'WordPress.WP.I18n.MissingArgText':
case 'WordPress.WP.I18n.NoEmptyStrings':
case 'WordPress.WP.I18n.NonSingularStringLiteralContext':
case 'WordPress.WP.I18n.NonSingularStringLiteralDomain':
case 'WordPress.WP.I18n.TooManyFunctionArgs':
$severity = 7;
break;
Expand All @@ -145,6 +143,11 @@ protected function add_result_message_for_file( Check_Result $result, $error, $m
break;
}

// Update severity for error code variations. Eg: WordPress.WP.I18n.NonSingularStringLiteralXXX.
if ( str_starts_with( $code, 'WordPress.WP.I18n.InterpolatedVariable' ) || str_starts_with( $code, 'WordPress.WP.I18n.NonSingularStringLiteral' ) ) {
$severity = 7;
}

if ( 'WordPress.WP.I18n.TextDomainMismatch' === $code ) {
$restricted_textdomains = $this->get_restricted_textdomains();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@

esc_html__( 'Hello World!', 'textdomain' ); // Restricted textdomain. Severity should be 7.
esc_html__( 'Hello World!', 'woocommerce' ); // Severity should be default 5.

// Non singular string literals.
echo esc_html__( $test, 'test-plugin-i18n-usage-errors' );
echo _n( $single, $plural, $number, 'test-plugin-i18n-usage-errors' );
echo _n_noop( $single, $plural, 'test-plugin-i18n-usage-errors' );
echo _x( $text, $context, 'test-plugin-i18n-usage-errors' );

// Interpolated variables.
echo esc_html__( "${text}", 'test-plugin-i18n-usage-errors' );
echo _n( "${single}", "${plural}", $number, 'test-plugin-i18n-usage-errors' );
echo _n_noop( "${single}", "${plural}", 'test-plugin-i18n-usage-errors' );
echo _x( "${text}", "${context}", 'test-plugin-i18n-usage-errors' );
30 changes: 30 additions & 0 deletions tests/phpunit/tests/Checker/Checks/I18n_Usage_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ public function test_run_with_errors() {
// Mismatched textdomain but not restricted and with severity 5.
$this->assertCount( 1, wp_list_filter( $errors['load.php'][36][29], array( 'code' => 'WordPress.WP.I18n.TextDomainMismatch' ) ) );
$this->assertSame( 5, $errors['load.php'][36][29][0]['severity'] );

// Non singular string literal errors.
$this->assertCount( 1, wp_list_filter( $errors['load.php'][40][10], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralSingle' ) ) );
$this->assertSame( 7, $errors['load.php'][40][10][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][40][19], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralPlural' ) ) );
$this->assertSame( 7, $errors['load.php'][40][19][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][41][15], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralSingular' ) ) );
$this->assertSame( 7, $errors['load.php'][41][15][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][41][24], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralPlural' ) ) );
$this->assertSame( 7, $errors['load.php'][41][24][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][42][10], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralText' ) ) );
$this->assertSame( 7, $errors['load.php'][42][10][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][42][17], array( 'code' => 'WordPress.WP.I18n.NonSingularStringLiteralContext' ) ) );
$this->assertSame( 7, $errors['load.php'][42][17][0]['severity'] );

// Interpolated variable errors.
$this->assertCount( 1, wp_list_filter( $errors['load.php'][45][18], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableText' ) ) );
$this->assertSame( 7, $errors['load.php'][45][18][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][46][10], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableSingle' ) ) );
$this->assertSame( 7, $errors['load.php'][46][10][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][46][23], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariablePlural' ) ) );
$this->assertSame( 7, $errors['load.php'][46][23][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][47][15], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableSingular' ) ) );
$this->assertSame( 7, $errors['load.php'][47][15][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][47][28], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariablePlural' ) ) );
$this->assertSame( 7, $errors['load.php'][47][28][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][48][10], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableText' ) ) );
$this->assertSame( 7, $errors['load.php'][48][10][0]['severity'] );
$this->assertCount( 1, wp_list_filter( $errors['load.php'][48][21], array( 'code' => 'WordPress.WP.I18n.InterpolatedVariableContext' ) ) );
$this->assertSame( 7, $errors['load.php'][48][21][0]['severity'] );
}

public function test_run_without_errors() {
Expand Down