Skip to content

Newline Spacing Around Control Structures Not Working #606

Description

@ShaneOH

Hi, I'd like to include a rule to add newlines around control structures like so:

        $customFieldsDeleted = [];
        foreach ($deleteCustomFields as $customFieldToDelete) {
            $customFieldIdToDelete = $customFieldToDelete['id'];
            if (empty($customFieldIdToDelete)) {
                continue;
            }
            $result = $this->client->deleteCustomField($productId, $customFieldIdToDelete);
            if ($result) {
                array_push($customFieldsDeleted, $customFieldIdToDelete);
            }
        }
        return $customFieldsDeleted;

becomes this:

        $customFieldsDeleted = [];
        
        foreach ($deleteCustomFields as $customFieldToDelete) {
            $customFieldIdToDelete = $customFieldToDelete['id'];
            
            if (empty($customFieldIdToDelete)) {
                continue;
            }
            
            $result = $this->client->deleteCustomField($productId, $customFieldIdToDelete);
            
            if ($result) {
                array_push($customFieldsDeleted, $customFieldIdToDelete);
            }
        }
        
        return $customFieldsDeleted;

Essentially, adding a newline both before and after a control structure (and ensuring it's only one line total in the case of back-to-back control structures).

First, I can't seem to find a rule that works for this. When I include <rule ref="Squiz.WhiteSpace.ControlStructureSpacing"/>, it almost gets me there, because it seems to be adding newlines after the control structure, but not before:

        $customFieldsDeleted = [];
        foreach ($deleteCustomFields as $customFieldToDelete) {
            $customFieldIdToDelete = $customFieldToDelete['id'];
            if (empty($customFieldIdToDelete)) {
                continue;
            }

            $result = $this->client->deleteCustomField($productId, $customFieldIdToDelete);
            if ($result) {
                array_push($customFieldsDeleted, $customFieldIdToDelete);
            }
        }

        return $customFieldsDeleted;

Even more confusing, because we want to lint against the PSR-12 standard, when we add <rule ref="PSR12"/>, it seems to overwrite the above rule and phpcbf will not add any spacing around the control structures when I run it. When I dive into the PSR12 ruleset here, and pulled all the rules into my own phpcs.xml and started removing them one-by-one until the spacing worked to try to figure out potential conflicts, it seemed like <rule ref="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpen"/> was the culprit, because when that is in the config along with <rule ref="Squiz.WhiteSpace.ControlStructureSpacing"/>, the spacing is not added, but when I remove the former rule, the latter rule finally works.

I tried excluding this from the PSR-12 config block in our file and I also tried

  <rule ref="PSR12">
    <exclude name="PSR12.WhiteSpace.ControlStructureSpacing"/>
  </rule>

But none of this seems to work.

Any guidance on how to get this spacing rule working?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions