-
-
Notifications
You must be signed in to change notification settings - Fork 328
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 a ReflectionPropertyAccessor #716
Conversation
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'm ok to add this accessor as it can be useful, I however wouldn't wire it by default. I actually removed that behaviour in 3.x (setting properties by accessing them directly) because it was causing too many issues
It would also be nice to add a doc entry for it as well :)
|
||
class DummyWithPrivateProperty | ||
{ | ||
private $val; |
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.
you should add a test with a private static property
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.
You mean to ensure the accessor never consider static properties? Done (and accessor updated).
throw $e; | ||
} | ||
|
||
$getPropertyClosure = \Closure::bind(function ($object) use ($propertyPath) { |
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.
(CS)
$getPropertyClosure = \Closure::bind(
function ($object) use ($propertyPath) {
return $object->{$propertyPath};
},
$objectOrArray,
$objectOrArray
);
$class = get_class($objectOrArray); | ||
$reflClass = new \ReflectionClass($class); | ||
|
||
return $reflClass->hasProperty($propertyPath); |
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.
return (new \ReflectionClass($class))->hasProperty($propertyPath);
(I would keep the $class
though)
@@ -13,6 +13,7 @@ | |||
|
|||
namespace Nelmio\Alice\Loader; | |||
|
|||
use Nelmio\Alice\Entity\DummyWithPrivateProperty; |
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.
hm, the order doesn't seem right, I really need to add a StyleCI...
85df321
to
78bc563
Compare
Changes made. :) Where would the documentation fit the best in the current documentation? (mention the decoration of the loader property accessor + symfony configuration sample) |
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException | ||
* @expectedExceptionMessage Cannot read property "staticVal". | ||
*/ | ||
public function testThrowsAnOriginalExceptionIfPropertyIsStatic() |
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.
Shouldn't it be able to read it?
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 don't think we should support it (use cases look a bit edgy to me), but you set the rules. 😄
Actually, I'm not even sure the property access component is meant to be used to access any static property.
78bc563
to
a920eb6
Compare
Documentation added. |
I use this accessor mainly for hydratation because my domain objects do not have proper setters for every properties, or in order to set predictable values for those computed directly in the constructor.
However, I wonder if it should be wired by default or not.