Skip to content

Commit a509ed7

Browse files
authored
Fix: Preserve parameter order when inserting missing @param annotations (#44)
When multiple missing parameters need to be inserted before an existing parameter (or at the end of a docblock), they were being inserted in reverse order due to how addContentBefore() works - each call inserts before the same position, resulting in a LIFO order. The fix reverses the pendingInserts array before iterating, so the parameters are inserted in the correct signature order. Example: For a method like: function foo(string $a, bool $b, string $c) This was causing cascading issues with other sniffs like DocBlockParamAllowDefaultValue and DocBlockParamTypeMismatch, which would detect type mismatches due to the wrong parameter order.
1 parent f5ee62c commit a509ed7

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

PhpCollective/Sniffs/Commenting/DocBlockParamSniff.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ protected function canAddMissingParams(File $phpcsFile, int $docBlockStartIndex,
345345
}
346346
}
347347

348-
foreach ($pendingInserts as $pendingParam) {
348+
// Reverse the array since addContentBefore inserts before the same position
349+
// multiple times, which would reverse the order
350+
foreach (array_reverse($pendingInserts) as $pendingParam) {
349351
$paramLine = $indent . '* @param ' . $pendingParam['type'] . ' ' . $pendingParam['variable'] . "\n";
350352
$phpcsFile->fixer->addContentBefore($insertBeforeIndex, $paramLine);
351353
}
@@ -398,7 +400,9 @@ protected function canAddMissingParams(File $phpcsFile, int $docBlockStartIndex,
398400
}
399401

400402
if ($insertBeforeIndex !== null) {
401-
foreach ($pendingInserts as $pendingParam) {
403+
// Reverse the array since addContentBefore inserts before the same position
404+
// multiple times, which would reverse the order
405+
foreach (array_reverse($pendingInserts) as $pendingParam) {
402406
$paramLine = $indent . '* @param ' . $pendingParam['type'] . ' ' . $pendingParam['variable'] . "\n";
403407
$phpcsFile->fixer->addContentBefore($insertBeforeIndex, $paramLine);
404408
}

0 commit comments

Comments
 (0)