Skip to content

Commit 3decd5e

Browse files
committed
Fixed bug #3284 : Unused parameter false positive when using array index in arrow function
1 parent 9ae6d1a commit 3decd5e

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
150150
-- Thanks to Juliette Reinders Folmer for the patch
151151
- Fixed bug #3273 : Squiz.Functions.FunctionDeclarationArgumentSpacing reports line break as 0 spaces between parenthesis
152152
- Fixed bug #3277 : Nullable static return typehint causes whitespace error
153+
- Fixed bug #3284 : Unused parameter false positive when using array index in arrow function
153154
</notes>
154155
<contents>
155156
<dir name="/">

src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function process(File $phpcsFile, $stackPtr)
9191

9292
foreach ($methodParams as $param) {
9393
if (isset($param['property_visibility']) === true) {
94-
// Ignore. Constructor property promotion.
94+
// Ignore constructor property promotion.
9595
continue;
9696
}
9797

@@ -101,6 +101,13 @@ public function process(File $phpcsFile, $stackPtr)
101101
$next = ++$token['scope_opener'];
102102
$end = --$token['scope_closer'];
103103

104+
// Check the end token for arrow functions as
105+
// they can end at a content token due to not having
106+
// a clearly defined closing token.
107+
if ($token['code'] === T_FN) {
108+
++$end;
109+
}
110+
104111
$foundContent = false;
105112
$validTokens = [
106113
T_HEREDOC => T_HEREDOC,

src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,5 @@ class ConstructorPropertyPromotionWithContentInMethod {
150150
}
151151
}
152152
}
153+
154+
$found = in_array_cb($needle, $haystack, fn($array, $needle) => $array[2] === $needle);

src/Tokenizers/PHP.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2343,7 +2343,12 @@ protected function processAdditional()
23432343
&& $this->tokens[$scopeCloser]['code'] === T_CLOSE_PARENTHESIS
23442344
&& $this->tokens[$scopeCloser]['parenthesis_opener'] < $arrow
23452345
) {
2346-
$scopeCloser = $lastEndToken;
2346+
for ($lastNonEmpty = ($scopeCloser - 1); $lastNonEmpty > $arrow; $lastNonEmpty--) {
2347+
if (isset(Util\Tokens::$emptyTokens[$this->tokens[$lastNonEmpty]['code']]) === false) {
2348+
$scopeCloser = $lastNonEmpty;
2349+
break;
2350+
}
2351+
}
23472352
}
23482353

23492354
break;

tests/Core/Tokenizer/BackfillFnTokenTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ $extended = fn($c) => $callable(function() {
4040
echo 'done';
4141
}, $c);
4242

43+
/* testArrayIndex */
44+
$found = in_array_cb($needle, $haystack, fn($array, $needle) => $array[2] === $needle);
45+
4346
$result = array_map(
4447
/* testReturnType */
4548
static fn(int $number) : int => $number + 1,

tests/Core/Tokenizer/BackfillFnTokenTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ public function testClosure()
192192
}//end testClosure()
193193

194194

195+
/**
196+
* Test arrow functions using an array index.
197+
*
198+
* @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
199+
*
200+
* @return void
201+
*/
202+
public function testArrayIndex()
203+
{
204+
$token = $this->getTargetToken('/* testArrayIndex */', T_FN);
205+
$this->backfillHelper($token);
206+
$this->scopePositionTestHelper($token, 8, 17, 'comma');
207+
208+
}//end testArrayIndex()
209+
210+
195211
/**
196212
* Test arrow functions with a return type.
197213
*

0 commit comments

Comments
 (0)