Auto generating argument typehints for PhpStorm in Laravel projects cover image

Auto generating argument typehints for PhpStorm in Laravel projects

Matthew Erwin • May 8, 2021

laravel phpstorm

In a Laravel project you can generate PHPDocs and PhpStorm Meta using the barryvdh/laravel-ide-helper package.
We can add additional typehints for custom functions by writing a command to generate a new PhpStorm Meta file.

Writing a command to generate the meta file

This command will generate a meta file compatible with PhpStorm https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#expected-arguments

php artisan make:command GenerateSettingsMeta --command=settings:meta

app/Console/GenerateSettingsMeta.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

class GenerateSettingsMeta extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'settings:meta';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate PhpStorm meta file';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $stub = File::get(__DIR__ . '/stubs/settings_meta.stub');

        $names = DB::table('settings')
            ->pluck('name')
            ->sort();

        $stub = str_replace('{{ names }}', $names->implode(",\n\t\t"), $stub);

        if (!File::isDirectory('.phpstorm.meta.php')) {
            File::makeDirectory('.phpstorm.meta.php');
        }

        File::put(base_path('.phpstorm.meta.php/settings.php'), $stub);

        $this->info('.phpstorm.meta.php/settings.php Generated');

        return;
    }
}

app/Console/stubs/settings_meta.stub

<?php

namespace PHPSTORM_META {

    registerArgumentsSet(
        'setting_values',
        {{ names }}
    );


    expectedArguments(
        \setting(),
        0,
       argumentsSet('setting_values')
    );
}

Updating the meta file

php artisan settings:meta

This will generate a PhpStorm meta file in .phpstorm.meta.php/settings.php like the one below.

<?php

namespace PHPSTORM_META {

    registerArgumentsSet(
        'setting_values',
        'my_setting_one',
        'my_setting_two',
        'my_setting_three'
    );

    expectedArguments(
        \setting(),
        0,
       argumentsSet('setting_values')
    );
}

Now in PhpStorm we should see the parameter typehints when calling the function. PhpStorm Typehint

Using with barryvdh/laravel-ide-helper

By default barryvdh/laravel-ide-helper writes it's meta file to .phpstorm.meta.php which means we can't add custom files.
PhpStorm allows phpstorm.meta.php to be a directory so we can configure this by editing config/ide-helper.php to change the meta_filename.

...
/*
    |--------------------------------------------------------------------------
    | Where to write the PhpStorm specific meta file
    |--------------------------------------------------------------------------
    |
    | PhpStorm also supports the directory `.phpstorm.meta.php/` with arbitrary
    | files in it, should you need additional files for your project; e.g.
    | `.phpstorm.meta.php/laravel_ide_Helper.php'.
    |
    */
    'meta_filename' => '.phpstorm.meta.php/ide_helper.php',
...