Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonasik/9d729ad21b7e6561a4fef4e52919a0cf to your computer and use it in GitHub Desktop.
Save samsonasik/9d729ad21b7e6561a4fef4e52919a0cf to your computer and use it in GitHub Desktop.
cara penggunan codeigniter coding standard

CodeIgniter Coding Standard v1.0.0 released! https://github.com/CodeIgniter/coding-standard/releases/tag/v1.0.0

Cara pakai:

  1. Daftarkan code ke Version Control biar bisa di-undo kalau ada false positive, contoh undo dari git:
git checkout .
  1. Require via composer:
composer require --dev codeigniter/coding-standard
  1. Buat file .php-cs-fixer.dist.php

a. Untuk project, cukup dengan konfigurasi

<?php

use CodeIgniter\CodingStandard\CodeIgniter4;
use Nexus\CsConfig\Factory;

return Factory::create(new CodeIgniter4())
    ->forProjects();

atau untuk custom config:

<?php

use CodeIgniter\CodingStandard\CodeIgniter4;
use Nexus\CsConfig\Factory;
use PhpCsFixer\Finder;

$finder = Finder::create()
    ->files()
    ->in([
        __DIR__ . '/app',
        __DIR__ . '/data',
        __DIR__ . '/public',
        __DIR__ . '/tests',
    ])
    ->exclude(['Views']);

$options = [
    'finder' => $finder,
];

return Factory::create(new CodeIgniter4(), [], $options)
    ->forProjects();

Contoh hasil generate cs-fix untuk project: https://github.com/samsonasik/ci4-vue/commit/d978c6a66b29653982298daa3dfbea4ab2406e44

b. Jika untuk library atau custom module, bisa seperti ini:

<?php

use CodeIgniter\CodingStandard\CodeIgniter4;
use Nexus\CsConfig\Factory;
use PhpCsFixer\Finder;

$finder = Finder::create()
    ->files()
    ->in([
        __DIR__ . '/src',
        __DIR__ . '/test',
    ])
    ->exclude(['Views']);

$options = [
    'finder' => $finder,
];

return Factory::create(new CodeIgniter4(), [], $options)
    ->forLibrary('namapackage/library', 'Nama Author', 'emailauthor@email.com', 2021);

Contoh hasil generate cs-fix untuk module: https://github.com/samsonasik/ci4-album/commit/ddff6fefd7ffa22da93ecbdbd09fe6f7b98e3c7c

  1. Run preview diff perubahan:
vendor/bin/php-cs-fixer fix --dry-run --diff
  1. Kalau sudah yakin, running fixnya:
vendor/bin/php-cs-fixer fix

Enjoy ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment