Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions PhpCollective/Sniffs/Commenting/DocBlockVarSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DocBlockVarSniffTest extends TestCase
*/
public function testDocBlockConstSniffer(): void
{
$this->assertSnifferFindsErrors(new DocBlockVarSniff(), 1);
$this->assertSnifferFindsErrors(new DocBlockVarSniff(), 2);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/_data/DocBlockVar/after.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions tests/_data/DocBlockVar/before.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading