Joyce Babu
Joyce Babu

Reputation: 20714

Use tab for indentation in PHP-CS-Fixer

How can I configure PHP-CS-Fixer to use tab for indentation?

I can see the indentation_type fixer

* indentation_type [@PSR2, @Symfony]
  | Code MUST use configured indentation type.

But how do I configure the indentation type? If I try to set 'indentation_type' => 'tab',, I am getting the error

[indentation_type] Is not configurable.

Upvotes: 15

Views: 7553

Answers (3)

Jan 'splite' K.
Jan 'splite' K.

Reputation: 2124

2023

Config::create() has been deprecated in version 2.17 and removed in 3.0 (see upgrade guide). You should use the constructor instead. (julienfalque)

Now its .php-cs-fixer.php:

<?php
use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = PhpCsFixer\Finder::create()
    /*
    ->exclude('somedir') 
    maybe we should parse `./.vscode/settings.json`
    looking for "files.exclude" / "search.exclude" here?
    */
    ->in(__DIR__);

$config = new PhpCsFixer\Config();

$config->setRules([
        '@PSR2' => true,
        'indentation_type' => true,
    ])
    ->setIndent("\t")
    ->setLineEnding("\n")
    ->setFinder($finder);

return $config;

see documentation.

Upvotes: 2

Aryashree Pritikrishna
Aryashree Pritikrishna

Reputation: 351

First create .php_cs file in your project root directory. Then add the below lines to .php_cs file

<?php
return PhpCsFixer\Config::create()
->setRules([
    '@PSR2' => true,
    'indentation_type' => true,
])
->setIndent("\t")
->setLineEnding("\n")
->setFinder($finder)

Then run the below command to fix the issue

vendor/bin/php-cs-fixer fix . --config .php_cs

Upvotes: 2

Joyce Babu
Joyce Babu

Reputation: 20714

It was there in the documentation, and some how I missed it (probably because I was searching for the term 'tab' instead of 'indentation')

For anyone else looking for the same, there is a setIndent method in PhpCsFixer\Config.

return PhpCsFixer\Config::create()
    ->setRules([
        '@PSR2' => true,
        'indentation_type' => true,
    ])
    ->setIndent("\t")
    ->setLineEnding("\n")
    ->setFinder($finder)

Upvotes: 30

Related Questions