Skip to content

Commit 99fb8bb

Browse files
committed
Flag attempt to escape function which prints it's output
Fixes #120
1 parent 5909ca2 commit 99fb8bb

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* WordPress-VIP-Minimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
* @link https://github.com/Automattic/VIP-Coding-Standards
7+
*/
8+
9+
namespace WordPressVIPMinimum\Sniffs\VIP;
10+
11+
use PHP_CodeSniffer_File as File;
12+
use PHP_CodeSniffer_Tokens as Tokens;
13+
14+
/**
15+
* Flag suspicious WP_Query and get_posts params.
16+
*
17+
* @package VIPCS\WordPressVIPMinimum
18+
*/
19+
class EscapingVoidReturnFunctionsSniff implements \PHP_CodeSniffer_Sniff {
20+
21+
/**
22+
* Returns an array of tokens this test wants to listen for.
23+
*
24+
* @return array
25+
*/
26+
public function register() {
27+
return array(
28+
T_STRING,
29+
);
30+
}
31+
32+
/**
33+
* Process this test when one of its tokens is encountered
34+
*
35+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
37+
*
38+
* @return void
39+
*/
40+
public function process( File $phpcsFile, $stackPtr ) {
41+
42+
$tokens = $phpcsFile->getTokens();
43+
44+
if ( 0 !== strpos( $tokens[ $stackPtr ]['content'], 'esc_' ) && 0 !== strpos( $tokens[ $stackPtr ]['content'], 'wp_kses' ) ) {
45+
// Not what we are looking for.
46+
return;
47+
}
48+
49+
$next_token = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
50+
51+
if ( T_OPEN_PARENTHESIS !== $tokens[ $next_token ]['code'] ) {
52+
// Not a function call.
53+
return;
54+
}
55+
56+
$next_token = $phpcsFile->findNext( Tokens::$emptyTokens, ( $next_token + 1 ), null, true );
57+
58+
if ( T_STRING !== $tokens[ $next_token ]['code'] ) {
59+
// Not what we are looking for.
60+
return;
61+
}
62+
63+
if ( 0 === strpos( $tokens[ $next_token ]['content'], '_e' ) ) {
64+
$phpcsFile->addError( sprintf( 'Attempting to escape %s() which is printing it\'s output.', $tokens[ $next_token ]['content'] ), $stackPtr, 'escapingVoidReturningFunction' );
65+
return;
66+
}
67+
}
68+
69+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
esc_html( _e( $something ) ); // NOK.
4+
esc_html( __( $something ) ); // NOK.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Unit test class for WordPressVIPMinimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
*/
7+
8+
namespace WordPressVIPMinimum\Tests\VIP;
9+
10+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
11+
12+
/**
13+
* Unit test class for the EscapingVoidReturnFunctions sniff.
14+
*
15+
* @package VIPCS\WordPressVIPMinimum
16+
*/
17+
class EscapingVoidReturnFunctionsUnitTest extends AbstractSniffUnitTest {
18+
19+
/**
20+
* Returns the lines where errors should occur.
21+
*
22+
* @return array <int line number> => <int number of errors>
23+
*/
24+
public function getErrorList() {
25+
return array(
26+
3 => 1,
27+
);
28+
}
29+
30+
/**
31+
* Returns the lines where warnings should occur.
32+
*
33+
* @return array <int line number> => <int number of warnings>
34+
*/
35+
public function getWarningList() {
36+
return array();
37+
}
38+
39+
} // End class.

0 commit comments

Comments
 (0)