Skip to content

Tokenizer/PHP: fix ? tokenization in property hook parameters - #1495

Open
lazerg wants to merge 1 commit into
PHPCSStandards:4.xfrom
lazerg:fix/issue-1494-nullable-in-property-hook-param
Open

Tokenizer/PHP: fix ? tokenization in property hook parameters#1495
lazerg wants to merge 1 commit into
PHPCSStandards:4.xfrom
lazerg:fix/issue-1494-nullable-in-property-hook-param

Conversation

@lazerg

@lazerg lazerg commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Description

The ? of a nullable type on a property hook parameter, like set(?int $value), was tokenized as T_INLINE_THEN instead of T_NULLABLE, so Squiz.WhiteSpace.OperatorSpacing treated it as a ternary and phpcbf "fixed" the accessor to set( ? int $value).

When the backward walk in the ? disambiguation reaches the parameter list opener, it currently keeps walking and hits the { of the hook block, which is in the list of tokens that mean "ternary". A ? directly preceded by ( is always the start of a nullable type though, never a ternary, since a ternary needs an expression before its ?. This adds ( to the tokens that make the previous non-empty token check conclude T_NULLABLE, which also stops the walk before it can reach the hook block.

Regular methods and closures were never affected: their backward walk finds function/fn first.

Suggested changelog entry

  • Fixed a bug where the ? of a nullable type on a property hook parameter would be tokenized as T_INLINE_THEN instead of T_NULLABLE, which made Squiz.WhiteSpace.OperatorSpacing report false positives and phpcbf break the code.

Related issues/external references

Fixes #1494

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.
  • I have opened a sister-PR in the documentation repository to update the Wiki.

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.

Nullable type hint on a property hook parameter is tokenized as T_INLINE_THEN (ternary), breaking Squiz.WhiteSpace.OperatorSpacing

1 participant