Keep imported phpstan/psalm types unqualified (#1797)

A type brought in with @phpstan-import-type is a local alias, not a class
in the model's namespace, but it was resolved as one:

    @property-read \App\Models\ArrayShape $some_array

Collect the aliases declared with @phpstan-type/@psalm-type or imported
with @phpstan-import-type/@psalm-import-type on the model and its parents,
and return them as written.

Fixes #1773
This commit is contained in:
Hugo Mayonobe
2026-08-20 16:16:03 +02:00
committed by GitHub
parent cc26683e8e
commit 20497cb988
5 changed files with 152 additions and 0 deletions
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\PhpstanImportType\Models;
/**
* @phpstan-type ArrayShape array{name: string, age: int}
*/
class Shapes
{
}
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\PhpstanImportType\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @phpstan-import-type ArrayShape from Shapes
*/
class Simple extends Model
{
/**
* @return ArrayShape
*/
public function getSomeArrayAttribute(): array
{
return ['name' => 'Taylor', 'age' => 40];
}
/**
* @phpstan-return LocalShape
*/
public function getLocalAttribute(): array
{
return [];
}
}
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\PhpstanImportType;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
class Test extends AbstractModelsCommand
{
/**
* Types imported with `@phpstan-import-type` are local aliases, not classes
* in the model's namespace, so they must not be prefixed with it.
*
* @link https://github.com/barryvdh/laravel-ide-helper/issues/1773
*/
public function testImportedTypeIsNotQualifiedWithTheModelNamespace(): void
{
$command = $this->app->make(ModelsCommand::class);
$tester = $this->runCommand($command, ['--write' => true]);
$this->assertSame(0, $tester->getStatusCode());
$this->assertMatchesMockedSnapshot();
}
}
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\PhpstanImportType\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @phpstan-import-type ArrayShape from Shapes
* @property int $id
* @property-read array $local
* @property-read ArrayShape $some_array
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple query()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple whereId($value)
* @mixin \Eloquent
*/
class Simple extends Model
{
/**
* @return ArrayShape
*/
public function getSomeArrayAttribute(): array
{
return ['name' => 'Taylor', 'age' => 40];
}
/**
* @phpstan-return LocalShape
*/
public function getLocalAttribute(): array
{
return [];
}
}