Skip to content

Commit 8d3d569

Browse files
committed
Improved AnnotationTypeHelper::export() for callable in array
1 parent b574727 commit 8d3d569

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function count;
2424
use function in_array;
2525
use function preg_replace;
26+
use function sprintf;
2627
use function strtolower;
2728
use function substr;
2829

@@ -273,6 +274,10 @@ public static function export(TypeNode $typeNode): string
273274
$exportedTypeNode = substr($exportedTypeNode, 1, -1);
274275
}
275276

277+
if ($typeNode instanceof ArrayTypeNode && $typeNode->type instanceof CallableTypeNode) {
278+
$exportedTypeNode = sprintf('(%s)[]', substr($exportedTypeNode, 0, -2));
279+
}
280+
276281
return $exportedTypeNode;
277282
}
278283

tests/Sniffs/Namespaces/ReferenceUsedNamesOnlySniffTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ public function testSearchingInAnnotations(): void
727727
]
728728
);
729729

730-
self::assertSame(31, $report->getErrorCount());
730+
self::assertSame(32, $report->getErrorCount());
731731

732732
self::assertSniffError(
733733
$report,
@@ -918,6 +918,13 @@ public function testSearchingInAnnotations(): void
918918

919919
self::assertNoSniffError($report, 138);
920920

921+
self::assertSniffError(
922+
$report,
923+
145,
924+
ReferenceUsedNamesOnlySniff::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME,
925+
'Class \Foo\Anything should not be referenced via a fully qualified name, but via a use statement.'
926+
);
927+
921928
self::assertAllFixedInFile($report);
922929
}
923930

tests/Sniffs/Namespaces/data/shouldBeInUseStatementSearchingInAnnotations.fixed.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,9 @@ class Whatever
143143
private $anythingDifferent;
144144

145145
}
146+
147+
class CallableInArray
148+
{
149+
/** @var (callable(): Anything)[] */
150+
private array $callableInArray;
151+
}

tests/Sniffs/Namespaces/data/shouldBeInUseStatementSearchingInAnnotations.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,9 @@ class Whatever
139139
private $anythingDifferent;
140140

141141
}
142+
143+
class CallableInArray
144+
{
145+
/** @var (callable(): \Foo\Anything)[] */
146+
private array $callableInArray;
147+
}

0 commit comments

Comments
 (0)