-
-
Notifications
You must be signed in to change notification settings - Fork 400
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
Property check #673
Property check #673
Conversation
f3d81da
to
1802235
Compare
@Daanra Can you do a review? |
@canvural Yes. I'll try to make some time either tomorrow or the day after. |
I'm getting quite a few false positives when testing this on some of my projects. The main problems comes from columns that are present on pivot tables. E.g.: $user->groups()->where('group_joined_at', '>=', now()->subWeek())->get(); I think, for now, the easiest would be to just return no errors in the rule when the method is called on an instance of |
Isn't that code trying to find the |
Under the hood a join is performed and the where clause will match any joined table. I'm fine with throwing an error when using |
That'd be a good idea! I'll try to implement that. |
@Daanra How should we display the error for that case? There may be two reasons why column does not exists. First it can be that column really doesn't exist in related model. Second case is when it doesn't exist in pivot table. But we really don't know that. So should we display 2 errors for that line? One for missing property. Second, a warning about that column name is ambiguous and tell user to prefix it with table name or use wherePivot. Or just warn about the ambiguity of the colum name? |
I'm personally not a fan of displaying two errors. I think in this scenario a single error message with a small hint appended is best. Something along the lines of:
This hint should only be appended when the query is called on a subclass of |
35dbb5d
to
0a61126
Compare
0a61126
to
36dc3dc
Compare
LGTM! I suppose we still need some docs before releasing this though. |
Yeah, we need docs. I'm working on it 👍 Plus we need to make sure every Laravel core function has the new type And there is still stuff to do I wrote in the |
I just read https://github.com/nunomaduro/larastan/releases/tag/v0.6.5 and upgraded:
This example does not report anything my codebase using 0.6.5: Profile::where('column_does_not_exist', 'true')->get(); What are the requirements to make this work? How are the properties inferred? Thanks! |
@mfn Did you add:
to your config? |
No 😢 It was not mentioned on https://github.com/nunomaduro/larastan/releases/tag/v0.6.5 (only that it's "beta") and when I read https://github.com/nunomaduro/larastan/blob/master/docs/rules.md#modelpropertyrule I did not scroll to the last part because, again, it just says "beta" at the beginning but not mentions that it's actually disabled (and I saw the code example and was eager to see this in action). TL;DR: works great, thank you 🙇 |
It seems to ignore columns entirely which use the explicit table prefix, i.e. they contain a dot: Is this expected / a limitation or should I open an issue? |
Hi @mfn , I'll try to update the docs so it'll mention earlier that it is disabled by default 👍
Yes, this is expected. If the value contains a dot we simply ignore the check. Because there is no good way to determine which model it should be checked on. This can be improved maybe. By comparing the model's table and the table name given in the argument. But it'll not work in every case. |
Damn 😄 In our projects it's a requirement to us the "full form", because it happens to easily that joins shadow same-named columns and the only way to avoid this was the brute-force approach to make a rule to always use the table name too. |
This PR adds support for checking arguments passed to methods that expect model properties. For example
How it works?
This PR uses the types introduced in #657 by @Daanra But unlike that PR there is no check done in type level. Instead there are 2 new rules (currently, more to come) that check severy method call and inspects if any of the arguments have the new
model-property
or genericmodel-property<Model>
type. If so the parameter will be checked against the model property.Using in custom methods
It's not limited to Laravel core methods. Any custom method can typehint that it expects properties of a model with
model-property<Model>
typehint.Stubs
New types added to Laravel core methods by the help of stubs. Currently not all methods have the new type in docblocks. But it's fairly easy to add them to stubs so I'm thinking preparing a guide of how-to, and ask people to contribute. Would be a cool and easy way to contribute to OSS and Hacktoberfest.
Next steps
model-property<Model>
we should check the assigned value.