Skip to content

The generic return type on CastAttributes::castUsing is ignored #1700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Watercycle opened this issue Apr 15, 2025 · 0 comments
Open

The generic return type on CastAttributes::castUsing is ignored #1700

Watercycle opened this issue Apr 15, 2025 · 0 comments
Labels

Comments

@Watercycle
Copy link

Watercycle commented Apr 15, 2025

Versions:

  • ide-helper Version: 3.5.5
  • Laravel Version: 11.44.2
  • PHP Version: 8.3.19

Description:

I'm using spatie/laravel-data and have code along the lines of...

// SettingsData.php
class SettingsData extends Data implements Castable
{
    /**
     * @param  array<string, mixed>  $arguments
     * @return CastsAttributes<static, static>
     */
    public static function castUsing(array $arguments): CastsAttributes
    {
        return new PartialDataCast(static::class, $arguments);
    }
}

// PartialDataCast.php
use Spatie\LaravelData\Support\EloquentCasts\DataEloquentCast;

class PartialDataCast extends DataEloquentCast
{
    // ...
}

php artisan ide-helper:models results in doc-blocks along the lines of: \Spatie\LaravelData\Contracts\BaseData $settings

because ModelsCommand sees

class DataEloquentCast implements CastsAttributes
{
    // ...
    public function get($model, string $key, $value, array $attributes): ?BaseData

instead of the class I'm actually casting to (i.e. AssignmentSettingsData):

// AssignmentSettings.php
class AssignmentSettingsData extends SettingsData
{
    public bool $some_property = true;
}

// Assignment.php
class Assignment extends Model
{
    protected $casts = ['settings' => AssignmentSettingsData::class];
}

All this to say, it should be possible to specialize the return type instead of just returning BaseData so that AssignmentSettingsData was used from the start. DataEloquentCast is both vendor code and presents a use-case where the get type is too broad.

#1437 is a related, slightly different, issue I saw in the backlog. Ideally we can have out-of-the-box laravel-data support to encourage strict typing in PHP projects.

Steps To Reproduce:

See #1701 - also happy to do something here upon request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant