DEV Community

Freek Van der Herten
Freek Van der Herten

Posted on • Originally published at freek.dev

Configuring PhpStorms code generation

I've been using PhpStorm for quite some time now, but never took the effort to fix a few minor annoyances I had with it.

Getting rid of the default comment for new PHP files

First up, when creating a new PHP file or class you PhpStorm will add this comment block like this by default:

/**
 * Created by PhpStorm.
 * User: freek
 * Date: 2018-11-25
 * Time: 23:44
 */

Sure, you can manually remove that block (this is what I have been doing too), but even better is to configure PhpStorm so that block won't get generated at all.

If you want to get rid of that block forever, head over to File and Code Templates section in the preferences. There, go to PHP File and remove the #parse("PHP File Header.php") line. To the same thing for PHP Class.

The result should look like this:
File and code template settings

Alternatively you could go the PHP File Header and remove all content.
PHP File Header settings

Try to create a new PHP file or class now. The comment block won't be there anymore.

UPDATE: newer versions of PHPStorm won't have the comment there by default anymore

Compact docblocks for instance variables.

When creating the constructor of a new class in PhpStorm, you can automatically initialize the parameters. Just press alt+enter while the cursor is on a variable. This opens a dialog that allows you to automatically generate the instance variable.

Initialize

The result is something like this:

class Foo {

    /**
     * @var string 
     */
    private $bar;

    public function __construct(string $bar)
    {
        $this->bar = $bar;
    }
}

Nice, but what I don't like about the autogenerated code is the multiline docblock . Luckily this can be fixed by going to Settings > File and Code Templates > PHP Field Doc Comment and customize the template to your liking. This is what my template looks like:

PHP Field Doc comment

This will make the autogenerated look like this.

class Foo {

    /** @var string */
    private $bar;

    public function __construct(string $bar)
    {
        $this->bar = $bar;
    }
}

Fixing the placement of the caret

You can easily create a new class with the New PHP Class action. PhpStorm will ask the name of the class. Unfortunately when the file has been created the caret won't be inside the the created class. Let's fix that too!

In File and Code Templates > PHP Class type #[[$END$]]# where you want to caret to be after the PHP class has been created. Also tick the Enable Live Templates box. Here's what it looks like for me:

Initialize

Try creating a new class now. The caret should be positioned inside your class.

Using fully qualified class names in doc blocks.

When autogenerating docblock for instance variables that are class, PhpStorm will use the short class name. If you, like me, prefer the fully qualified class name there, just tick the Use fully-qualified class names in the PHPDoc settings.

PHP Doc settings.

Changing the default visibility

For my code I prefer using protected instance variables. By default PhpStorm will use private. You can change the default visibility in the Code Generation screen.

Visibility

Saving your settings in a Git Repo

Probably you'd like to backup these settings. Or, if you use multiple computers, you'd like to sync all the settings. Luckily, PhpStorm does have support for that. It can sync all its settings to a Git repo.

First, start creating a repo on GitHub (or alternative service). Then, select File > Settings Repository and past the upstream url in that dialog box. Click Overwrite remote the first time to write all your settings to the repo.

Visibility.

If you automatically want to sync the settings, head over to Settings > Tool > Settings Repository and make sure the Auto Sync box is checked.

Visibility.

In closing

These are only a few of the options that PhpStorm offers for autogenerating code. I highly recommend going through all the Editor > PHP settings and set everything to your liking.

Top comments (0)