Hướng dẫn format code phpstorm
PhpStorm lets you reformat your code according to the requirements you've specified in your current code style scheme or the .editorconfig file. If anything is not defined in You can reformat a part of code, the whole file, group of files, or a directory. You can also exclude part of code or some files from the
reformatting. In the editor, select a code fragment you want to reformat. Before reformatting, you can take a look at the code style settings that are applied to the selected code: press
Alt+Enter and click Adjust code style settings. From the main menu, select or press
Ctrl+Alt+L. Either open your file in the editor and press Ctrl+Alt+Shift+L or in the Project tool window, right-click the file and select . In the dialog that opens, if you need, select the following reformatting options: Optimize imports: select this option if you want to remove unused imports, add missing ones, or organize import statements. For more information, refer to the
Optimize imports section. Rearrange entries: select this option if you need to rearrange your code based on the arrangement rules specified in the code style
settings. Code cleanup: select this option to run the code cleanup inspections. Do not keep line breaks: reformat line breaks according to the code style settings. This option overrides the setting. In the project view,
right-click a folder and from the context menu, select Reformat Code or press
Ctrl+Alt+L. In the dialog that opens, specify the necessary options and click OK. You can reformat line indents based on the specified settings. While in the editor, select the necessary code fragment and
press Ctrl+Alt+I. If you need to adjust indentation settings, in the Settings/Preferences dialog
(Ctrl+Alt+S), go to . On the appropriate language page, on the Tabs and Indents tab, specify the appropriate indents options and click OK. You can configure the IDE to reformat code in modified files automatically when your changes are saved. Press
Ctrl+Alt+S to open the IDE settings and select . Enable the Reformat code option. Additionally, you can configure the way the IDE will reformat your code:
Exclude files from reformattingYou can exclude a group of files and directories from reformatting , code arrangement, and import optimization.
Exclude code fragments from reformatting in the editor
The example shows two code fragments. One of them is enclosed in the The original formatting is preserved: The original formatting is not preserved in either code fragment: Keep existing formattingYou can select formatting rules which will be ignored when you reformat the code. For example, you can adjust the IDE to keep simple methods and functions in one line, whereas normally they are expanded into multiple lines after code reformatting.
PhpStorm will reformat your code in accordance with the current style settings, keeping existing formatting for the rules which you've selected. Rearrange codeYou can rearrange your code according to the arrangement rules set in the Code Style. PHP page of the Settings/Preferences dialog. Configure grouping rulesGrouping rules let you keep related class methods together.
Create matching rulesMatching rules let you define elements order as a list of rules, where every rule has a set of matching conditions, such as modifier or type.
Create rule aliasesWith aliases, you can group several arrangement rules into a single entity and refer to it when you adding a new matching rule.
The created alias can now be referred to when adding a matching rule. Create section rulesSection rules let you move methods or variables into the sections that you have defined.
Rearrange code entries
Automatically rearrange code on saveYou can configure the IDE to rearrange code elements in modified files automatically when your changes are saved.
Last modified: 01 August 2022 |