Use tab for indentation in PHP-CS-Fixer
Asked Answered
B

3

16

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.
Borchardt answered 17/3, 2017 at 4:54 Comment(0)
B
31

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)
Borchardt answered 22/3, 2017 at 11:29 Comment(2)
It's really annoying to have these arrays and no good docs on all the rules... A method like ->useTabsForIndent() would be so much more descriptive and easy to discover with any idea providing autocomplete.Germaun
Even though having a single method would be nice, I consider it only as a minor inconvenience. Since this is a one time configuration for a project, and almost always the same across projects, I don't see the IDE autocomplete as a huge advantage.Borchardt
M
3

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
Manyplies answered 23/10, 2020 at 6:48 Comment(5)
The question asks about using tab for indentation, and your answer explains how to use space.Borchardt
@JoyceBabu Thank you for your comment, I have updated the code for tab.Manyplies
Now this is a duplicate of the existing answer. What is the value addition?Borchardt
if we have to fix the issue we need the code as well the command to fix the issue.Manyplies
Configuration file .php_cs is outdated, rename to .php-cs-fixer.php.Stickle
H
2

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.

Hesperus answered 4/8, 2023 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.