Skip to content

Add OperatorPosition sniff for wrapped boolean, ternary, coalesce and arithmetic operators - #91

Merged
dereuromark merged 1 commit into
masterfrom
operator-position
Oct 25, 2026
Merged

dereuromark merged 1 commit into
masterfrom
operator-position

Conversation

@dereuromark

Copy link
Copy Markdown
Contributor

Closes #87

Adds PhpCollective.Operators.OperatorPosition and enables it in the PhpCollective ruleset next to Universal.Operators.ConcatPosition.

// before
$ok = $a &&
    $b;
$x = $cond ?
    $one :
    $two;

// after
$ok = $a
    && $b;
$x = $cond
    ? $one
    : $two;

Covered: &&, ||, and, or, xor, ??, ternary ? / : (?: moves as one unit, also when split as ? newline :), and binary + - * / % **. Not touched: unary +/-, return-type and case colons, nullable ?, = and other assignment operators, comparison and bitwise operators. With a comment between the operator and the next token, the error is reported as IncorrectWithComment and not fixed. An operator standing alone on its own line is left as is.

This changes what the standard enforces: projects with operators at the end of a line get new, auto-fixable errors. The repo's own sources are updated in this PR (5 files).

Checked against the 206 files of cakephp/cakephp#19740: the fixer output matches the PR's php-cs-fixer (@PER-CS3x0) result for 171 of 175 wrapped non-concat expressions. The four others are an = moved to the line start, a phpcs:disable block and a ?? alone on its own line, all out of scope.

@dereuromark dereuromark added the enhancement New feature or request label Oct 25, 2026
Copilot AI balanced review requested due to automatic review settings October 25, 2026 13:59
@dereuromark dereuromark added the enhancement New feature or request label Oct 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Universal.Operators.ConcatPosition only covers `.`. The new
PhpCollective.Operators.OperatorPosition sniff applies the same rule to
`&&`, `||`, `and`, `or`, `xor`, `??`, the ternary `?` and `:` (with `?:`
moved as one unit) and binary `+ - * / % **`: when the operator ends a
line and the expression continues on the next, it moves to the start of
the continuation line. Unary plus/minus, return-type and case colons,
nullable `?` and operators followed by a comment (reported, not fixed)
are left alone.

The repository's own sources are updated to pass the new rule.

On the 206 files of the cakephp PER-CS PR, the fixer output matches the
PR's php-cs-fixer result for 171 of the 175 wrapped non-concat
expressions. The rest are an `=` moved to the line start (not covered),
a `phpcs:disable` block and a `??` that stood alone on its own line.
Copilot AI balanced review requested due to automatic review settings October 25, 2026 14:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@dereuromark
dereuromark merged commit d421bfd into master Oct 25, 2026
5 checks passed
@dereuromark
dereuromark deleted the operator-position branch October 25, 2026 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enforce operator position for wrapped boolean, ternary, coalesce and arithmetic operators

2 participants