Tabs or Spaces? Ok spaces, but then 2 or 4 spaces? Some of you probably will answer “doesn’t matter, it will still work”. Let me guess, you probably work alone, not in a team of developers? Cause as soon as there are multiple people pushing the code, there needs to be a coding standard. This article will show you how to adopt it.

Let’s start with a simple example.

class ChangePasswordController extends Controller {

    public function edit() {
        return view('profile.password.edit');
    }

}

or

class ChangePasswordController extends Controller 
{
    public function edit() 
    {
        return view('profile.password.edit');
    }
}

Notice the difference above? { brackets are on a new line, for both class name and function edit(). No big deal, you would say?

But if we follow currently most popular PHP standards PSR-2/PSR-12, the second code is the correct one – it should be new lines.


What is the Actual Problem if Code is not Formatted?

As I said above, code styling is not a problem if you work alone, and no one else would edit your code in the future.

But for serious projects, if they grow, then the team will likely expand. Not to mention open-source projects that may have hundreds of contributors.

Now, imagine you work in a team and push the code formatted in any way you want, without looking at any standard.

And your teammate pulls down that code to make some changes. But he’s coding to the standard and his IDE is formatting code automatically (later I will show how to do it).

Then what happens – he pushes the code with the changes, and that commit has two things:
– a lot of re-formatting the code
– and then the main code of commit

See the screenshot from SourceTree – this could be a commit with a small fix:

Only three lines of code related to the actual fix, everything else is a “noise”. It’s very difficult to read for anyone who wants to review the code or to find in history what was changed exactly.

In other words, if different teammates use different coding standards (or none at all), then code will be full of re-formatting or, even worse, differently formatted code in different places.

It may sound like a non-issue, but imagine having different fonts in a big Word document. It would be still readable, but not pleasant.


What is Code Style/Standard and Why PSR-2/PSR-12?

Code standard describes the rules like:

  • Tabs or Spaces? 2 Spaces or 4 Spaces? Correct answer is 4 spaces (by the way, hilarious video from “Silicon Valley” TV show)
  • “{” symbol for methods – on the same line or separate line? (answer is separate line)
  • Do we have to declare public/private for all class properties? (yes)
  • Method names: “snake_case” or “camelCase”? (camelCase)

In PHP language, for a long time, the most popular standard has been PSR-2. In August 2019, it was replaced by PSR-12 that became a new standard.

But both are pretty similar, PSR-12 is kind of an extended PSR-2, adapted to modern PHP syntax. So you may find PSR-2 in older articles, and PSR-12 in newer ones.

Also, PSR-12 is expanding on a Basic Coding Standard called PSR-1.

In general, both PSR-12 and PSR-1 standards are very long documents and you don’t have to know all the rules. But I encourage you to read both standards at least once and note down some of the most widely used questions.


How to Reformat Code in Seconds with IDE?

I told you don’t need to learn the rules, partly because there are tools that reformate the code for us.

See quick screencast what can be done by PhpStorm by using menu Code -> Reformat Code, or just shortcut Cmd + Shift + L:

You can read more about Code Reformatting in PhpStorm here and here.

How to tell PhpStorm, what code style to adopt? It can be done in the Preferences modal window, in Editor -> Code Style -> PHP, then choose “Set from” and pick your style, I recommend PSR-12.

Notice: screenshots and shortcuts here are for Mac users, look into PhpStorm docs for Windows.

Also, I’m sure such formatters exist in other IDEs like Sublime Text, VS Code and others. So please search in their documentation.


Code Formatting Tools

Also, there are tools to automatically format many files in all your project, you can also attach to your process of deployment – so format all files either locally, or on the server.


Please Please, Push Formatting Changes in a Separate Commit

Final message. Whatever code formatting method you use, if you need to reformat the existing file and push it to the repository, please do it in a separate commit.

If done manually, you can do it in such a sequence:

  • Make the code changes and commit the code
  • Click “Code Reformat” in PhpStorm and if there are changes – commit with message “Code formatting”

It can also be automized with the tools mentioned above.


Conclusion

Code styling and formatting is a part of effective work in a team. So if you want to make life easier for your teammates, please agree on code standard and adapt it together.