From 359d901417b6a83ed8b22100f96e0c20d07ca195 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Tue, 30 Sep 2025 15:29:42 +0530 Subject: [PATCH 1/3] ContextHelper::is_in_type_test(): add tests --- .../ContextHelper/IsInTypeTestUnitTest.inc | 24 ++++ .../ContextHelper/IsInTypeTestUnitTest.php | 107 ++++++++++++++++++ .../Security/NonceVerificationUnitTest.php | 1 - 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc create mode 100644 WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc new file mode 100644 index 0000000000..9108259721 --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc @@ -0,0 +1,24 @@ +is_string( /* testObjectMethod */ $value ); +$obj?->is_object( /* testNullsafeObjectMethod */ $value ); +TypeChecker::is_int( /* testStaticMethod */ $value ); +is_scalar( my_function( /* testNestedFunctionCall */ $value ) ); + +/* + * The below should be recognized as being inside a type test function call. + */ + +is_array( /* testUnqualifiedFunction */ $value ); +Is_Bool( /* testMixedCaseFunction */ $value ); +\is_string( /* testFullyQualifiedFunction */ $value ); +\IS_NUMERIC( /* testFullyQualifiedUpperCaseFunction */ $value ); diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php new file mode 100644 index 0000000000..c8c7ca98b0 --- /dev/null +++ b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php @@ -0,0 +1,107 @@ +getTargetToken( $testMarker, \T_VARIABLE ); + $result = ContextHelper::is_in_type_test( self::$phpcsFile, $stackPtr ); + + $this->assertSame( $expectedResult, $result ); + } + + /** + * Data provider. + * + * @see testIsInTypeTest() + * + * @return array> + */ + public static function dataIsInTypeTest() { + return array( + // Cases that should return false. + 'other_function_call' => array( + 'testMarker' => '/* testOtherFunctionCall */', + 'expectedResult' => false, + ), + 'partially_qualified_function' => array( + 'testMarker' => '/* testPartiallyQualifiedFunction */', + 'expectedResult' => false, + ), + 'fully_qualified_namespaced_function' => array( + 'testMarker' => '/* testFullyQualifiedNamespacedFunction */', + 'expectedResult' => false, + ), + 'namespace_relative_function' => array( + 'testMarker' => '/* testNamespaceRelativeFunction */', + 'expectedResult' => false, + ), + 'namespace_relative_sub_function' => array( + 'testMarker' => '/* testNamespaceRelativeSubFunction */', + 'expectedResult' => false, + ), + 'object_method' => array( + 'testMarker' => '/* testObjectMethod */', + 'expectedResult' => false, + ), + 'nullsafe_object_method' => array( + 'testMarker' => '/* testNullsafeObjectMethod */', + 'expectedResult' => false, + ), + 'static_method' => array( + 'testMarker' => '/* testStaticMethod */', + 'expectedResult' => false, + ), + 'nested_function_call' => array( + 'testMarker' => '/* testNestedFunctionCall */', + 'expectedResult' => false, + ), + + // Cases that should return true. + 'unqualified_function' => array( + 'testMarker' => '/* testUnqualifiedFunction */', + 'expectedResult' => true, + ), + 'mixed_case_function' => array( + 'testMarker' => '/* testMixedCaseFunction */', + 'expectedResult' => true, + ), + 'fully_qualified_function' => array( + 'testMarker' => '/* testFullyQualifiedFunction */', + 'expectedResult' => true, + ), + 'fully_qualified_upper_case_function' => array( + 'testMarker' => '/* testFullyQualifiedUpperCaseFunction */', + 'expectedResult' => true, + ), + ); + } +} diff --git a/WordPress/Tests/Security/NonceVerificationUnitTest.php b/WordPress/Tests/Security/NonceVerificationUnitTest.php index 081de9a00f..d5ee162048 100644 --- a/WordPress/Tests/Security/NonceVerificationUnitTest.php +++ b/WordPress/Tests/Security/NonceVerificationUnitTest.php @@ -18,7 +18,6 @@ * @since 0.13.0 Class name changed: this class is now namespaced. * @since 1.0.0 This sniff has been moved from the `CSRF` category to the `Security` category. * - * @covers \WordPressCS\WordPress\Helpers\ContextHelper::is_in_type_test * @covers \WordPressCS\WordPress\Sniffs\Security\NonceVerificationSniff */ final class NonceVerificationUnitTest extends AbstractSniffUnitTest { From 830b91fbca24ba718beb6b36ce4191f3a7ebd437 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 24 Apr 2026 09:48:07 -0300 Subject: [PATCH 2/3] Add nested type test test as suggested during PR review --- .../Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc | 3 ++- .../Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc index 9108259721..4e9408aae2 100644 --- a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc +++ b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc @@ -12,7 +12,7 @@ namespace\Sub\is_float( /* testNamespaceRelativeSubFunction */ $value ); $obj->is_string( /* testObjectMethod */ $value ); $obj?->is_object( /* testNullsafeObjectMethod */ $value ); TypeChecker::is_int( /* testStaticMethod */ $value ); -is_scalar( my_function( /* testNestedFunctionCall */ $value ) ); +is_scalar( my_function( /* testNestedNonTargetFunctionCall */ $value ) ); /* * The below should be recognized as being inside a type test function call. @@ -22,3 +22,4 @@ is_array( /* testUnqualifiedFunction */ $value ); Is_Bool( /* testMixedCaseFunction */ $value ); \is_string( /* testFullyQualifiedFunction */ $value ); \IS_NUMERIC( /* testFullyQualifiedUpperCaseFunction */ $value ); +is_int( is_float( /* testNestedTypeTestCall */ $value ) ); diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php index c8c7ca98b0..53712f155b 100644 --- a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php +++ b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php @@ -80,8 +80,8 @@ public static function dataIsInTypeTest() { 'testMarker' => '/* testStaticMethod */', 'expectedResult' => false, ), - 'nested_function_call' => array( - 'testMarker' => '/* testNestedFunctionCall */', + 'nested_non_target_function_call' => array( + 'testMarker' => '/* testNestedNonTargetFunctionCall */', 'expectedResult' => false, ), @@ -102,6 +102,10 @@ public static function dataIsInTypeTest() { 'testMarker' => '/* testFullyQualifiedUpperCaseFunction */', 'expectedResult' => true, ), + 'nested_type_test_call' => array( + 'testMarker' => '/* testNestedTypeTestCall */', + 'expectedResult' => true, + ), ); } } From 5447a46132197511e052b4c5d214ddf201f03e28 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 24 Apr 2026 10:16:23 -0300 Subject: [PATCH 3/3] Add a bare variable test for consistency with the other similar helper method tests --- .../Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc | 1 + .../Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc index 4e9408aae2..b3b2015bf2 100644 --- a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc +++ b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.inc @@ -4,6 +4,7 @@ * The below should NOT be recognized as being inside a type test function call. */ +/* testBareVariable */ $value; some_function( /* testOtherFunctionCall */ $value ); MyNamespace\is_numeric( /* testPartiallyQualifiedFunction */ $value ); \MyNamespace\is_array( /* testFullyQualifiedNamespacedFunction */ $value ); diff --git a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php index 53712f155b..cc731c6713 100644 --- a/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php +++ b/WordPress/Tests/Helpers/ContextHelper/IsInTypeTestUnitTest.php @@ -48,6 +48,10 @@ public function testIsInTypeTest( $testMarker, $expectedResult ) { public static function dataIsInTypeTest() { return array( // Cases that should return false. + 'bare_variable' => array( + 'testMarker' => '/* testBareVariable */', + 'expectedResult' => false, + ), 'other_function_call' => array( 'testMarker' => '/* testOtherFunctionCall */', 'expectedResult' => false,