-
Notifications
You must be signed in to change notification settings - Fork 1
create pull best practice #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hapo-tuanlh
wants to merge
2
commits into
master
Choose a base branch
from
create/best_practice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| ## if, elseif, else | ||
| - Bạn hãy chú ý đến các dấu ngoặc đơn, khoảng trắng và dấu ngoặc nhọn; các từ khóa như else và elseif phải được đặt dùng dòng với dấu ngoặc nhọn đóng của phần thân cấu trúc điều khiển phía trước và nên sử dụng elseif hơn là else if.. | ||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| if ($expr1) | ||
| { | ||
| // if body | ||
| } | ||
| elseif ($expr2) | ||
| { | ||
| // elseif body | ||
| } | ||
| else | ||
| { | ||
| // else body; | ||
| } | ||
|
|
||
| //good | ||
| <?php | ||
|
|
||
| if ($expr1) { | ||
| // if body | ||
| } elseif ($expr2) { | ||
| // elseif body | ||
| } else { | ||
| // else body; | ||
| } | ||
| ```` | ||
| - Biểu thức ở giữa cặp dấu ngoặc đơn có thể được viết trên nhiều dòng, với mỗi dòng sẽ được thù lề thêm một cấp. Điều kiện đầu tiên sẽ phải được viết trên dòng tiếp theo. Dấu ngoặc đơn đóng và dấu ngoặc nhọn mở phải được viết trên cùng một dòng và ngăn cách bằng một khoảng trắng. | ||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| if ( $expr1 && $expr2 ) { | ||
| // if body | ||
| } elseif ( $expr3 && $expr4 ) { | ||
| // elseif body | ||
| } | ||
| //good | ||
| <?php | ||
|
|
||
| if ( | ||
| $expr1 | ||
| && $expr2 | ||
| ) { | ||
| // if body | ||
| } elseif ( | ||
| $expr3 | ||
| && $expr4 | ||
| ) { | ||
| // elseif body | ||
| } | ||
| ```` | ||
|
|
||
| ## switch, case | ||
| - Bạn hãy để ý tới vị trí của các dấu ngoặc đơn, khoảng trắng và dấu ngoặc nhọn. Từ khóa case phải được thụt lề thêm một cấp so với từ khóa switch, và từ khóa break (hoặc từ khóa kết thúc như return) phải được thụt lề thêm một cấp so với case. | ||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| switch ($expr) { | ||
| case 0: | ||
| echo 'First case, with a break'; | ||
| break; | ||
| case 1: | ||
| echo 'Second case, which falls through'; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| echo 'Third case, return instead of break'; | ||
| return; | ||
| default: | ||
| echo 'Default case'; | ||
| break; | ||
| } | ||
|
|
||
| //good | ||
| <?php | ||
|
|
||
| switch ($expr) { | ||
| case 0: | ||
| echo 'First case, with a break'; | ||
| break; | ||
| case 1: | ||
| echo 'Second case, which falls through'; | ||
| // no break | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| echo 'Third case, return instead of break'; | ||
| return; | ||
| default: | ||
| echo 'Default case'; | ||
| break; | ||
| } | ||
| ```` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| ## Tham số của method và function | ||
| - Trong danh sách các tham số, không được có khoảng trắng ở trươc dấu phẩy, và phía sau dấu phẩy luôn phải có một khoảng trắng. | ||
|
|
||
| Các tham số mà có giá trị mặc định phải nằm ở các vị trí sau cùng trong danh sách các tham số. | ||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| namespace Vendor\Package; | ||
|
|
||
| class ClassName | ||
| { | ||
| public function foo(int $arg1 , &$arg3 = [] , $arg2) | ||
| { | ||
| // method body | ||
| } | ||
| } | ||
| //good | ||
| <?php | ||
|
|
||
| namespace Vendor\Package; | ||
|
|
||
| class ClassName | ||
| { | ||
| public function foo(int $arg1, &$arg2, $arg3 = []) | ||
| { | ||
| // method body | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| - Khi bạn khai báo kiểu dữ liệu trả về, thì phải có một khoảng trắng nằm giữa dấu hai chấm : và kiểu dữ liệu. Dấu hai chấm và kiểu dữ liệu phải nằm cùng dòng với dấu ngoặc đơn đóng. | ||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Vendor\Package; | ||
|
|
||
| class ReturnTypeVariations | ||
| { | ||
| public function functionName(int $arg1, $arg2):string | ||
| { | ||
| return 'foo'; | ||
| } | ||
|
|
||
| public function anotherFunction( | ||
| string $foo, | ||
| string $bar, | ||
| int $baz | ||
| ): string { | ||
| return 'foo'; | ||
| } | ||
| } | ||
| // good | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Vendor\Package; | ||
|
|
||
| class ReturnTypeVariations | ||
| { | ||
| public function functionName(int $arg1, $arg2): string | ||
| { | ||
| return 'foo'; | ||
| } | ||
|
|
||
| public function anotherFunction( | ||
| string $foo, | ||
| string $bar, | ||
| int $baz | ||
| ): string { | ||
| return 'foo'; | ||
| } | ||
| } | ||
| ```` | ||
| - Nếu khai khai bảo trả về kiểu nullable, thì không được có khoảng trắng giữa dấu hỏi chấm và kiểu dữ liệu. | ||
|
|
||
| Nếu sử dụng toán tử & trước các tham số, thì không được có khoảng trắng ở phía sau nó. | ||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Vendor\Package; | ||
|
|
||
| class ReturnTypeVariations | ||
| { | ||
| public function functionName(?string $ arg1, ?int & $arg2): ? string | ||
| { | ||
| return 'foo'; | ||
| } | ||
| } | ||
| //good | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Vendor\Package; | ||
|
|
||
| class ReturnTypeVariations | ||
| { | ||
| public function functionName(?string $arg1, ?int &$arg2): ?string | ||
| { | ||
| return 'foo'; | ||
| } | ||
| } | ||
| ```` | ||
| - Không được có khoảng trắng giữa dấu ba chấm ... và tên tham số: | ||
| ````php | ||
| //bad | ||
| public function process(string $algorithm, ... $parts) | ||
| { | ||
| // processing | ||
| } | ||
| //good | ||
| public function process(string $algorithm, ...$parts) | ||
| { | ||
| // processing | ||
| } | ||
| ```` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| --- | ||
|
|
||
| # Files | ||
|
|
||
| ## php tags | ||
| ## Đối với file PHP | ||
| - Nguyên tắc 1: File PHP chỉ được phép sử dụng <?php và <?=. <?php được sử dụng để mở đầu cho code PHP, và <?= là cú pháp short-echo (thay vì code là <?php echo $a ?>, bạn có thể code là <?= $a ?>) | ||
|
|
||
| - Nguyên tắc 2: File code PHP sử dụng encode UTF-8 without BOM. | ||
|
|
||
| - Nguyên tắc 3: File PHP NÊN dùng để khai báo các thành phần của PHP (class, function, const) và các hiệu ứng phụ (include, thiết lập init PHP), nhưng KHÔNG NÊN dùng cả hai trong một file. Để hiểu rõ hơn nguyên tắc này, bạn hãy xem ví dụ sau | ||
| ````php | ||
| //bad | ||
|
|
||
| <?php | ||
| // hiệu ứng phụ: đổi thiết lập ini | ||
| ini_set('error_reporting', E_ALL); | ||
|
|
||
| // hiệu ứng phụ: nạp file vào | ||
| include "file.php"; | ||
|
|
||
| // hiệu ứng phụ: xuất dữ liệu | ||
| echo "<html>\n"; | ||
|
|
||
| // khai báo hàm | ||
| function foo() | ||
| { | ||
| // function body | ||
| } | ||
|
|
||
| //good | ||
| //file functions.php | ||
| <?php | ||
| // functions.php | ||
|
|
||
| // khai báo hàm | ||
| function foo() | ||
| { | ||
| // function body | ||
| } | ||
|
|
||
| //file index.php | ||
|
|
||
| <?php | ||
| // index.php | ||
|
|
||
| // hiệu ứng phụ: đổi thiết lập ini | ||
| ini_set('error_reporting', E_ALL); | ||
|
|
||
| // hiệu ứng phụ: nạp file vào | ||
| include "file.php"; | ||
| include "functions.php"; | ||
|
|
||
| // hiệu ứng phụ: xuất dữ liệu | ||
| echo "<html>\n"; | ||
| ```` | ||
|
|
||
| ## Đối với khai báo namespace và class | ||
| - Nguyên tắc 1: namespace và class phải thuân theo chuẩn “autoload” PSR-0, PSR-4. | ||
|
|
||
| Mỗi class được khai báo trên một file PHP riêng và có namespace tối thiểu một cấp, cấp đầu tiên là tên vendor (tên đơn vị phát hành) | ||
|
|
||
| Tên class PHẢI được viết dạng ClassName thay vì classname, Classname, class_name hay Class_Name | ||
|
|
||
| Từ PHP 5.3, PHẢI sử dụng namespace khi khai báo class. | ||
|
|
||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| class Classname | ||
| { | ||
| // | ||
| } | ||
|
|
||
| //good | ||
| <?php | ||
|
|
||
| namespace Vendor; | ||
|
|
||
| class ClassName | ||
| { | ||
| // | ||
| } | ||
| ```` | ||
|
|
||
| ## Hằng, thuộc tính và phương thức của class | ||
| - Quy tắc 1: Hằng khai báo trong class phải được viết hoa và ngăn cách bằng dấu gạch dưới. | ||
|
|
||
| ````php | ||
| //bad | ||
| <?php | ||
| namespace Vendor\Model; | ||
|
|
||
| class Foo | ||
| { | ||
| const version = '1.0'; | ||
| const date_approved = '2012-06-01'; | ||
| } | ||
|
|
||
| //good | ||
| <?php | ||
| namespace Vendor\Model; | ||
|
|
||
| class Foo | ||
| { | ||
| const VERSION = '1.0'; | ||
| const DATE_APPROVED = '2012-06-01'; | ||
| } | ||
| ```` | ||
|
|
||
| - Tên phương thực phải được đặt ở dang tenPhuongThuc() | ||
|
|
||
| ````php | ||
| //bad | ||
| <?php | ||
|
|
||
| namespace Vendor\Model; | ||
|
|
||
| class Foo | ||
| { | ||
| public function method_name() | ||
| { | ||
| // | ||
| } | ||
| } | ||
|
|
||
| //good | ||
| <?php | ||
|
|
||
| namespace Vendor\Model; | ||
|
|
||
| class Foo | ||
| { | ||
| public function methodName() | ||
| { | ||
| // | ||
| } | ||
| } | ||
| ```` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dich lai phan commennt nay