Repository navigation
feat: ✨ Report multi-condition expressions and require blank lines around Blade directives. - #400
Merged
Merged
Conversation
…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.
Brings in the LogicalKeywordAfterAssignment sniff from #401. phpcbf splits two of its return statements so they pass the new multi-condition rule.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OneConditionPerLine only checked
if,elseif,whileandforconditions. A line such as this one passed phpcs:The sniff now also checks assignments, returns, arrow-function bodies, match arms and ternary branches. phpcbf fixes each case:
What to know:
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.MultiLineStatementIndentnow indents a branch's conditions from that branch's?or:line, so the two fixers agree.and,orandxorfollow 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.if. Otherwise every multi-line call or array in a return would be flagged.ifcondition is left to that condition. Reporting it makes phpcbf loop.Tests: 3111 pass in CI on PHP 8.3, 8.4 and 8.5, with
main(#401) merged in.composer lintandcomposer lint:selfshow 0 errors.Also in this PR: Blade directive blank lines
New sniff
WhiteSpace.BladeDirectiveBlankLines. In.blade.phpfiles 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.@authfollowed by@ifpasses. Sibling blocks still need the blank line.{{ }},{!! !!}, Blade comments, PHP tags,@phpand@verbatim, as well as@@and email addresses.Found in crossbible PR #875, where
@endguestran straight into</flux:callout.text>.