Skip to content

PEAR/FunctionDeclaration: bug fix - block comment handling#1378

Merged
jrfnl merged 1 commit into4.xfrom
feature/1368/squiz-multilinefunctiondeclaration-improve-comment-handling
Mar 5, 2026
Merged

PEAR/FunctionDeclaration: bug fix - block comment handling#1378
jrfnl merged 1 commit into4.xfrom
feature/1368/squiz-multilinefunctiondeclaration-improve-comment-handling

Conversation

@jrfnl
Copy link
Copy Markdown
Member

@jrfnl jrfnl commented Mar 1, 2026

Description

When a block comment is multi-line, the first T_COMMENT token will start with the /*, but T_COMMENT tokens which are part of the block comment on subsequent lines will include the indentation in the T_COMMENT token. This makes these tokens more complex to deal with in sniffs, especially for indentation calculations.

The PEAR.Functions.FunctionDeclaration sniff did not take the possibility of a multi-line block comment being part of the signature of a multi-line function declaration into account (at all). This also affected, by extension, the Squiz.Functions.MultiLineFunctionDeclaration sniff and would cause a fixer conflict of the sniff with itself.

This commit fixes this.

Includes extensive tests.

Note - there are two caveats to the fix:

  1. If subsequent lines in a block comment are not prefixed with a *, the indentation fix will not be precise, but will be a best-effort "indent needs to be the same or larger than the expected indent".
  2. Like for all sniffs, lines containing only PHPCS annotations will be ignored unless the --ignore-annotations CLI flag is used. That also applies to these lines when they are part of a multi-line block comment.

Fixes #1368

Suggested changelog entry

  • Fixed: PEAR.Functions.FunctionDeclaration: the indentation for subsequent lines in multi-line block comments would be incorrectly determined, leading to false positives and resulting in a fixer conflict when running phpcbf.
    This also affected the Squiz.Functions.MultiLineFunctionDeclaration sniff by extension.

When a block comment is multi-line, the first `T_COMMENT` token will start with the `/*`, but `T_COMMENT` tokens which are part of the block comment on subsequent lines will include the indentation in the `T_COMMENT` token.
This makes these tokens more complex to deal with in sniffs, especially for indentation calculations.

The `PEAR.Functions.FunctionDeclaration` sniff did not take the possibility of a multi-line block comment being part of the signature of a multi-line function declaration into account (at all).
This also affected, by extension, the `Squiz.Functions.MultiLineFunctionDeclaration` sniff and would cause a fixer conflict of the sniff with itself.

This commit fixes this.

Includes extensive tests.

Note - there are two caveats to the fix:
1. If subsequent lines in a block comment are not prefixed with a `*`, the indentation fix will not be precise, but will be a best-effort "indent needs to be the same or larger than the expected indent".
2. Like for all sniffs, lines containing PHPCS annotations will be ignored unless the `--ignore-annotations` CLI flag is used.
    That also applies to these lines when they are part of a multi-line block comment.

Fixes 1368
Copy link
Copy Markdown
Contributor

@anomiex anomiex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works to fix #1368.

@jrfnl jrfnl merged commit 0addd30 into 4.x Mar 5, 2026
71 checks passed
@jrfnl jrfnl deleted the feature/1368/squiz-multilinefunctiondeclaration-improve-comment-handling branch March 5, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PEAR.Functions.FunctionDeclaration gets confused by multi-line block comments inside a function's argument list

2 participants