Skip to content

Commit f29fe59

Browse files
committed
WIP SanitizationHelperTrait: update for PHPCS 4.0
Need to check if this way is the best way to fix the problem and improve it (document, make more readable).
1 parent 87f4d90 commit f29fe59

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

WordPress/Helpers/ArrayWalkingFunctionsHelper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function get_functions() {
7171
* @return bool
7272
*/
7373
public static function is_array_walking_function( $functionName ) {
74-
return isset( self::$arrayWalkingFunctions[ strtolower( $functionName ) ] );
74+
return isset( self::$arrayWalkingFunctions[ strtolower( ltrim( $functionName, '\\' ) ) ] );
7575
}
7676

7777
/**
@@ -94,6 +94,11 @@ public static function get_callback_parameter( File $phpcsFile, $stackPtr ) {
9494
}
9595

9696
$functionName = strtolower( $tokens[ $stackPtr ]['content'] );
97+
98+
if ( \T_NAME_FULLY_QUALIFIED === $tokens[ $stackPtr ]['code'] ) {
99+
$functionName = ltrim( $functionName, '\\' );
100+
}
101+
97102
if ( isset( self::$arrayWalkingFunctions[ $functionName ] ) === false ) {
98103
return false;
99104
}

WordPress/Helpers/SanitizationHelperTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ final public function get_sanitizing_and_unslashing_functions() {
239239
* @return bool
240240
*/
241241
final public function is_sanitizing_function( $functionName ) {
242-
return isset( $this->get_sanitizing_functions()[ strtolower( $functionName ) ] );
242+
return isset( $this->get_sanitizing_functions()[ strtolower( ltrim( $functionName, '\\' ) ) ] );
243243
}
244244

245245
/**
@@ -252,7 +252,7 @@ final public function is_sanitizing_function( $functionName ) {
252252
* @return bool
253253
*/
254254
final public function is_sanitizing_and_unslashing_function( $functionName ) {
255-
return isset( $this->get_sanitizing_and_unslashing_functions()[ strtolower( $functionName ) ] );
255+
return isset( $this->get_sanitizing_and_unslashing_functions()[ strtolower( ltrim( $functionName, '\\' ) ) ] );
256256
}
257257

258258
/**

WordPress/Helpers/UnslashingFunctionsHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ public static function get_functions() {
5454
* @return bool
5555
*/
5656
public static function is_unslashing_function( $functionName ) {
57-
return isset( self::$unslashingFunctions[ strtolower( $functionName ) ] );
57+
return isset( self::$unslashingFunctions[ strtolower( ltrim( $functionName, '\\' ) ) ] );
5858
}
5959
}

WordPress/Tests/Helpers/SanitizationHelperTrait/IsSanitizedUnitTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace WordPressCS\WordPress\Tests\Helpers\SanitizationHelperTrait;
1111

1212
use PHP_CodeSniffer\Files\File;
13+
use PHPCSUtils\BackCompat\Helper;
1314
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
1415
use WordPressCS\WordPress\Helpers\SanitizationHelperTrait;
1516

@@ -84,6 +85,9 @@ public function testIsSanitized( $testMarker, $expectedResult ) {
8485
* @see testIsSanitized()
8586
*/
8687
public static function dataIsSanitized() {
88+
$phpcs_version = Helper::getVersion();
89+
$is_phpcs_4 = version_compare( $phpcs_version, '3.99.99', '>' );
90+
8791
return array(
8892
'not_sanitized_echo' => array(
8993
'testMarker' => '/* testNotSanitizedEcho */',
@@ -165,18 +169,21 @@ public static function dataIsSanitized() {
165169
'testMarker' => '/* testFullyQualifiedUnslashThenSanitized */',
166170
'expectedResult' => true,
167171
),
172+
173+
// These are false positives in PHPCS 3.x. See: https://github.com/WordPress/WordPress-Coding-Standards/issues/2665.
168174
'namespaced_unslash_but_still_sanitized_1' => array(
169175
'testMarker' => '/* testNamespacedUnslashButStillSanitized1 */',
170-
'expectedResult' => true,
176+
'expectedResult' => ( true === $is_phpcs_4 ) ? false : true,
171177
),
172178
'namespaced_unslash_but_still_sanitized_2' => array(
173179
'testMarker' => '/* testNamespacedUnslashButStillSanitized2 */',
174-
'expectedResult' => true,
180+
'expectedResult' => ( true === $is_phpcs_4 ) ? false : true,
175181
),
176182
'namespaced_unslash_but_still_sanitized_3' => array(
177183
'testMarker' => '/* testNamespacedUnslashButStillSanitized3 */',
178-
'expectedResult' => true,
184+
'expectedResult' => ( true === $is_phpcs_4 ) ? false : true,
179185
),
186+
180187
'int_cast' => array(
181188
'testMarker' => '/* testIntCast */',
182189
'expectedResult' => true,

0 commit comments

Comments
 (0)