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

17,888

Solution 1

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:

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
17,888
gmponos
Author by

gmponos

I believe in the phrase Tabula Rasa which means that no one is born knowing everything. You evolve every second of your life. You learn something new everyday. I never judge someone who doesn't know something. I try with patience to teach him everything I might know and support him the way I can. Furthermore, I enjoy programming in many languages but I prefer PHP and C. I have excellent knowledge of MVC architecture. I mostly use CakePHP Framework for developing but I am also familiar with many other MVC frameworks (CodeIgniter, Laravel) and CMS applications (Wordpress). I like using Twitter Bootstrap for front-end and many other JavaScript tools like DataTables, qTip2 etc.

Updated on June 22, 2022

Comments

  • gmponos
    gmponos about 2 years

    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?