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

4

38

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?

Bodega answered 15/3, 2016 at 18:42 Comment(0)
S
73

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:

Sita answered 15/3, 2016 at 19:7 Comment(4)
To me did not work until I also enabled Editor -> General -> Virtual Space -> Allow placement of caret after end of lineGilbertina
As of PhpStorm 2020.3.2, I see this setting worded slightly differently: Ensure every saved file ends with a line break.Maki
@KyleTolle Yeah, they seem to make changes in wording from time to time. Could also be that not everybody was understanding how that option worked...Sita
Yes, the current wording does seem to be clearer and match what I'd expect.Maki
A
15

someone said picture speaks louder :D

you will need to tick the Ensure line feed at file end on save box

enter image description here

Azerbaijan answered 20/9, 2017 at 14:50 Comment(0)
R
11

In PhpStorm preferences go to:

Editor > General

Check the box "Other: Ensure line feed at file end on Save."

Just realize this affects all code files you edit in PhpStorm, not just PHP files.

Rn answered 15/3, 2016 at 18:44 Comment(0)
P
7

Under Settings: Editor > General click Ensure line feed at file end on save is checked.

It will add a new line to EVERY file (.js, .css, .html), but it's the only way I've found, and it's a small hit for allowing PSR-2.

Peroxidase answered 15/3, 2016 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.