-1

I was unable to find out how many spaces we have to use for intendation according to the PSR-2 standard. Can I use as many as I like?

I always use 4 spaces

public function test()
{
    //4 spaces

But I also saw code which uses more than 24 spaces. Which is the coding standard? I changed code with 24 spaces intendation to 4 spaces and then my coworker asked me why I changed it.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Black
  • 18,150
  • 39
  • 158
  • 271

2 Answers2

6

PSR-4 is an autoloading standard. It has NOTHING to do with indentation or spaces.

https://www.php-fig.org/psr/psr-4/

You are thinking of PSR-2, the coding style guide.

https://www.php-fig.org/psr/psr-2/

Code MUST use 4 spaces for indenting, not tabs.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
2

There are many different coding standards, so the first thing you need to check is which one the code you are using should be following.

If the code in question is following PSR-2 (not PSR-4, which is an unrelated standard), then section 2.4 clearly states:

Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting.

Note that this doesn't mean "every line of code in a file should be indented by exactly 4 spaces", it means "each time you open a new structure which requires indenting, increase the indent by exactly 4 spaces".

So 24 spaces would be perfectly valid for 6 layers of indenting; e.g. an array definition within an if within a loop within a switch within a function within a class.

To reiterate, this assumes PSR-2 is actually the agreed standard for this code; 24 spaces would also be valid for 3 levels of indentation if the coding standard specified 8 spaces per indent. There might even be some exotic coding standard that uses indentation to mean something other than nesting level, so that the 24 spaces were to produce some alignment that wasn't obvious to you.

IMSoP
  • 89,526
  • 13
  • 117
  • 169