Skip to content

Commit 8b6e91e

Browse files
dereuromarkclaude
andauthored
Fix VariableWrong false positives for partial doc blocks (#40)
Previously, when a doc block only documented some parameters (e.g., only those needing complex type hints like `array<string>`), the sniff would compare params by position rather than by name, causing false positives. For example, this code: ```php /** * @param array<string> $lines */ protected function parseBlocks(Node $parent, array $lines, int $indent): void ``` Would incorrectly report: "Doc Block param variable `$lines` should be `$parent`" because it was comparing the first doc block param with the first method param positionally. Now the sniff matches doc block params to method params by variable name, allowing partial documentation to work correctly. Only params that don't exist in the method signature at all are reported as errors. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
1 parent 0773620 commit 8b6e91e

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

PhpCollective/Sniffs/Commenting/DocBlockParamSniff.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,29 @@ public function process(File $phpcsFile, $stackPointer): void
153153
return;
154154
}
155155

156-
foreach ($docBlockParams as $docBlockParam) {
157-
/** @var array<string, mixed> $methodParam */
158-
$methodParam = array_shift($methodSignature);
159-
$variableName = $tokens[$methodParam['variableIndex']]['content'];
156+
// Build a map of method param variable names for lookup
157+
$methodParamsByName = [];
158+
foreach ($methodSignature as $methodParam) {
159+
$varName = $tokens[$methodParam['variableIndex']]['content'];
160+
$methodParamsByName[$varName] = $methodParam;
161+
}
160162

161-
if ($docBlockParam['variable'] === $variableName) {
162-
continue;
163-
}
163+
foreach ($docBlockParams as $docBlockParam) {
164164
// We let other sniffers take care of missing type for now
165165
if (str_contains($docBlockParam['type'], '$')) {
166166
continue;
167167
}
168168

169-
$error = 'Doc Block param variable `' . $docBlockParam['variable'] . '` should be `' . $variableName . '`';
170-
// For now just report (buggy yet)
169+
$docBlockVariable = $docBlockParam['variable'];
170+
171+
// Check if the doc block variable exists in the method signature
172+
if (isset($methodParamsByName[$docBlockVariable])) {
173+
// Variable name matches a method param - this is correct
174+
continue;
175+
}
176+
177+
// Variable doesn't exist in method signature - report error
178+
$error = 'Doc Block param variable `' . $docBlockVariable . '` does not exist in method signature';
171179
$phpcsFile->addError($error, $docBlockParam['index'], 'VariableWrong');
172180
}
173181
}

0 commit comments

Comments
 (0)