Lewati ke konten utama

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