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.php
intelephense_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