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.
Cấu hình PSR-2 làm chuẩn code style mặc định cho PHPStorm
Mở PHPStorm IDE lên. Vào [ File ] -> [ Setting ] -> [ Editor ] -> [ Code Style ] -> [ PHP ]
– Scheme: Default IDE
– Bấm vào set from…
Chọn [ Predefined Style ] là: PSR1/PSR2
Hướng dẫn format code trong PHPStorm
Chọ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
This is a code style based on PSR-2, as per Laravel's Contribution Guide Configuration folder : Drop the IDE Settings : Open your Settings [ This code style has been updated in line with Laravel 5.4, and matches Laravel's code styles as best as possible with PhpStorm's available
configuration.PhpStorm Laravel Code Style
Installation
Laravel.xml
file in the /config/codestyles/
subfolder within your
configuration directory.Ctrl + Alt + S
] and go to Editor > Code Style
section. Click on the Scheme
wrench on the right and select Import Scheme > Intellij IDEA code style XML
.Notes
Be mindful of the PHPDoc layout, which dictates @param
is followed by two spaces, the argument type, two more spaces, and finally the variable name, which I've not been able to replicate in PhpStorm.
Use this page to configure formatting options for PHP files. When you change these settings, the Preview pane shows how this will affect your code. Click this link to choose the base for the current language default code style from the list, that appears. The list
contains two options: Language: choose this option to inherit the coding style settings from another language. Select the source language from the list, that opens. Only the settings that are applicable to the current language are taken. All the other settings are not affected. Predefined code style: choose this option to use the coding standards defined for
a specific framework. Select one of the following frameworks from the list: PEAR Zend Symfony2. PhpStorm supports the official Symfony2 code style for Twig and automatically inserts one space after an opening pair of curvy braces and
before a closing pair of curvy braces in Twig templates: PSR1/PSR-2 CodeIgniter Laravel PSR-12. If the PSR-12 code style is set, PhpStorm will
prompt you to automatically enable the PSR-12-related code inspections.Set from...
{{ some_variable }}
.
Drupal
Joomla!
WordPress
This link appears in the upper-right corner of the language-specific code style page, when applicable.
Click Reset to discard changes and return to the initial set of code style settings.
Tabs and Indents
Use tab character |
|
Smart tabs |
The Smart tabs checkbox is available if the Use tab character checkbox is selected. |
Tab size | In this field, specify the number of spaces included in a tab. |
Indent | In this field, specify the number of spaces to be inserted for each indent level. |
Continuation indent | In this field, specify the number of spaces to be inserted between the elements of an array, in expressions, method declarations and method calls. |
Keep indents on empty lines | If this checkbox is selected, PhpStorm will keep indents on the empty lines as if they contained some code. If this checkbox is cleared, PhpStorm will delete the tab characters and spaces. |
Indent code in PHP tags | Select this checkbox to have the code enclosed in Chủ Đề |