Skip to content

feat: ✨ Report multi-condition expressions and require blank lines around Blade directives. - #400

Merged
mikebronner merged 3 commits into
mainfrom
feat/one-condition-per-line-expressions
Nov 13, 2026
Merged

mikebronner merged 3 commits into
mainfrom
feat/one-condition-per-line-expressions

Conversation

@mikebronner

@mikebronner mikebronner commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

OneConditionPerLine only checked if, elseif, while and for conditions. A line such as this one passed phpcs:

$isActiveOrPastDue = $subscription?->active() || $subscription?->pastDue();

The sniff now also checks assignments, returns, arrow-function bodies, match arms and ternary branches. phpcbf fixes each case:

$isActiveOrPastDue = $subscription?->active()
    || $subscription?->pastDue();

$status = $isCurrent
    ? $isPaid
        && $isListed
    : $isDraft;

What to know:

  • A new fixable code, TernaryOperatorNotLeading, makes ? and : start their own lines when a condition before them, or a branch, is split. Without it, the split reads with the wrong grouping.
  • MultiLineStatementIndent now indents a branch's conditions from that branch's ? or : line, so the two fixers agree.
  • and, or and xor follow PHP precedence. They stay outside a ternary, and an assignment ends where one starts. So $granted = $isOwner or $isAdmin; is not reported here. Flag and, or and xor directly after an assignment #401 added a separate rule, LogicalKeywordAfterAssignment, for that bug.
  • A single condition in an expression may span lines, unlike in an if. Otherwise every multi-line call or array in a return would be flagged.
  • An expression inside an if condition is left to that condition. Reporting it makes phpcbf loop.
  • 45 of the 54 changed files are this package's own sources, reformatted by phpcbf to pass the new rule. Those changes only move operators.

Tests: 3111 pass in CI on PHP 8.3, 8.4 and 8.5, with main (#401) merged in. composer lint and composer lint:self show 0 errors.

Also in this PR: Blade directive blank lines

New sniff WhiteSpace.BladeDirectiveBlankLines. In .blade.php files it requires a blank line before an opening block directive (@if, @auth, @foreach...) and after its closing one, even directly inside an HTML tag. phpcbf adds the line.

  • A block nested directly in another block is exempt, so @auth followed by @if passes. Sibling blocks still need the blank line.
  • It ignores directives inside {{ }}, {!! !!}, Blade comments, PHP tags, @php and @verbatim, as well as @@ and email addresses.

Found in crossbible PR #875, where @endguest ran straight into </flux:callout.text>.

…ructures.

OneConditionPerLine now checks the boolean expression in an assignment,
a return statement, an arrow-function body and a match-arm body. When
two or more top-level conditions share a line, it reports
MultipleConditionsOnOneLine or BooleanOperatorNotLeading, and phpcbf
moves each operator to the start of its own line, as it does for
if/elseif/while/for. A single-condition expression may still span
lines, and nested operators are not top-level.

Ternaries follow the same rule. When the ternary condition or either
branch holds several conditions, the "?" and the ":" must each start
their own line (TernaryOperatorNotLeading). A branch's further
conditions sit one level deeper than its "?" or ":" line, and
MultiLineStatementIndent now anchors those lines on that operator, so
the fixed layout cannot be misread and the two fixers agree.

The layout follows PHP's precedence for "and", "or" and "xor". They
bind looser than a ternary, so they stay at statement level in both
sniffs. They also bind looser than "=", so an assignment ends where
one of them starts.

An expression nested inside a control-structure condition is left to
that condition, so the fixer never splits a line the condition rule
joins back. An operator at the end of a line outside a condition stays
with OperatorLineBreak, so each wrap is still reported once.

The package's own sources are reformatted to pass the new check.
Add CleanCode.WhiteSpace.BladeDirectiveBlankLines. In a Blade view it
reports an opening block directive that starts its line with no blank
line before it, and its paired closing directive with no blank line
after it. This also applies when the block is the first or last child
of an element. phpcbf inserts the missing blank line.

A block nested directly in another block is exempt: its opener may
follow the outer opener, and its closer may precede the outer closer.
Sibling blocks still need a blank line between them. A middle
directive such as @else or @empty is not a parent.

Directive text inside {{ }}, {!! !!}, {{-- --}}, PHP tags, @php and
@verbatim is ignored, and so are @@-escaped directives and email
addresses. A directive after one of those regions on the same line
does not start its line. An opener counts only when a matching closer
pairs with it. When the masking pattern cannot run, the sniff reports
nothing.
@mikebronner mikebronner changed the title feat: ✨ Report multi-condition boolean expressions outside control structures. feat: ✨ Report multi-condition expressions and require blank lines around Blade directives. Nov 7, 2026
Brings in the LogicalKeywordAfterAssignment sniff from #401. phpcbf
splits two of its return statements so they pass the new
multi-condition rule.
@mikebronner mikebronner self-assigned this Nov 13, 2026
@mikebronner
mikebronner merged commit 9629dc3 into main Nov 13, 2026
3 checks passed
@mikebronner
mikebronner deleted the feat/one-condition-per-line-expressions branch November 13, 2026 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant