Skip to content

Commit

Permalink
Ensure translatable strings, and string domains are not dynamic, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shadyvb committed Dec 12, 2013
1 parent b46eeda commit 242c05f
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Sniffs/WP/I18nSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* WordPress_Sniffs_WP_I18nSniff
*
* Makes sure internationalization functions are used properly
*
* @category PHP
* @package PHP_CodeSniffer
* @author Shady Sharaf <[email protected]>
*/
class WordPress_Sniffs_WP_I18nSniff implements PHP_CodeSniffer_Sniff
{

public $i18n_functions = array(
'translate',
'translate_with_gettext_context',
'__',
'esc_attr__',
'esc_html__',
'_e',
'esc_attr_e',
'esc_html_e',
'_x',
'_ex',
'esc_attr_x',
'esc_html_x',
'_n',
'_nx',
'_n_noop',
'_nx_noop',
);

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_STRING,
);

}//end register()


/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

if ( ! in_array( $token['content'], $this->i18n_functions ) ) {
return;
}

if ( $nextToken = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ) ) {
if ( $tokens[$nextToken]['code'] != T_OPEN_PARENTHESIS ) {
return;
}
}

// Get arguments
for ( $i = $nextToken + 1; $i < $tokens[$nextToken]['parenthesis_closer'] - 1; $i++ ) {
if ( in_array( $tokens[$i]['code'], array( T_WHITESPACE, T_COMMA,T_CONSTANT_ENCAPSED_STRING ) ) ) {
continue;
}

if ( $tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING ) {
$string = $tokens[$i]['content'];
if ( preg_match( '#\$#', $string ) > 0 ) {
$phpcsFile->addError( 'Translatable strings should not contain variables, found ' . $tokens[$i]['content'], $i );
return;
}
continue;
}

$phpcsFile->addError( sprintf( 'Translatable string expected, but found "%s"', $tokens[$i]['content'] ), $i );
return;
}

}//end process()


}//end class
9 changes: 9 additions & 0 deletions Tests/WP/I18nUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

__( "hell$varo", 'domain' ); // Bad, shouldn't use a string with variables

__( "hell\$varo", 'domain' ); // Bad, shouldn't use a string with variables

__( $var, 'domain' ); // Bad, shouldn't use variables

__( 'string', SOMETHING ); // Bad, shouldn't use CONSTANTS
49 changes: 49 additions & 0 deletions Tests/WP/I18nUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Unit test class for the EnqueuedResources sniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Shady Sharaf <[email protected]>
*/
class WordPress_Tests_WP_I18nUnitTest extends AbstractSniffUnitTest
{


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array(int => int)
*/
public function getErrorList()
{
return array(
3 => 1,
7 => 1,
9 => 1,
);

}//end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array(int => int)
*/
public function getWarningList()
{
return array();

}//end getWarningList()


}//end class

0 comments on commit 242c05f

Please sign in to comment.