Skip to content

Commit

Permalink
Add the check for the leading and trailing spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
amieiro committed Nov 4, 2024
1 parent 2133137 commit 27715e8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
44 changes: 44 additions & 0 deletions WordPress/Sniffs/WP/I18nSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
$has_content = $this->check_string_has_translatable_content( $matched_content, $param_name, $param_info );
if ( true === $has_content ) {
$this->check_string_has_no_html_wrapper( $matched_content, $param_name, $param_info );
$this->check_string_has_no_leading_trailing_spaces( $matched_content, $param_name, $param_info );
}
}
}
Expand Down Expand Up @@ -804,6 +805,49 @@ private function check_string_has_no_html_wrapper( $matched_content, $param_name
}
}

/**
* Check if a translatable string has leading or trailing spaces.
*
* @since 3.2.0
*
* @param string $matched_content The token content (function name) which was matched
* in lowercase.
* @param string $param_name The name of the parameter being examined.
* @param array|false $param_info Parameter info array for an individual parameter,
* as received from the PassedParameters class.
*
* @return void
*/
private function check_string_has_no_leading_trailing_spaces( $matched_content, $param_name, $param_info ) {
// Strip surrounding quotes.
$content_without_quotes = TextStrings::stripQuotes( $param_info['clean'] );
$first_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, $param_info['start'], ( $param_info['end'] + 1 ), true );

Check warning on line 824 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

if ( ltrim( $content_without_quotes ) !== $content_without_quotes ) {
$this->phpcsFile->addError(
'Translatable string should not have leading spaces. Found: %s',
$first_non_empty,
'NoLeadingTrailingSpaces',
array( $param_info['clean'] )
);
}
if ( rtrim( $content_without_quotes ) !== $content_without_quotes ) {
$this->phpcsFile->addError(
'Translatable string should not have trailing spaces. Found: %s',
$first_non_empty,
'NoLeadingTrailingSpaces',
array( $param_info['clean'] )
);
}

if ('cadena' == $param_info['raw']) {

Check failure on line 843 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

No space after opening parenthesis is prohibited

Check warning on line 843 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Loose comparisons are not allowed. Expected: "==="; Found: "=="

Check failure on line 843 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

No space before closing parenthesis is prohibited
fwrite(STDERR, "\n" . '$matched_content: ' . print_r($matched_content, TRUE));

Check failure on line 844 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Expected 1 spaces after opening parenthesis; 0 found

Check warning on line 844 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

print_r() found. Debug code should not normally be used in production.

Check failure on line 844 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 844 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

TRUE, FALSE and NULL must be lowercase; expected "true" but found "TRUE"

Check failure on line 844 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 844 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Expected 1 spaces before closing parenthesis; 0 found
fwrite(STDERR, "\n" . '$param_name: ' . print_r($param_name, TRUE));

Check failure on line 845 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Expected 1 spaces after opening parenthesis; 0 found

Check warning on line 845 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

print_r() found. Debug code should not normally be used in production.

Check failure on line 845 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 845 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

TRUE, FALSE and NULL must be lowercase; expected "true" but found "TRUE"
fwrite(STDERR, "\n" . '$param_info: ' . print_r($param_info, TRUE));

Check warning on line 846 in WordPress/Sniffs/WP/I18nSniff.php

View workflow job for this annotation

GitHub Actions / Run code sniffs

print_r() found. Debug code should not normally be used in production.
exit(1);
}
}

/**
* Check for inconsistencies in the placeholders between single and plural form of the translatable text string.
*
Expand Down
23 changes: 23 additions & 0 deletions WordPress/Tests/WP/I18nUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,27 @@ esc_html_e( 'foo', '' ); // Bad: text-domain can not be empty.
// PHP 8.0+: safeguard handling of newly introduced placeholders.
__( 'There are %1$h monkeys in the %H', 'my-slug' ); // Bad: multiple arguments should be numbered.

__( ' string', 'my-slug' ); // Bad: leading space.
_e( 'string ', 'my-slug' ); // Bad: trailing space.
_x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces.
_ex( ' string', 'context', 'my-slug' ); // Bad: leading spaces.
_n( ' There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys', $number, 'my-slug' ); // Bad: leading space.
_n( 'There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys ', $number, 'my-slug' ); // Bad: trailing space.
_n( ' There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys ', $number, 'my-slug' ); // Bad: leading and trailing spaces.
_nx( ' I have %d cat.', 'I have %d cats.', $number, 'Not really.', 'my-slug' ); // Bad: leading spaces.
_nx( 'I have %d cat.', 'I have %d cats. ', $number, 'Not really.', 'my-slug' ); // Bad: trailing spaces.
_nx( ' I have %d cat.', 'I have %d cats. ', $number, 'Not really.', 'my-slug' ); // Bad: leading and trailing spaces.
_n_noop( ' I have %d cat.', 'I have %d cats.', 'my-slug' ); // Bad: leading space.
_n_noop( 'I have %d cat.', 'I have %d cats. ', 'my-slug' ); // Bad: trailing space.
_n_noop( ' I have %d cat.', 'I have %d cats. ', 'my-slug' ); // Bad: leading and trailing spaces.
_nx_noop( ' I have %d cat.', 'I have %d cats.', 'Not really.', 'my-slug' ); // Bad: leading spaces.
_nx_noop( 'I have %d cat.', 'I have %d cats. ', 'Not really.', 'my-slug' ); // Bad: trailing spaces.
_nx_noop( ' I have %d cat.', 'I have %d cats. ', 'Not really.', 'my-slug' ); // Bad: leading and trailing spaces.
esc_html__( ' string', 'my-slug' ); // Bad: leading space.
esc_html_e( 'string ', 'my-slug' ); // Bad: trailing space.
esc_html_x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces.
esc_attr__( ' string', 'my-slug' ); // Bad: leading space.
esc_attr_e( 'string ', 'my-slug' ); // Bad: trailing space.
esc_attr_x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces.

// phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment
23 changes: 23 additions & 0 deletions WordPress/Tests/WP/I18nUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,27 @@ esc_html_e( 'foo', '' ); // Bad: text-domain can not be empty.
// PHP 8.0+: safeguard handling of newly introduced placeholders.
__( 'There are %1$h monkeys in the %H', 'my-slug' ); // Bad: multiple arguments should be numbered.

__( ' string', 'my-slug' ); // Bad: leading space.
_e( 'string ', 'my-slug' ); // Bad: trailing space.
_x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces.
_ex( ' string', 'context', 'my-slug' ); // Bad: leading spaces.
_n( ' There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys', $number, 'my-slug' ); // Bad: leading space.
_n( 'There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys ', $number, 'my-slug' ); // Bad: trailing space.
_n( ' There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys ', $number, 'my-slug' ); // Bad: leading and trailing spaces.
_nx( ' I have %d cat.', 'I have %d cats.', $number, 'Not really.', 'my-slug' ); // Bad: leading spaces.
_nx( 'I have %d cat.', 'I have %d cats. ', $number, 'Not really.', 'my-slug' ); // Bad: trailing spaces.
_nx( ' I have %d cat.', 'I have %d cats. ', $number, 'Not really.', 'my-slug' ); // Bad: leading and trailing spaces.
_n_noop( ' I have %d cat.', 'I have %d cats.', 'my-slug' ); // Bad: leading space.
_n_noop( 'I have %d cat.', 'I have %d cats. ', 'my-slug' ); // Bad: trailing space.
_n_noop( ' I have %d cat.', 'I have %d cats. ', 'my-slug' ); // Bad: leading and trailing spaces.
_nx_noop( ' I have %d cat.', 'I have %d cats.', 'Not really.', 'my-slug' ); // Bad: leading spaces.
_nx_noop( 'I have %d cat.', 'I have %d cats. ', 'Not really.', 'my-slug' ); // Bad: trailing spaces.
_nx_noop( ' I have %d cat.', 'I have %d cats. ', 'Not really.', 'my-slug' ); // Bad: leading and trailing spaces.
esc_html__( ' string', 'my-slug' ); // Bad: leading space.
esc_html_e( 'string ', 'my-slug' ); // Bad: trailing space.
esc_html_x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces.
esc_attr__( ' string', 'my-slug' ); // Bad: leading space.
esc_attr_e( 'string ', 'my-slug' ); // Bad: trailing space.
esc_attr_x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces.

// phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment
22 changes: 22 additions & 0 deletions WordPress/Tests/WP/I18nUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ public function getErrorList( $testFile = '' ) {
311 => 1,
315 => 1,
318 => 1,
320 => 1,
321 => 1,
322 => 2,
323 => 1,
324 => 1,
325 => 1,
326 => 2,
327 => 1,
328 => 1,
329 => 2,
330 => 1,
331 => 1,
332 => 2,
333 => 1,
334 => 1,
335 => 2,
336 => 1,
337 => 1,
338 => 2,
339 => 1,
340 => 1,
341 => 2,
);

case 'I18nUnitTest.2.inc':
Expand Down

0 comments on commit 27715e8

Please sign in to comment.