Hướng dẫn phpstorm psr-12 code style
PSR-2 đang dần là chuẩn Coding Convention phổ biển trong các dự án lập trình PHP, và đang được các PHP Framework nổi tiếng hỗ trợ như Laravel, Symfony 2… Nếu bạn đang sử dụng PHPStorm để lập trình, thì thủ thuật này sẽ giúp các bậu cấu hình format code PHP theo chuẩn PSR-2. Show
Cấu hình PSR-2 làm chuẩn code style mặc định cho PHPStormMở PHPStorm IDE lên. Vào [ File ] -> [ Setting ] -> [ Editor ] -> [ Code Style ] -> [ PHP ] – Scheme: Default IDE Chọn [ Predefined Style ] là: PSR1/PSR2 Hướng dẫn format code trong PHPStormChọn folder/file source code mà bạn muốn format, vào [ Code ] -> [ Reformat Code ]. Hoặc bấm tổ hợp phím Ctrl + Alt + L để format code. Nguồn: vinasupport.com
From the php-fig site. Long story short, while not mandatory, it covers more code cases than PSR-2. I'd like to use it in PhpStorm. Unfortunately, by default, storm doesn't offer this option. At predefined styles you have a fairly comprehensive list, but not comprehensive enough. Of course, I can always define the style myself doing it by hand, but that doesn't strike me as a good idea. Mostly because I WILL miss something and if there are any updates to the PSR I can't easily update it. Not to mention I can't share it properly with my team. Except maybe exporting my .idea folder. I googled around for a plugin or something of that sort but came up empty handed.
PhpStorm 2019.3 Released: Full PHP 7.4 Support, PSR-12, WSL, MongoDB, and MorePhpStorm 2019.3 is now available! This major release is focused on performance and quality, expect a more stable and faster IDE. It also brings full support for PHP 7.4, PSR-12 code style, WSL for remote interpreters, MongoDB support, HTTP Client updates, and many usability improvements! Download PhpStorm 2019.3 In this blog post, we’ll provide a detailed overview of all the changes and things that you should know about this new version. It’s a combination of all the EAP blog posts that we’ve been publishing here over the past several months plus many other useful additions that 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:
Reformat code in a folder
Reformat line indentsYou can reformat line indents based on the specified settings.
Automatically reformat code on saveYou can configure the IDE to reformat code in modified files automatically when your changes are saved.
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 |