Skip to content

Commit

Permalink
Merge multi-line string literal tokens into single token
Browse files Browse the repository at this point in the history
Fixes #50.
  • Loading branch information
westonruter authored and JDGrimes committed Oct 7, 2016
1 parent 66110ec commit 0b57fb2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Sniffs/WP/I18nSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public function process( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) {
$argument_tokens = array();
continue;
}

// Merge consecutive single or double quoted strings (when they span multiple lines).
if ( T_CONSTANT_ENCAPSED_STRING === $this_token['code'] || T_DOUBLE_QUOTED_STRING === $this_token['code'] ) {
for ( $j = $i + 1; $j < $tokens[ $func_open_paren_token ]['parenthesis_closer']; $j += 1 ) {
if ( $this_token['code'] === $tokens[ $j ]['code'] ) {
$this_token['content'] .= $tokens[ $j ]['content'];
$i = $j;
} else {
break;
}
}
}
$argument_tokens[] = $this_token;

// Include everything up to and including the parenthesis_closer if this token has one.
Expand All @@ -106,16 +118,16 @@ public function process( PHP_CodeSniffer_File $phpcs_file, $stack_ptr ) {
if ( 'simple' === $this->i18n_functions[ $translation_function ] ) {
$argument_assertions[] = array( 'arg_name' => 'text', 'tokens' => array_shift( $arguments_tokens ) );
$argument_assertions[] = array( 'arg_name' => 'domain', 'tokens' => array_shift( $arguments_tokens ), 'warning' => true );
} else if ( 'context' === $this->i18n_functions[ $translation_function ] ) {
} elseif ( 'context' === $this->i18n_functions[ $translation_function ] ) {
$argument_assertions[] = array( 'arg_name' => 'text', 'tokens' => array_shift( $arguments_tokens ) );
$argument_assertions[] = array( 'arg_name' => 'context', 'tokens' => array_shift( $arguments_tokens ) );
$argument_assertions[] = array( 'arg_name' => 'domain', 'tokens' => array_shift( $arguments_tokens ), 'warning' => true );
} else if ( 'number' === $this->i18n_functions[ $translation_function ] ) {
} elseif ( 'number' === $this->i18n_functions[ $translation_function ] ) {
$argument_assertions[] = array( 'arg_name' => 'single', 'tokens' => array_shift( $arguments_tokens ) );
$argument_assertions[] = array( 'arg_name' => 'plural', 'tokens' => array_shift( $arguments_tokens ) );
array_shift( $arguments_tokens );
$argument_assertions[] = array( 'arg_name' => 'domain', 'tokens' => array_shift( $arguments_tokens ), 'warning' => true );
} else if ( 'number_context' === $this->i18n_functions[ $translation_function ] ) {
} elseif ( 'number_context' === $this->i18n_functions[ $translation_function ] ) {
$argument_assertions[] = array( 'arg_name' => 'single', 'tokens' => array_shift( $arguments_tokens ) );
$argument_assertions[] = array( 'arg_name' => 'plural', 'tokens' => array_shift( $arguments_tokens ) );
array_shift( $arguments_tokens );
Expand Down Expand Up @@ -175,6 +187,7 @@ protected function check_argument_tokens( PHP_CodeSniffer_File $phpcs_file, $con
}
return false;
}

$code = 'NonSingularStringLiteral' . ucfirst( $arg_name );
$phpcs_file->$method( 'The $%s arg should be single a string literal, not "%s".', $stack_ptr, $code, array( $arg_name, $tokens[0]['content'] ) );
return false;
Expand Down
7 changes: 7 additions & 0 deletions Tests/WP/I18nUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ _x( 'string', 'context', 'domain', 'too-many-args' ); // Bad
_n( 'I have a cat.', 'I have cats.', $number, 'my-slug', 'too-many-args' ); // Bad
_n_noop( 'I have a cat.', 'I have cats.', $number, 'my-slug', 'too-many-args' ); // Bad.
_nx_noop( 'I have a cat.', 'I have cats.', $number, 'Not really.', 'my-slug', 'too-many-args' ); // Bad.

// Make sure that multi-line string literals are accepted.
_nx( 'I have
a cat.', 'I have
cats.', $number, 'Not
really.', 'my-
slug' ); // OK.

0 comments on commit 0b57fb2

Please sign in to comment.