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

avoid multiple reads/writes for inherited properties in hydrator class #85

Open
wants to merge 3 commits into
base: 4.2.x
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ private function findAllInstanceProperties(?ReflectionClass $class = null) : arr
$this->findAllInstanceProperties($class->getParentClass() ?: null), // of course PHP is shit.
array_values(array_filter(
$class->getProperties(),
static function (ReflectionProperty $property) : bool {
return ! $property->isStatic();
static function (ReflectionProperty $property) use ($class) : bool {
return ! ($property->isStatic() || $property->class !== $class->name);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using public properties from ext-reflection: they are very unreliable.

Besides that: could you expand https://github.com/Ocramius/GeneratedHydrator/blob/ad230228c71c32279af94ed1484b907cd403886f/tests/GeneratedHydratorTest/CodeGenerator/Visitor/HydratorMethodsVisitorTest.php to verify this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored property access to use functions instead. Thanks for the heads up 👍

I looked into the testing before but didn't see a nice way to test this. The easiest way would be to inspect the property maps of the HydratorMethodsVisitor. They are currently private and there are no getters for them. Would you consider weakening the isolation for them to enable easier testing or should I look into other possibilities?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into the testing before but didn't see a nice way to test this.

Verifying the generated code is the correct way here. I can't remember how I initially tested this, but I'd suggest expanding https://github.com/Ocramius/GeneratedHydrator/blob/ad230228c71c32279af94ed1484b907cd403886f/tests/GeneratedHydratorTest/CodeGenerator/Visitor/HydratorMethodsVisitorTest.php to include an "expected generated method body" (can compare AST of produced nodes against AST of a parsed expected block)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius added a test to verify the change. It's quite ugly and will break if the parser changes structure of the AST but I guess that's a trade off we are willing to make.

}
))
));
Expand Down