From c459f670720b7906f0345fd0f65d3de3d42e8de1 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 24 Mar 2016 16:13:38 -0700 Subject: [PATCH] Merge multi-line string literal tokens into single token Fixes #50. --- WordPress/Sniffs/WP/I18nSniff.php | 19 ++++++++++++++++--- WordPress/Tests/WP/I18nUnitTest.inc | 7 +++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/WordPress/Sniffs/WP/I18nSniff.php b/WordPress/Sniffs/WP/I18nSniff.php index e7a7baa771..bc311b9b47 100644 --- a/WordPress/Sniffs/WP/I18nSniff.php +++ b/WordPress/Sniffs/WP/I18nSniff.php @@ -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. @@ -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 ); @@ -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; diff --git a/WordPress/Tests/WP/I18nUnitTest.inc b/WordPress/Tests/WP/I18nUnitTest.inc index d4c4b2b45e..d7d340504f 100644 --- a/WordPress/Tests/WP/I18nUnitTest.inc +++ b/WordPress/Tests/WP/I18nUnitTest.inc @@ -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.