From 78ed4e109a201339d60682a4c88f06ae38cbc4c5 Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Sat, 20 Feb 2016 21:43:51 +0000 Subject: [PATCH] i18n-tools: Respect the coding standards when adding textdomains with add-textdomain.php. Props GaryJ, groovecoder. Fixes #21616. git-svn-id: https://develop.svn.wordpress.org/trunk@36603 602fd350-edb4-49c9-b593-d223f7449a82 --- tools/i18n/add-textdomain.php | 88 ++++++++++++++----- tools/i18n/t/AddTextdomainTest.php | 28 +++++- tools/i18n/t/data/add-textdomain-0-result.php | 32 +++---- tools/i18n/t/data/add-textdomain-0.php | 2 +- 4 files changed, 106 insertions(+), 44 deletions(-) diff --git a/tools/i18n/add-textdomain.php b/tools/i18n/add-textdomain.php index 14eabf1991..a4b1dd4a2f 100644 --- a/tools/i18n/add-textdomain.php +++ b/tools/i18n/add-textdomain.php @@ -17,40 +17,78 @@ class AddTextdomain { /** * Constructor. */ - function __construct() { + public function __construct() { $makepot = new MakePOT; $this->funcs = array_keys( $makepot->rules ); $this->funcs[] = 'translate_nooped_plural'; } - function usage() { + /** + * Prints CLI usage. + */ + public function usage() { $usage = "Usage: php add-textdomain.php [-i] \n\nAdds the string as a last argument to all i18n function calls in \nand prints the modified php file on standard output.\n\nOptions:\n -i Modifies the PHP file in place, instead of printing it to standard output.\n"; fwrite(STDERR, $usage); exit(1); } - function process_token($token_text, $inplace) { - if ($inplace) - $this->modified_contents .= $token_text; - else - echo $token_text; + /** + * Adds textdomain to a single file. + * + * @see AddTextdomain::process_string() + * + * @param string $domain Text domain. + * @param string $source_filename Filename with optional path. + * @param bool $inplace True to modifies the PHP file in place. False to print to standard output. + */ + public function process_file( $domain, $source_filename, $inplace ) { + $new_source = $this->process_string( $domain, file_get_contents( $source_filename ) ); + + if ( $inplace ) { + $f = fopen( $source_filename, 'w' ); + fwrite( $f, $new_source ); + fclose( $f ); + } else { + echo $new_source; + } } + /** + * Adds textdomain to a string of PHP. + * + * Functions calls should be wrapped in opening and closing PHP delimiters as usual. + * + * @see AddTextdomain::process_tokens() + * + * @param string $domain Text domain. + * @param string $string PHP code to parse. + * @return string Modified source. + */ + public function process_string( $domain, $string ) { + $tokens = token_get_all( $string ); + return $this->process_tokens( $domain, $tokens ); + } - function process_file($domain, $source_filename, $inplace) { - + /** + * Adds textdomain to a set of PHP tokens. + * + * @param string $domain Text domain. + * @param array $tokens PHP tokens. An array of token identifiers. Each individual token identifier is either a + * single character (i.e.: ;, ., >, !, etc.), or a three element array containing the token + * index in element 0, the string content of the original token in element 1 and the line + * number in element 2. + * @return string Modified source. + */ + public function process_tokens( $domain, $tokens ) { $this->modified_contents = ''; - $domain = addslashes($domain); - - $source = file_get_contents($source_filename); - $tokens = token_get_all($source); + $domain = addslashes( $domain ); $in_func = false; $args_started = false; $parens_balance = 0; $found_domain = false; - foreach($tokens as $token) { + foreach($tokens as $index => $token) { $string_success = false; if (is_array($token)) { list($id, $text) = $token; @@ -71,26 +109,28 @@ class AddTextdomain { } elseif (')' == $token) { --$parens_balance; if ($in_func && 0 == $parens_balance) { - $token = $found_domain? ')' : ", '$domain')"; + if ( ! $found_domain ) { + $token = ", '$domain'"; + if ( T_WHITESPACE == $tokens[ $index - 1 ][0] ) { + $token .= ' '; // Maintain code standards if previously present + // Remove previous whitespace token to account for it. + $this->modified_contents = trim( $this->modified_contents ); + } + $token .= ')'; + } $in_func = false; $args_started = false; $found_domain = false; } } - $this->process_token($token, $inplace); + $this->modified_contents .= $token; } - if ($inplace) { - $f = fopen($source_filename, 'w'); - fwrite($f, $this->modified_contents); - fclose($f); - } + return $this->modified_contents; } } - -// run the CLI only if the file -// wasn't included +// Run the CLI only if the file wasn't included. $included_files = get_included_files(); if ($included_files[0] == __FILE__) { $adddomain = new AddTextdomain(); diff --git a/tools/i18n/t/AddTextdomainTest.php b/tools/i18n/t/AddTextdomainTest.php index 1c991dd363..22fb1dece7 100755 --- a/tools/i18n/t/AddTextdomainTest.php +++ b/tools/i18n/t/AddTextdomainTest.php @@ -10,15 +10,37 @@ require_once dirname( dirname( __FILE__ ) ) . '/add-textdomain.php'; class AddTextDomainTest extends PHPUnit_Framework_TestCase { - function __construct() { - $this->atd = new AddTextdomain; + function setUp() { + parent::setUp(); + $this->atd = new AddTextdomain(); } function test_add() { - # copy to a new file, so that we don't corrupt the old one + // Copy to a new file, so that we don't corrupt the old one. copy( 'data/add-textdomain-0.php', 'data/add-textdomain-0-work.php' ); $this->atd->process_file( 'test-domain', 'data/add-textdomain-0-work.php', true ); $this->assertEquals( file_get_contents( 'data/add-textdomain-0-result.php' ), file_get_contents( 'data/add-textdomain-0-work.php' ) ); unlink( 'data/add-textdomain-0-work.php' ); } + + /** + * @dataProvider data_textdomain_sources + */ + function test_basic_add_textdomain( $source, $expected ) { + $tokens = token_get_all( $source ); + $result = $this->atd->process_tokens( 'foo', $tokens ); + $this->assertEquals( $expected, $result ); + } + + function data_textdomain_sources() { + return array( + array( "", "" ), // Simple single quotes + array( '', "" ), // Simple double quotes + array( "", "" ), // Simple single quotes CS + array( '', "" ), // Simple double quotes CS + array( "", "" ), // Multiple string args + array( '', '' ), // Multiple string / var args + array( "", "" ), // Existing textdomain + ); + } } diff --git a/tools/i18n/t/data/add-textdomain-0-result.php b/tools/i18n/t/data/add-textdomain-0-result.php index a6a1257b97..d518a9de17 100755 --- a/tools/i18n/t/data/add-textdomain-0-result.php +++ b/tools/i18n/t/data/add-textdomain-0-result.php @@ -1,18 +1,18 @@