PhpStorm 2024.2 Help

Rearrange code

Configure arrangement rules

You can rearrange your code according to the arrangement rules set in the Code Style. PHP page of the Settings dialog.

Configure grouping rules

Grouping rules let you keep related class methods together.

  1. Press Ctrl+Alt+S to open settings and select Editor | Code Style | PHP.

  2. On the Arrangement tab, choose the grouping options in the Grouping rules area.

    Grouping rules example

    For the Keep dependent methods together option, you can select depth-first order or breadth-first order. The former will arrange the methods according to the nesting hierarchy; the latter will group together the sibling methods from the same nesting level.

    class foo { public function parent() { $this->child1(); $this->child2(); } private function child1() { $this->nested1(); } private function nested1() { $this->nested2(); } private function nested2() { } private function child2() { } }
    class foo { public function parent() { $this->child1(); $this->child2(); } private function child1() { $this->nested1(); } private function child2() { } private function nested1() { $this->nested2(); } private function nested2() { } }

Create matching rules

Matching rules let you define the order of elements as a list of rules, where every rule has a set of matching conditions, such as modifier or type.

  1. Press Ctrl+Alt+S to open settings and select Editor | Code Style | PHP.

  2. On the Arrangement tab, click the Add button and provide the rule parameters in the Matching rules area.

    • Use the Type and Modifier filters to choose the code constructs and their visibility modifiers that should be regulated by the rule. Note that double-clicking a filter negates the condition.

    • Use the Name field to specify entry names the rule should affect.

      This filter matches only entry names, such as property names, method names, class names, and so on. The filter supports regular expressions and uses the standard syntax. The match is performed against the entire name.

    • To sort code entries alphabetically, select the appropriate Matching rules entry and set the Order field to order by name.

      Matching rules example

    You can also create groups (aliases) of rules and refer to them when creating a new matching rule.

Create rule aliases

With aliases, you can group several arrangement rules into a single entity and refer to it when you are adding a new matching rule.

  1. Press Ctrl+Alt+S to open settings and select Editor | Code Style | PHP.

  2. On the Arrangement tab, click Configure matching rules aliases.

  3. In the Rules Alias Definitions dialog that opens, add a group name and its rules.

    • the Add button: adds a new alias.

    • the Remove button: removes an existing alias from the list.

    • the Duplicate button: copies a specified rule sequence to the created alias.

  4. In the Rules alias definitions area, define the rule sequence for the created alias.

    Rules Alias Definitions

The created alias can now be referred to when adding a matching rule.

Create section rules

Section rules let you move methods or variables into the sections that you have defined.

  1. Press Ctrl+Alt+S to open settings and select Editor | Code Style | PHP.

  2. On the Arrangement tab, click the Add Section Rule button and provide the rule parameters in the Matching rules area.

    For example, you can create the following section rule:

    A section matching rule example

    After the arrangement, methods in the class will be rearranged as specified in the created section rule and will be surrounded by comments.

    //methods start public function test() {} private function a() { return 1; } static function r() {} //methods end

Rearrange code

  1. Press Ctrl+Alt+S to open settings and then select Editor | Code Style.

  2. In the editor, select the code entries you want to rearrange and go to Code | Rearrange Code in the main menu.

Automatically rearrange code on save

You can configure the IDE to rearrange code elements in modified files automatically when your changes are saved.

  1. Press Ctrl+Alt+S to open settings and then select Tools | Actions on Save.

  2. Enable the Rearrange code option.

    Additionally, you can click Configure arrangement rules to specify the rules for reordering code elements for the selected language.

Last modified: 08 October 2024