Merge pull request #170 from caugner/model-@property-support

fix: Support @property on Model with imported type
This commit is contained in:
feek 2021-07-09 12:28:25 -04:00 committed by GitHub
commit 0e2f510f2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 1 deletions

View File

@ -50,7 +50,8 @@ final class ModelStubProvider implements GeneratesStubs
$models_generator_command->run(
new ArrayInput([
'--nowrite' => true
'--nowrite' => true,
'--reset' => true,
]),
new NullOutput()
);

View File

@ -0,0 +1,22 @@
<?php
namespace Tests\Psalm\LaravelPlugin\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Ramsey\Uuid\UuidInterface;
/**
* @property-read UuidInterface $uuid
*/
abstract class AbstractUuidModel extends Model
{
protected static function boot()
{
parent::boot();
static::creating(function (Model $model) {
$model->setAttribute('uuid', Str::uuid());
});
}
}

6
tests/Models/Secret.php Normal file
View File

@ -0,0 +1,6 @@
<?php declare(strict_types=1);
namespace Tests\Psalm\LaravelPlugin\Models;
final class Secret extends AbstractUuidModel {
};

View File

@ -2,6 +2,7 @@
namespace Tests\Psalm\LaravelPlugin\Models;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
@ -9,6 +10,7 @@ use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* @property string $id
* @property CarbonInterface|null $email_verified_at
*/
final class User extends Model {
protected $table = 'users';

View File

@ -0,0 +1,59 @@
Feature: Eloquent Model property types
Illuminate\Database\Eloquent\Model have property type support
Background:
Given I have the following config
"""
<?xml version="1.0"?>
<psalm totallyTyped="true" usePhpDocPropertiesWithoutMagicCall="true">
<projectFiles>
<directory name="."/>
<ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\LaravelPlugin\Plugin"/>
</plugins>
</psalm>
"""
And I have the following code preamble
"""
<?php declare(strict_types=1);
namespace Tests\Psalm\LaravelPlugin\Sandbox;
use Tests\Psalm\LaravelPlugin\Models\Secret;
use Tests\Psalm\LaravelPlugin\Models\User;
"""
Scenario: Property annotation with scalar type
Given I have the following code
"""
function test(User $user): string
{
return $user->id;
}
"""
When I run Psalm
Then I see no errors
Scenario: Property annotation with imported type
Given I have the following code
"""
function test(User $user): ?\Carbon\CarbonInterface
{
return $user->email_verified_at;
}
"""
When I run Psalm
Then I see no errors
Scenario: Inherited property annotation
Given I have the following code
"""
function test(Secret $secret): \Ramsey\Uuid\UuidInterface
{
return $secret->uuid;
}
"""
When I run Psalm
Then I see no errors