4

I want my IDE (PhpStorm) to enforce PSR-2 compatible formatting on all of own files. Occasionally however, I have to submit patches on legacy code where I only want to commit functional changes, not changes in code style.

How can I enforce PSR-2 using PhpStorm on only a subset of files, preferably using a magic comment / marker in the files which should be enforced?

ooxi
  • 3,159
  • 2
  • 28
  • 41
  • Such setting (Code Style) is applied to ALL files in a project. ATM there is no way to have more than one scheme in the same project (e.g. one gets applied to these files; another to other). But, different projects can have different rules. Right here in these answers the IDE-wide scheme is used (so it can be applied to any project). You may just use Project-specific one that will work for this project only. – LazyOne May 12 '17 at 09:04
  • I may only suggest to switch between schemes when needed (it's pretty quick if you use `Quick Switch` functionality) .. but it needs to be done manually (and you have to remember to do that .. and then switch back to another when editing other files). To help you differentiating what file it is ... use custom Scope .. and then File Colors -- to give different background to Editor Tabs based on certain criteria (path/file name match). – LazyOne May 12 '17 at 09:05
  • @ooxi or anyone, have you found the answer yet? – Martijn Oct 16 '19 at 13:52
  • @Martijn unfortunately not :-( – ooxi Oct 18 '19 at 11:59

2 Answers2

4

In PHPstorm you can set default code style for all specific language, Since you mentioning about php, you can follow the step to set PSR-2

  • Go to setting ( File->Setting )
  • The windows will pop up, where you can search "code style"
  • Now click on PHP and you will see "Set from" , ( Here you can set from for default code style )

PHP-STORM PSR-2 SETUP BY DEFAULT

Moshe Harush
  • 671
  • 6
  • 20
Shajibur Rahman
  • 436
  • 3
  • 12
  • 1
    But wouldn't this just force PSR-2 for *all* PHP files, even those which are not my own? – ooxi May 12 '17 at 07:02
2

You can set up in project settings the code style you want to apply. Step-By-Step to setup the code styling

After this you just need to press right-click the directory in project view and from the context menu select Reformat Code (Ctrl+Alt+L).

Check the setting on the dialog was open and hit "run" button.

I hope I was helpful to you.

More information on: https://www.jetbrains.com/help/phpstorm/2017.1/reformatting-source-code.html#d425724e29

Moshe Harush
  • 671
  • 6
  • 20
  • But doesn't this mean I have to change the settings of every project manually after checking out the source code? – ooxi May 12 '17 at 07:03
  • @ooxi No, If you want to set up this on global settings just get down more one option on step 2 - Default settings (insted "Settings...") - and continue the next steps :) – Moshe Harush May 12 '17 at 07:28