-
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure translatable strings, and string domains are not dynamic, fixes …
- Loading branch information
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |