51

Is there any way to beautify my php code in PhpStorm? (a keyboard shortcut would be preferable)

5 Answers 5

85

Choose Code | Reformat Code, or press Ctrl + Alt + L.

EDIT: The keyboard shortcode has changed since PHPStorm 10 for windows to Ctrl + Alt + F

4
  • 10
    On OSX, it's Alt+Cmd+L (version 2016.2.1)
    – damd
    Oct 3, 2016 at 10:39
  • 4
    additionally You can hit Alt + Cmd + Shift + L for ability to choose scope (only selected content, whole file etc.) of reformatting (prettifying) the file - on OSX, PhpStorm version 2018.1
    – Paul Mark
    Apr 25, 2018 at 14:18
  • 2
    Tune codestyle via Settings - Editor - Code Style. Then standardize codestyle with teammates via Export or (better) Copy to project (near Scheme drop-down list) if project directory is attached to any Version Control System. Bad (but identical for entire team) codestyle is better then no codestyle :) Oct 11, 2018 at 10:09
  • This shortcut will reformat selected text only or entire file (if no text selected). For me, it is better to use this periodically without any selection to keep entire file standardized. Oct 11, 2018 at 10:15
54

Select code to reformat and press Ctrl + Alt + Shift + L

This is tested and working (Ubuntu), cheers!

For Mac:

Select code to reformat and press Command + Alt + Shift + L

For full page code reformat Alt + Command + L

Tested in phpStorm 2018.1.1

1
  • 2
    On some Linux environments this shortcut may be assigned to system commands (i.e., Lock screen). You can simply change it in Settings - Keymap - search for "Reformat code" Oct 11, 2018 at 10:20
4

Under the 'Code' menu there is an option to Reformat Code. In the project settings there is an option to set code style for each file type.

4

Sometimes Code | Reformat isn't enough.

As an example, after reformating a very ugly code I got this result

 public function createAction(Request $request)

{

    $news = new News();

    $em = $this->getDoctrine()->getManager();


    $form = $this->get('form.factory')
        ->createNamed(

            'backend_news',

            'backend_news',

            $news,

            array(

                'action' => '',

                'method' => 'POST',

                'attr' => array(

                    'class' => 'form-horizontal',

                    'role' => 'form'

                )

            )

        )->add('submit', 'submit', array('label' => 'Valider'));;


    $form->handleRequest($request);


   [...]

    return $this->render('BackendNewsBundle:List:form.html.twig', array(

        'form' => $form->createView()

    ));

}

In this case you need to go to

Preferences | Editor | Code Style | PHP

and from here fine tune. Then reformat again. Example for this file it was a lot of blank line, so I put to 0 the 'In Code' section of the Blank Lines tab. Once I reformated the code I put it back to 2. Anyway a lot of tuning is possible in this section.

2
  • 1
    why downvote ? My answer add extra informations showing a real exemple Feb 20, 2018 at 13:53
  • maybe it will be better to edit/comment top answer to show your advise to more people? Oct 11, 2018 at 10:24
2

This has since been changed in PHPStorm 10 to Code | Format Code Ctrl|Cmd + Alt + F

1
  • 2
    On OSX, it's Alt+Cmd+L (version 2016.2.1)
    – damd
    Oct 3, 2016 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.