Skip to content

Commit 89dabeb

Browse files
dereuromarkclaude
andcommitted
Fix false positive in DocBlockVarSniff for class aliases.
Resolves issue where properties using class aliases from use statements incorrectly triggered doc block type errors. Added typesMatch() method to properly compare doc block types with typed properties by resolving aliases through use statements. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c79a21b commit 89dabeb

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

PhpCollective/Sniffs/Commenting/DocBlockVarSniff.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHP_CodeSniffer\Util\Tokens;
1212
use PhpCollective\Sniffs\AbstractSniffs\AbstractSniff;
1313
use PhpCollective\Traits\CommentingTrait;
14+
use PhpCollective\Traits\UseStatementsTrait;
1415

1516
/**
1617
* Ensures Doc Blocks for variables exist and are correct.
@@ -21,6 +22,7 @@
2122
class DocBlockVarSniff extends AbstractSniff
2223
{
2324
use CommentingTrait;
25+
use UseStatementsTrait;
2426

2527
/**
2628
* @inheritDoc
@@ -377,7 +379,7 @@ protected function handleDefaultValue(
377379
protected function handleTypes(File $phpCsFile, int $stackPointer, array $types, mixed $content, string $appendix, int $classNameIndex): void
378380
{
379381
foreach ($types as $type) {
380-
if (str_contains($content, $type)) {
382+
if ($this->typesMatch($phpCsFile, $content, $type)) {
381383
continue;
382384
}
383385

@@ -387,4 +389,35 @@ protected function handleTypes(File $phpCsFile, int $stackPointer, array $types,
387389
}
388390
}
389391
}
392+
393+
/**
394+
* Check if two types match, considering use statement aliases.
395+
*
396+
* @param \PHP_CodeSniffer\Files\File $phpCsFile
397+
* @param string $docBlockType
398+
* @param string $propertyType
399+
*
400+
* @return bool
401+
*/
402+
protected function typesMatch(File $phpCsFile, string $docBlockType, string $propertyType): bool
403+
{
404+
// Direct match
405+
if (str_contains($docBlockType, $propertyType)) {
406+
return true;
407+
}
408+
409+
// Get use statements
410+
$useStatements = $this->getUseStatements($phpCsFile);
411+
412+
// Check if the property type is an alias
413+
if (isset($useStatements[$propertyType])) {
414+
$fullClassName = $useStatements[$propertyType]['fullName'];
415+
// Check if doc block contains the full class name (with or without leading backslash)
416+
if (str_contains($docBlockType, '\\' . $fullClassName) || str_contains($docBlockType, $fullClassName)) {
417+
return true;
418+
}
419+
}
420+
421+
return false;
422+
}
390423
}

tests/_data/DocBlockVar/after.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace PhpCollective;
44

5+
use Tools\Mailer\Message as MailerMessage;
6+
use Some\Other\Class;
7+
use Yet\Another\Thing as AnotherAlias;
8+
59
class FixMe
610
{
711
protected $x = [];
@@ -29,4 +33,19 @@ class FixMe
2933
* @var list<string|null>
3034
*/
3135
protected $left = [];
36+
37+
/**
38+
* @var \Tools\Mailer\Message
39+
*/
40+
protected MailerMessage $message;
41+
42+
/**
43+
* @var \Some\Other\Class
44+
*/
45+
protected Class $class;
46+
47+
/**
48+
* @var \Yet\Another\Thing
49+
*/
50+
protected AnotherAlias $another;
3251
}

tests/_data/DocBlockVar/before.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace PhpCollective;
44

5+
use Tools\Mailer\Message as MailerMessage;
6+
use Some\Other\Class;
7+
use Yet\Another\Thing as AnotherAlias;
8+
59
class FixMe
610
{
711
protected $x = [];
@@ -29,4 +33,19 @@ class FixMe
2933
* @var list<string|null>
3034
*/
3135
protected $left = [];
36+
37+
/**
38+
* @var \Tools\Mailer\Message
39+
*/
40+
protected MailerMessage $message;
41+
42+
/**
43+
* @var \Some\Other\Class
44+
*/
45+
protected Class $class;
46+
47+
/**
48+
* @var \Yet\Another\Thing
49+
*/
50+
protected AnotherAlias $another;
3251
}

0 commit comments

Comments
 (0)