Add an empty line at end of file according to PSR-2 on PhpStorm

gmponos picture gmponos · Mar 15, 2016 · Viewed 16k times · Source

I use PSR-2 for code styling my code.

When I inspect a file using Codesniffer most of the times I get the following error.

332 | ERROR   | [x] Expected 1 newline at end of file; 0 found

It's obvious how to fix this. What I need to know is if PhpStorm has a way to add the 1 newline at end of file

I already loaded the predefined style from Settings -> Editor -> Code Style -> PHP -> Set From -> PSR-1/PSR-2 and also used the Reformat Code to change the CS accordingly.

Everything is fixed except from the new line. Am I missing something?

Answer

LazyOne picture LazyOne · Mar 15, 2016

You already have two answers for how to turn it on for ALL file types in IDE (in short: Settings/Preferences | Editor | General -> Ensure line feed at file end on Save).

In modern version (2020.3) that option has been reworded a bit. Now it says Ensure every saved file ends with a line break

enter image description here


To do this for .php files only you can do the following:

  1. Install EditorConfig plugin (if you do not have it installed yet). In modern versions it is already bundled and enabled by default.
  2. Create .editorconfig file in your project root (or whatever folder that would be where you want to apply such style -- it will be applied to files in this folder and below). If you have one already -- open and inspect it
  3. Add rule for *.php files only -- property to use would be insert_final_newline

An example:

[*.php]
insert_final_newline = true

Links: