Skip to content
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

Laravel built in casts now have incorrect type documented #1423

Closed
Jefemy opened this issue Feb 19, 2023 · 5 comments
Closed

Laravel built in casts now have incorrect type documented #1423

Jefemy opened this issue Feb 19, 2023 · 5 comments
Labels

Comments

@Jefemy
Copy link
Contributor

Jefemy commented Feb 19, 2023

Versions:

  • ide-helper Version: 2.13.0
  • Laravel Version: 9.52.0
  • PHP Version: 8.1.16

Description:

Using Laravel built in casts such as Illuminate\Database\Eloquent\Casts\AsCollection will now return it as its own class, which doesn't actually implement a Collection meaning it will error out even when using it properly. Previously it returned as mixed which at least didn't error. The change causing this is #1388 but is more down to the actual Laravel class not having type hints for the get method. This could be fixed on the Laravel side, or it could be extracted from the docblock return from the castUsing function. I did test with making a modification here but I got stuck as I believe the CastsAttributes wrapper in the docblock would need to be stripped for proper functionality and I wasn't quite sure on the best way to do that

Steps To Reproduce:

  • Create a Model like this
class CollectionModel extends Model
{
    public $casts = [
        'collection' => \Illuminate\Database\Eloquent\Casts\AsCollection::class,
    ];
}
  • Generate model docblocks php artisan ide-helper:model -M
  • Outputted type is @property \Illuminate\Database\Eloquent\Casts\AsCollection $collection
@Jefemy Jefemy added the bug label Feb 19, 2023
@Jefemy
Copy link
Contributor Author

Jefemy commented Feb 19, 2023

I am now seeing that #1393 exists but it is also missing types for the 5 other casts that Laravel has. If that turns out to be the proper way to solve this I can make a pull containing all of them.

@jamesrusso
Copy link

jamesrusso commented Sep 24, 2023

I'm running into this same issue, wish there was a workaround to just update the config until this is either fixed upstream in laravel or here in this package.

@moussaclarke
Copy link

@jamesrusso My workaround is to keep generated model types in separate file and manually over-ride broken types only in the relevant model files.

@AndrewMast
Copy link

I was able to fix this by adding the following to the type_overrides setting in the ide-helper config file:

    /*
    |--------------------------------------------------------------------------
    | Property Casts
    |--------------------------------------------------------------------------
    |
    | Cast the given "real type" to the given "type".
    |
    */
    'type_overrides' => [
        'integer' => 'int',
        'boolean' => 'bool',
        '\\'.AsCollection::class => '\Illuminate\Support\Collection',
        '\\'.AsArrayObject::class => '\ArrayObject',
    ],

@barryvdh
Copy link
Owner

barryvdh commented Feb 8, 2024

Fixed with #1393?

@barryvdh barryvdh closed this as completed Feb 8, 2024
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

5 participants