Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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