Configure PHP-cs-fixer indentation for 2 spaces rather than 4?
Asked Answered
I

1

9

Not sure if this a better question for here or SuperUser. If it belongs there feel free to move it.

I'm using php-cs-fixer, and I have a unique requirement on indentations - I need two spaces rather than four. Is there a way to change this setting?

Note that I'm using atom-beautifier to run php-cs-fixer, so any solution should ideally be workable from there.

Ingeborg answered 12/5, 2016 at 17:18 Comment(2)
Please provide some examples of what you have tried. Will help community not repeat your efforts.Allister
The only thing I've tried on this is shutting off the indentation check on php-cs-fixer by adding -indentation in the "Fixers" field in Beatify's Atom settings, which seems to have no impact in Atom's usage of the tool.Ingeborg
M
11

You can set the PHP-CS-Fixer config file path and set the setIndent() value as defined at PHP-CS-Fixer's documentation website.

See image for Atom package settings

Set the .php_cs with something like this. Note the ->setIndent(" ") with two spaces instead of a \t character.

<?php

/*
 * This file is part of PHP CS Fixer.
 * (c) Fabien Potencier <[email protected]>
 *     Dariusz Rumiński <[email protected]>
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

$header = <<<'EOF'
This file is part of PHP CS Fixer.
(c) Fabien Potencier <[email protected]>
    Dariusz Rumiński <[email protected]>
This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

return PhpCsFixer\Config::create()
  /* ... other settings */
  ->setIndent("  ")
  /* ... other settings */

I am using php-cs-fixer plugin version 4.1.0 here.

Maculate answered 23/7, 2017 at 0:38 Comment(4)
Thanks for your answer! I note that <?php return PhpCsFixer\Config::create() ->setIndent(" "); is enough to change the indentation, though your example also shows how to combine it with other settings.Folketing
@Zachary I saved the config file you posted above into a file named .php_cs in the root folder of my project so that atom-beautify for php will use those rules, but now atom-beautify does not even do anything for my php files again. No more formatting! Removing the file restores atom-beautify formatting though, but I want it to use the file so I can specify my coding style. What do I do?Laplace
@OlowookereEmmanuel, only change the required line(s). Specifically the setIndent(" ") line.Maculate
For those (like me) who'd prefer not to keep track of white space count: <?php return PhpCsFixer\Config::create()->setIndent(str_pad('', 2));Stinko

© 2022 - 2024 — McMap. All rights reserved.