Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Conversation

@beberlei
Copy link
Member

Fixes #34

Related to doctrine/orm#7999

@beberlei beberlei force-pushed the GH-34-SetValueUnitialized branch from 8b195cf to cd4811c Compare March 16, 2020 08:39
Comment on lines +34 to +46
if ($value === null) {
$propertyName = $this->getName();

$unsetter = function () use ($propertyName) {
unset($this->$propertyName);
};
$unsetter = $unsetter->bindTo($object, $this->getDeclaringClass()->getName());
$unsetter();

return;
}

parent::setValue($object, $value);
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth to keep a property with the original closure? Doing that is safe since Closure#bindTo() is a immutable operation. It would avoid creating maaaaany unnecessary closures in runtime but we should benchmark it to see the impact (memory/processing time.)

That would simplify this quite a lot, considering the unsetter is created at construction time:

Suggested change
if ($value === null) {
$propertyName = $this->getName();
$unsetter = function () use ($propertyName) {
unset($this->$propertyName);
};
$unsetter = $unsetter->bindTo($object, $this->getDeclaringClass()->getName());
$unsetter();
return;
}
parent::setValue($object, $value);
if ($value === null) {
$this->unsetter->bindTo($object, $this->getDeclaringClass()->getName())();
return;
}
parent::setValue($object, $value);

Copy link
Member

Choose a reason for hiding this comment

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

We could also save the result of $this->getDeclaringClass()->getName() to reduce call count to these methods, btw.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wouldn't optimize this yet, because i don't see this happening often. It is needed for deleting entities for sure, when ORM sets the id to null. But why would users have a null value for a typed, non-null property themselves? That be a weird usage I don't recommend anyways

@beberlei beberlei merged commit b699ecc into master Mar 21, 2020
@beberlei beberlei added this to the 1.2.0 milestone Mar 21, 2020
@beberlei beberlei added the Bug Something isn't working label Mar 21, 2020
@greg0ire greg0ire deleted the GH-34-SetValueUnitialized branch March 21, 2020 13:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

setValue on typed property must unset to uninitialized

4 participants