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

Adding arguments to custom model casts via $casts[] results in incorrect property type #1525

Closed
attac-t opened this issue Jan 19, 2023 · 5 comments · Fixed by #1529
Closed
Labels
bug Something isn't working

Comments

@attac-t
Copy link

attac-t commented Jan 19, 2023

  • Larastan Version: 2.4.0
  • --level used: 5

Description

When using Castable classes and providing arguments in the eloquent model's $casts definition, the property is not being casted to a phpstan type.

Model property casting has been handled in #1333.

How to replicate

Implement a castable class as defined here and provide an argument via the model's $casts[].

use App\Models\Address;
 
protected $casts = [
    'address' => Address::class.':argument',
];
@canvural
Copy link
Collaborator

What error are you getting?

@attac-t
Copy link
Author

attac-t commented Jan 19, 2023

Let's say that the Address value object has a method getCountryName() and that address is a string database column in the users table. For $user->address->getCountryName(), larastan gives a cannot call method getCountryName() on string. error. This only happens if the ':argument' is present in the $casts.

@canvural
Copy link
Collaborator

I see.

@erikgaal Can you take a look at this please?

@canvural canvural added the bug Something isn't working label Jan 19, 2023
@erikgaal
Copy link
Contributor

This should be covered by the 'secret' => Hash::class . ':sha256' test case, but I will investigate.

@strotgen
Copy link

  • Larastan Version: 2.4.0
  • --level used: 5

Getting a similar error when I updated from 2.2.9 to 2.4.0 and using cknow/laravel-money casters. Example:

use Cknow\Money\Casts\MoneyIntegerCast;

protected $casts = [
        'amount' => MoneyIntegerCast::class.':currency_code',
];

And when calling it like $model->amount->formatByDecimal(), PHPStan says the following:

Cannot call method formatByDecimal() on int.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants