php-ruleset-formatter

Editing Rules

This guide explains the example: how to edit the rule in PHPFormatExtension that prevents cuddling of else conditions (i.e., enforces that else must start on a new line after a closing brace).

Changing the Rule for Yourself (No Recompile or Contributing to the Extension)

If you want to change the cuddled else rule just for yourself, you can do so by editing your ruleset XML file—no need to recompile the extension.

How to Allow Cuddled else Conditions:

  1. Open your ruleset XML file.
  2. Make sure it does not include the following exclude:
  <exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace"/>
  1. If this line is present, remove or comment it out.
  2. Save your ruleset XML file.
  3. Use the formatter as usual. The extension will now allow cuddled else conditions for you, without any need to recompile or change the extension code.

To Contribute To this Extension

1. Locate the Rule Logic

2. How the Rule Works

3. Editing the Rule

To change how this rule works (for example, to allow cuddled else conditions):

  1. Open src/formatter.ts.
  2. Find the splitCuddledElse function.
  3. Edit or comment out the function, or change the logic as needed.
    • To allow cuddled else, you can:
      • Remove or comment out the call to splitCuddledElse in the formatter pipeline:
        // lines = splitCuddledElse(lines);
        
      • Or, modify the function so it does nothing (returns lines unchanged).

4. Controlling the Rule with XML

5. Testing Your Change

Summary: