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
4 changes: 2 additions & 2 deletions PhpCollective/Sniffs/Classes/Psr4Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ protected function addError(

$phpcsFile->addError(
sprintf(
'Class name is not compliant with PSR-4 configuration. ' .
'It should be `%s` instead of `%s`.',
'Class name is not compliant with PSR-4 configuration. '
. 'It should be `%s` instead of `%s`.',
$result->getExpectedClassName(),
$result->getActualClassName(),
),
Expand Down
40 changes: 39 additions & 1 deletion PhpCollective/Sniffs/Commenting/DocBlockVarSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHP_CodeSniffer\Util\Tokens;
use PhpCollective\Sniffs\AbstractSniffs\AbstractSniff;
use PhpCollective\Traits\CommentingTrait;
use PhpCollective\Traits\UseStatementsTrait;

/**
* Ensures Doc Blocks for variables exist and are correct.
Expand All @@ -21,6 +22,7 @@
class DocBlockVarSniff extends AbstractSniff
{
use CommentingTrait;
use UseStatementsTrait;

/**
* @inheritDoc
Expand Down Expand Up @@ -377,7 +379,7 @@ protected function handleDefaultValue(
protected function handleTypes(File $phpCsFile, int $stackPointer, array $types, mixed $content, string $appendix, int $classNameIndex): void
{
foreach ($types as $type) {
if (str_contains($content, $type)) {
if ($this->typesMatch($phpCsFile, $content, $type)) {
continue;
}

Expand All @@ -387,4 +389,40 @@ protected function handleTypes(File $phpCsFile, int $stackPointer, array $types,
}
}
}

/**
* Check if two types match, considering use statement aliases.
*
* @param \PHP_CodeSniffer\Files\File $phpCsFile
* @param string $docBlockType
* @param string $propertyType
*
* @return bool
*/
protected function typesMatch(File $phpCsFile, string $docBlockType, string $propertyType): bool
{
// Direct match
if (str_contains($docBlockType, $propertyType)) {
return true;
}

// Check if property type is 'array' and doc block contains array syntax
if ($propertyType === 'array' && $this->containsTypeArray([$docBlockType])) {
return true;
}

// Get use statements
$useStatements = $this->getUseStatements($phpCsFile);

// Check if the property type is an alias
if (isset($useStatements[$propertyType])) {
$fullClassName = $useStatements[$propertyType]['fullName'];
// Check if doc block contains the full class name (with or without leading backslash)
if (str_contains($docBlockType, '\\' . $fullClassName) || str_contains($docBlockType, $fullClassName)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,25 @@ class FullyQualifiedClassNameInDocBlockSniff implements Sniff
* @var array<string>
*/
public static array $whitelistedTypes = [
'string', 'int', 'integer', 'float', 'bool', 'boolean', 'resource', 'null', 'void', 'callable',
'array', 'iterable', 'mixed', 'object', 'false', 'true', 'self', 'static', '$this',
'string',
'int',
'integer',
'float',
'bool',
'boolean',
'resource',
'null',
'void',
'callable',
'array',
'iterable',
'mixed',
'object',
'false',
'true',
'self',
'static',
'$this',
];

/**
Expand Down
Loading