Skip to content

Operators and linebreaks question #149

Description

@vadimonus

Problem

Before 3.0 we could write something like

$sum =
    $amount
    + $tax
    - $discount;

3.0 is much stricter, and we should write

// the added values ​​now have different indents, which makes it harder to read stack of variables
$sum = $amount
    + $tax
    - $discount;

or

// operators with different precedence have the same indentation,
// but the execution order does not correspond to the reading sequence from top to bottom
$sum
    = $amount
    + $tax
    - $discount;

Both versions are worse than the original in terms of readability. Thereforce i'm looking for a way to make it as readable, as it was before. But currently i cannot understand, if some cases obey standard rules or not.

I find this way of writing the best (operators with different precedence have different indents, all variables in sum are stacked whell). But php-cs-fixer disallow me to write such way in @PER-CS3.0 ruleset, requring to write = and $amount on same line. I examined standard (firstly 6.4 and 5.1 sections), but cannot find such a requirements.

$sum
    =
        $amount
        + $tax
        - $discount;

Please clarify whether this example meets the requirements of the standard.

php-cs-fixer accepted this option

$sum
    =   $amount
        + $tax
        - $discount;

please clarify, if it meets the requirements and original idea of standard.

Activity

  1. vadimonus commented on Jun 14, 2026

    @vadimonus
    Author

    I'm thinking about some new rule for cs fixer for such formatting, or maybe some rule in standard to require this. Main idea, is that operators with different precedence should not have same indents, because it may reduce readability and lead to the false impression that the statements will be executed in top-to-bottom reading order.

    Some more examples to think about

    // Arithmetic. Operators with 5 different precedences
    $r1 = $r2 = $x1 * $x2 + $y1 * $y2 + $z1 * $z2 |> sqrt(...);
    // not passed php-cs-fixer
    $r1
        = $r2
        =       
                $x1
                    * $x2
                + $y1
                    * $y2
                + $z
                    ** 2
            |> sqrt(...);
    // passed php-cs-fixer
    $r1
        = $r2
        =       $x1
                    * $x2
                + $y1
                    * $y2
                + $z
                    ** 2
            |> sqrt(...);
    
    // Logic.
    // not passed
    $getReward
        =
            $isBirthday
            or
                $isLoggedIn
                    and $hasPremium
                xor $completedQuest
                    and $defeatedBoss;
    
    // passed
    $getReward
        = $isBirthday
            or  $isLoggedIn
                    and $hasPremium
                xor $completedQuest
                    and $defeatedBoss;
    
    // pipes and function composition
    // not passed
    $pipe
        =
            'str'
            .
                $x
                |> $func1
                    + $func2
                |> $func3;
    // passed
    $pipe
        =   'str'
            .   $x
                |> $func1
                    + $func2
                |> $func3;
    

    As for me, i found here variants with operator on same line as more readable, than one, that passed php-cs-fixer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions