Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions WordPress/Helpers/ContextHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ public static function is_token_namespaced( File $phpcsFile, $stackPtr ) {
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The index of the token in the stack.
* @param array $valid_functions List of valid function names.
* Note: The keys to this array should be the function names
* in lowercase. Values are irrelevant.
* Note: The keys to this array should be the function names.
* Values are irrelevant. The matching of function names found in
* the code against the keys in this array is done case-insensitively.
* @param bool $global_function Optional. Whether to make sure that the function call is
* to a global function. If `false`, calls to methods, be it static
* `Class::method()` or via an object `$obj->method()`, and
Expand All @@ -217,6 +218,8 @@ public static function is_token_namespaced( File $phpcsFile, $stackPtr ) {
* @return int|bool Stack pointer to the function call T_STRING token or false otherwise.
*/
public static function is_in_function_call( File $phpcsFile, $stackPtr, array $valid_functions, $global_function = true, $allow_nested = false ) {
$valid_functions = array_change_key_case( $valid_functions, \CASE_LOWER );

$tokens = $phpcsFile->getTokens();
if ( ! isset( $tokens[ $stackPtr ]['nested_parenthesis'] ) ) {
return false;
Expand Down
34 changes: 30 additions & 4 deletions WordPress/Tests/Helpers/ContextHelper/IsInFunctionCallUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,47 @@ public function testIsInFunctionCallShouldReturnFalseWhenEmptyValidFunctions() {
}

/**
* Test to document that is_in_function_call() does not match when $valid_functions keys are not lowercase.
* Test that is_in_function_call() matches regardless of the case of $valid_functions keys.
*
* @dataProvider dataIsInFunctionCallShouldMatchRegardlessOfValidFunctionsKeyCase
*
* @param string $functionName The function name to use as a key in $valid_functions.
*
* @return void
*/
public function testIsInFunctionCallShouldReturnFalseWhenValidFunctionsKeysAreNotLowercase() {
public function testIsInFunctionCallShouldMatchRegardlessOfValidFunctionsKeyCase( $functionName ) {
$insideFunctionPtr = $this->getTargetToken( '/* testLowercaseNameInsideCall */', \T_VARIABLE );
$expected = $this->getTargetToken( '/* testLowercaseName */', \T_STRING );
$result = ContextHelper::is_in_function_call(
self::$phpcsFile,
$insideFunctionPtr,
array(
'Valid_Function1' => true,
$functionName => true,
)
);

$this->assertFalse( $result );
$this->assertSame( $expected, $result );
}

/**
* Data provider.
*
* @see testIsInFunctionCallShouldMatchRegardlessOfValidFunctionsKeyCase()
*
* @return array<string, array<string, string>>
*/
public static function dataIsInFunctionCallShouldMatchRegardlessOfValidFunctionsKeyCase() {
return array(
'lowercase key' => array(
'functionName' => 'valid_function1',
),
'uppercase key' => array(
'functionName' => 'VALID_FUNCTION1',
),
'mixed case key' => array(
'functionName' => 'Valid_Function1',
),
);
}

/**
Expand Down
Loading