Skip to content

Commit 6ca6fe8

Browse files
committed
SlevomatCodingStandard.Complexity.Cognitive: Support for arrow functions
1 parent 08c3db1 commit 6ca6fe8

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

SlevomatCodingStandard/Sniffs/Complexity/CognitiveSniff.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use const T_DO;
2323
use const T_ELSE;
2424
use const T_ELSEIF;
25+
use const T_FN;
2526
use const T_FOR;
2627
use const T_FOREACH;
2728
use const T_FUNCTION;
@@ -79,6 +80,7 @@ class CognitiveSniff implements Sniff
7980
* B3. Nesting increments
8081
*/
8182
private const NESTING_INCREMENTS = [
83+
T_FN => T_FN,
8284
T_CLOSURE => T_CLOSURE,
8385
// increments, but does not receive
8486
T_ELSEIF => T_ELSEIF,
@@ -127,10 +129,7 @@ class CognitiveSniff implements Sniff
127129
*/
128130
public function register(): array
129131
{
130-
return [
131-
T_CLOSURE,
132-
T_FUNCTION,
133-
];
132+
return TokenHelper::FUNCTION_TOKEN_CODES;
134133
}
135134

136135
public function process(File $phpcsFile, int $stackPtr): void

tests/Sniffs/Complexity/CognitiveSniffTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public static function dataProviderFiles(): array
5757
'anonymous function',
5858
19,
5959
],
60+
[
61+
__DIR__ . '/data/cognitive/nesting4.php',
62+
3,
63+
'anonymous function',
64+
19,
65+
],
6066
[
6167
__DIR__ . '/data/cognitive/nestingContinueWithLabel.php',
6268
3,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
fn (ClassJavaType $classType) =>
4+
if (true) { // +1
5+
return $Symbols->unknownMethodSymbol();
6+
}
7+
8+
$unknownFound = false;
9+
10+
$symbols = $classType->getSymbol()->members()->lookup(name);
11+
12+
foreach ($symbols as $symbol) { // +1
13+
if ($overrideSymbol->isKind(MTH) // +2 (nesting = 1)
14+
&& !$overrideSymbol->isStatic()) { // +1
15+
16+
if (canOverride($methodJavaSymbol)) { // +3 (nesting = 2)
17+
$overriding = checkOverridingParameters($methodJavaSymbol, $classType);
18+
19+
if ($overriding === null) { // +4 (nesting = 3)
20+
if (!$unknownFound) { // +5 (nesting = 4)
21+
$unknownFound = true;
22+
}
23+
} elseif ($overriding) { // +1
24+
return $methodJavaSymbol;
25+
}
26+
}
27+
}
28+
}
29+
30+
if ($unknownFound) { // +1
31+
return $symbols->unknownMethodSymbol;
32+
}
33+
34+
return null;

0 commit comments

Comments
 (0)