Skip to content

Commit 08c3db1

Browse files
committed
SlevomatCodingStandard.Complexity.Cognitive: Fixed internal error for closures
1 parent 142daf0 commit 08c3db1

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

SlevomatCodingStandard/Sniffs/Complexity/CognitiveSniff.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ public function process(File $phpcsFile, int $stackPtr): void
153153
return;
154154
}
155155

156-
$name = $phpcsFile->getDeclarationName($stackPtr);
156+
$name = $phpcsFile->getTokens()[$stackPtr]['code'] === T_FUNCTION
157+
? FunctionHelper::getName($phpcsFile, $stackPtr)
158+
: 'anonymous function';
157159

158160
$errorParameters = [
159161
'Cognitive complexity for "%s" is %d but has to be less than or equal to %d.',

tests/Sniffs/Complexity/CognitiveSniffTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public static function dataProviderFiles(): array
5151
'overriddenSymbolFrom',
5252
19,
5353
],
54+
[
55+
__DIR__ . '/data/cognitive/nesting3.php',
56+
3,
57+
'anonymous function',
58+
19,
59+
],
5460
[
5561
__DIR__ . '/data/cognitive/nestingContinueWithLabel.php',
5662
3,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
function (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;
35+
};

0 commit comments

Comments
 (0)