Pint - Code Style or Linter
Project ini menggunakan Laravel Pint dengan preset laravel dan beberapa aturan tambahan/kustom.
Tujuannya untuk menjaga konsistensi dan kualitas kode di seluruh project.
Visual Studio Codeβ
Jika anda menggunakan visual studio code gunakanlah ekstensi Laravel Pint untuk formating dengan Pint, Biasanya anda melakukan formating php menggunakan Ekstensi PHP Intelephense.
Saya merekomendasikan Laravel Pint untuk best partice linter.
Buka file .php > click kanan mouse pada editor > Format Document With > Laravel Pint. Atau bisa diconfigurasi secara default lewat settings.json vscode:
"editor.formatOnSave": true,
"[php]": {
"editor.defaultFormatter": "open-southeners.laravel-pint"
},
π File yang Dikecualikanβ
Rule Pint tidak dijalankan pada file berikut:
tests/TestCase.phpintelephense_helper.php_ide_helper.php
βοΈ Aturan yang Digunakanβ
Berikut adalah aturan yang aktif di pint.json:
array_pushβ
Gunakan [] ketimbang array_push.
// Before
array_push($arr, 1);
// After
$arr[] = 1;
backtick_to_shell_execβ
Hindari backtick, gunakan shell_exec().
// Before
$a = `ls -la`;
// After
$a = shell_exec('ls -la');
date_time_immutableβ
Wajib gunakan DateTimeImmutable alih-alih DateTime.
// Before
$d = new DateTime();
// After
$d = new DateTimeImmutable();
declare_strict_typesβ
Tambahkan declare(strict_types=1); di setiap file PHP.
<?php
// Before: tanpa declare
// After
declare(strict_types=1);
lowercase_keywordsβ
Kata kunci PHP selalu huruf kecil.
// Before
IF ($a) {}
// After
if ($a) {}
lowercase_static_referenceβ
Gunakan lowercase untuk self, static, parent.
// Before
SELF::class;
// After
self::class;
final_classβ
Semua class ditandai final kecuali memang perlu di-extend.
// Before
class User {}
// After
final class User {}
no_superfluous_elseifβ
Hindari elseif yang bisa jadi if.
// Before
if ($a) {
} elseif ($b) {
}
// After
if ($a) {
}
if ($b) {
}
no_useless_elseβ
Hindari else setelah return.
// Before
if ($a) return 1;
else return 2;
// After
if ($a) return 1;
return 2;
no_multiple_statements_per_lineβ
Satu statement per baris.
// Before
$a = 1; $b = 2;
// After
$a = 1;
$b = 2;
ordered_class_elementsβ
Elemen class harus sesuai urutan (use, const, property, construct, method, dll).
// Before
public function foo() {}
public const BAR = 'bar';
// After
public const BAR = 'bar';
public function foo() {}
protected_to_privateβ
Property/method protected jadi private jika tidak diwariskan.
// Before
protected $value;
// After
private $value;
strict_comparisonβ
Gunakan === atau !== bukan == atau !=.
// Before
if ($a == $b) {}
// After
if ($a === $b) {}
visibility_requiredβ
Semua method/property wajib ada visibility.
// Before
function foo() {}
// After
public function foo() {}
β Rule Dimatikanβ
new_with_parenthesesβ
Rule ini dimatikan (false) β membolehkan new Class tanpa ().
// Allowed
$user = new User;
π Menjalankan Pintβ
Linting otomatis bisa dijalankan dengan:
composer lint
Atau untuk mengecek tanpa auto-fix:
composer lint -- --test
π Catatanβ
-
Jika ada rule yang dianggap terlalu ketat, bisa dimatikan di
pint.json. -
Selalu commit perubahan linting secara terpisah dengan prefix:
chore(lint): apply Pint code style fixes