8

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.

dluxcru
  • 397
  • 1
  • 5
  • 16
  • Please provide some examples of what you have tried. Will help community not repeat your efforts. – muglio May 12 '16 at 19:54
  • 1
    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. – dluxcru May 12 '16 at 20:35

1 Answers1

10

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 <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 * 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 <fabien@symfony.com>
    Dariusz Rumiński <dariusz.ruminski@gmail.com>
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.

Dr.Ransom
  • 118
  • 2
  • 8
  • Thanks for your answer! I note that `setIndent(" ");` is enough to change the indentation, though your example also shows how to combine it with other settings. – deltab Jul 23 '17 at 01:36
  • @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? – Damilola Olowookere Sep 02 '17 at 14:44
  • @OlowookereEmmanuel, only change the required line(s). Specifically the `setIndent(" ")` line. – Dr.Ransom Sep 04 '17 at 20:42
  • 1
    For those (like me) who'd prefer not to keep track of white space count: ```setIndent(str_pad('', 2));``` – Kola Jan 27 '20 at 09:08