Hướng dẫn phpstorm code style laravel
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 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. Be mindful of the PHPDoc layout, which dictates 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. 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.
Item Description Use tab character If this checkbox is selected, tab characters are used: On pressing the
Tab key For indentation For code reformatting When the checkbox is cleared, PhpStorm uses spaces instead of tabs. Smart tabs If this checkbox is selected, the part of indentation defined by the nesting of code blocks, is made of the tabs and (if necessary) spaces, while the part of indentation defined by the alignment is made only of spaces. If this checkbox is cleared, only tabs are used. This means that a group of spaces that fits the specified tab size is automatically replaced with a tab, which may result in breaking fine alignment. 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 SpacesSelect or clear the checkboxes to have spaces inserted, not inserted, or removed in various PHP contexts. Before parentheses
Around operators
Before left brace
Before keywords
Within
In ternary operator (?:)
Other
Wrapping and BracesCommon options
Keep when reformattingUse these settings to configure exceptions that PhpStorm will make when reformatting the source code.
Braces placementIn this section, choose the position for opening braces in declarations of namespaces, classes, and functions, in loops, and in other constructs.
Extends/implements list
Extends/implements keywordIn this section, configure wrapping for the If you select Do not wrap, no wrapping will be applied; if you select Wrap if long or Wrap always, each keyword and each item in
an abstract class Foo extends Class1 implements Class2, Class3 { } abstract class Foo extends Class1 implements Class2, Class3 { } 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
Attributes for parameters
Blank linesUse this tab to define where and how many blank lines you want PhpStorm to retain and insert in your code after reformatting. For each type of location, specify the number of blank lines to be inserted. The results are displayed in the preview pane.
PHPDocIn this tab, configure the code style to be applied inside PHPDoc comments. Learn more about documenting PHP code at PHPDoc comments.
Code ConversionIn this tab, configure the code style to be applied when performing automatic code conversion.
Code GenerationIn this tab, configure the code style to be applied when performing certain code generation and refactoring procedures.
ArrangementIn this tab, define a set of rules to rearrange your PHP code according to your preferences.
Last modified: 01 August 2022 |