Configuring PhpStorms code generation
Tags: php, phpstorm
Published: 2020-01-02 22:38:49

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:

```php
/**
 * 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](https://freek.dev/uploads/media/phpstorm/php-file.png)


Alternatively you could go the `PHP File Header` and remove all content.
![PHP File Header settings](https://freek.dev/uploads/media/phpstorm/header.png)

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 

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">Nice to have influenced <a href="https://twitter.com/phpstorm?ref_src=twsrc%5Etfw">@phpstorm</a> a little ☺️<a href="https://t.co/2wyLjR6HLs">https://t.co/2wyLjR6HLs</a><br><br>Blogpost: <a href="https://t.co/0noKDyVGPS">https://t.co/0noKDyVGPS</a> <a href="https://t.co/tAKCKUOGBo">pic.twitter.com/tAKCKUOGBo</a></p>&mdash; Freek Van der Herten (@freekmurze) <a href="https://twitter.com/freekmurze/status/1088165618707296257?ref_src=twsrc%5Etfw">January 23, 2019</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>


## 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](https://freek.dev/uploads/media/phpstorm/initialize.png)

The result is something like this:

```php
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](https://freek.dev/uploads/media/phpstorm/field-doc.png)

This will make the autogenerated look like this.

```php
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](https://freek.dev/uploads/media/phpstorm/end.png)

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](https://freek.dev/uploads/media/phpstorm/fqcn.png).

## 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](https://freek.dev/uploads/media/phpstorm/visibility.png)

## 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](https://freek.dev/uploads/media/phpstorm/repo.png).

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](https://freek.dev/uploads/media/phpstorm/auto-sync.png).

## 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.