diff --git a/PhpCollective/Sniffs/Commenting/DocBlockVarSniff.php b/PhpCollective/Sniffs/Commenting/DocBlockVarSniff.php index 7ad77c9..6a0705e 100644 --- a/PhpCollective/Sniffs/Commenting/DocBlockVarSniff.php +++ b/PhpCollective/Sniffs/Commenting/DocBlockVarSniff.php @@ -283,17 +283,24 @@ protected function handle(File $phpcsFile, int $docBlockEndIndex, int $stackPoin } $content = $tokens[$classNameIndex]['content']; - if (str_contains($content, '{') || str_contains($content, '<')) { - return; - } $appendix = ''; $spaceIndex = strpos($content, ' '); - if ($spaceIndex) { + if ($spaceIndex !== false) { $appendix = substr($content, $spaceIndex); $content = substr($content, 0, $spaceIndex); } + // Skip types whose syntax contains internal structure that the simple + // first-space split above cannot handle: generics/array-shapes (`{`, + // `<`) and callable/Closure signatures (`(int): string`). Their opening + // token sits before the first space, so checking the split-off type + // catches them while leaving a trailing description like `Foo (note)` + // alone. Without this the fixer would split mid-type and corrupt it. + if (str_contains($content, '{') || str_contains($content, '<') || str_contains($content, '(')) { + return; + } + if (!$content) { $error = 'Doc Block type for property annotation @var missing'; if ($defaultValueType) { diff --git a/tests/PhpCollective/Sniffs/Commenting/DocBlockVarSniffTest.php b/tests/PhpCollective/Sniffs/Commenting/DocBlockVarSniffTest.php index 929cb45..f348126 100644 --- a/tests/PhpCollective/Sniffs/Commenting/DocBlockVarSniffTest.php +++ b/tests/PhpCollective/Sniffs/Commenting/DocBlockVarSniffTest.php @@ -17,7 +17,7 @@ class DocBlockVarSniffTest extends TestCase */ public function testDocBlockConstSniffer(): void { - $this->assertSnifferFindsErrors(new DocBlockVarSniff(), 1); + $this->assertSnifferFindsErrors(new DocBlockVarSniff(), 2); } /** diff --git a/tests/_data/DocBlockVar/after.php b/tests/_data/DocBlockVar/after.php index 341ff37..8b9c10f 100644 --- a/tests/_data/DocBlockVar/after.php +++ b/tests/_data/DocBlockVar/after.php @@ -70,4 +70,14 @@ class FixMe * @var MyClass[] */ protected array $objects; + + /** + * @var \Closure(string): string + */ + protected ?Closure $transformer = null; + + /** + * @var string|null (deprecated) + */ + protected ?string $note; } diff --git a/tests/_data/DocBlockVar/before.php b/tests/_data/DocBlockVar/before.php index 341ff37..a404432 100644 --- a/tests/_data/DocBlockVar/before.php +++ b/tests/_data/DocBlockVar/before.php @@ -70,4 +70,14 @@ class FixMe * @var MyClass[] */ protected array $objects; + + /** + * @var \Closure(string): string + */ + protected ?Closure $transformer = null; + + /** + * @var string (deprecated) + */ + protected ?string $note; }