-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add support for protected Attribute accessors #1339
Add support for protected Attribute accessors #1339
Conversation
f2e0c6d
to
4273911
Compare
@@ -1096,6 +1096,9 @@ protected function hasCamelCaseModelProperties() | |||
|
|||
protected function getAttributeReturnType(Model $model, \ReflectionMethod $reflectionMethod): Collection | |||
{ | |||
// Private/protected ReflectionMethods require setAccessible prior to PHP 8.1 | |||
$reflectionMethod->setAccessible(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose not to wrap this in an if-statement checking for PHP_VERSION_ID
since according to the docs from PHP 8.1 onward this method call is a noop.
Can this PR be looked at? Using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, but I'm a nobody.
Will soon check it out; this competes with #1325 right? |
Correct. |
I would like to ask if someone could look into this again. The summary states the case/necessity for this PR. There are some fairly major changes to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks perfectly fine to me, hope this will get reviewed and merged by a maintainer soon.
Very good job @pindab0ter!
I would like to update this PR, but it relies on #1338. I would like to note that this PR is required to support the Attribute accessors that are new since Laravel 9. Without this PR, they are not recognised when following Laravel's documentation in implementing those. |
I am currently using That would be unmaintainable... so please, @mfn @barryvdh or any collaborator, member or admin, can this be looked at and merged ASAP? @pindab0ter I do see there are conflicts (but I can't see them), could you fix them so this can be taken care of immediately? Would appreciate having this merged and release ASAP because it is a must ( |
0bd2e90
to
6bc3404
Compare
6bc3404
to
b1f85ab
Compare
@mfn With the 2.13.0 release, do you think it's possible to get this PR done? I would like to reiterate that this package as of now still does not support a basic Laravel feature introduced in Laravel 9; Attribute accessors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋🏼
Approach LGTM, I'm also preferring this over #1325 , covers more cases.
My feedback:
- please don't change the visibility of existing attributes -> please create a new column/attribute using
protected
(basically like you added theprivate
to show - to improve readability of the PR, I suggest to add
$method = $methodReflection->getName();
at the top of the loop -> this will reduce a LOT of noise in the PR (except for the cases where indeed have to pass on the reflect object) - resolve the merge conflicts
(ps: any reason for the style changes?)
thanks!
b1f85ab
to
3f923d4
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR is so much more readable now, thank you!
Approving, the error with psalm is unrelated and needs its own fix.
Btw, reported this over there -> vimeo/psalm#9345 |
Awesome, thanks! What are your thoughts on this?
Can you do #1338 next? It adds better support for (missing) type hinting of Attributes. |
* Add support for protected Attribute accessors * Fix formatting * Prevent accessors methods marked as private from being added * Exclude specific accessors based on trait instead of class * Add changelog entry * Add clarifying comment * Fix accessor attributes not working on PHP < 8.1 * Reintroduce method variable to reduce PR clutter * Change variable name to reduce PR clutter * Update CHANGELOG.md --------- Co-authored-by: Barry vd. Heuvel <[email protected]> Co-authored-by: Barry vd. Heuvel <[email protected]>
Summary
The documentation for defining accessors states that “To define an accessor, create a protected method on your model to represent the accessible attribute.” (emphasis mine).
This is currently not supported by IDE Helper as noted in #1293.
The problem is that
get_class_methods
doesn't return protected or private functions. The solution is to use Reflection instead and filter out any unwanted methods:private
since marking an accessor asprivate
breaks the accessor and the two methods defined in\Illuminate\Database\Eloquent\Concerns\HasAttributes
trait.Any accessors marked as private will now not show up in the IDE Helper files. I chose to not mark this as a breaking change since those accessors would not have worked anyway.
This PR conflicts with #1338.
Type of change
Checklist
composer fix-style