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
Hướng dẫn format code theo chuẩn PSR-2 cho PHPStorm
PHPStorm IDE 13/05/2018 7,246 lượt xem 2 Bình luận
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. […]
PSR-2 đang dần trở thành chuẩn Coding Convention phổ biến trong các dự án lập trình PHP, và đang được hỗ trợ bởi các PHP Framework nổi tiếng 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 ích cho bạn. Cấu hình định dạng mã PHP theo tiêu chuẩn PSR-2.
Định cấu hình PSR-2 làm kiểu mã mặc định cho PHPStorm
Mở PHPStorm IDE. Nhập [ File ] -> [ Setting ] -> [ Editor ] -> [ Code Style ] -> [ PHP ]
– Cơ chế: IDE mặc định
– Nhấp vào thiết lập từ…
Lựa chọn [ Predefined Style ] được: PSR1 / PSR2
Hướng dẫn định dạng mã trong PHPStorm
Chọn thư mục / File mã nguồn bạn muốn định dạng, đi tới [ Code ] -> [ Reformat Code ]. Hoặc nhấn Ctrl + Alt + L để định dạng mã.
Nguồn: vinasupport.com
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.
Nội dung chính
- Cấu hình PSR-2 làm chuẩn code style mặc định cho PHPStorm
- Hướng dẫn format code trong PHPStorm
- PhpStorm Laravel Code Style
- Installation
- Set from...
- Tabs and Indents
- Before parentheses
- Around operators
- Before left brace
- Before keywords
- In ternary operator [?:]
- Wrapping and Braces
- Common options
- Keep when reformatting
- Braces placement
- Extends/implements list
- Extends/implements keyword
- Function declaration parameters
- Function/constructor call arguments
- Chained method calls
- 'if[]' statement
- for[]/foreach[] statements
- 'while[]' statement
- 'do ... while[]' statement
- 'switch' statement
- 'try' statement
- Binary expressions
- Assignment statement
- Class property/constant groups
- Ternary operation
- Array initializer
- Modifier list
- Function return type
- PHP opening tag
- 'match' expression
- Group 'use'
- Attributes for parameters
- Blank lines
- Code Conversion
- Code Generation
- Arrangement
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
PhpStorm Laravel Code Style
This is a code style based on PSR-2, as per Laravel's Contribution Guide
Installation
Configuration
folder : Drop the Laravel.xml
file in the /config/codestyles/
subfolder within your configuration directory.
IDE Settings : Open your Settings [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
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.
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.
Set from...
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:
{{ some_variable }}
.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.
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ủ Đề |