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

check readonly property via serialization #11617

Open
wants to merge 3 commits into
base: 2.20.x
Choose a base branch
from

Conversation

garak
Copy link

@garak garak commented Sep 30, 2024

This is a remake of #9998
It should fix #9505

@garak garak force-pushed the fix-readonly-property branch from f76e781 to ed20cf5 Compare September 30, 2024 13:18
@greg0ire
Copy link
Member

greg0ire commented Sep 30, 2024

Thanks for working on this! Please improve your commit message according to the contributing guide.

Make possible to use value objects (most notably implementations of UUIDs)
as readonly properties
@garak garak force-pushed the fix-readonly-property branch from ed20cf5 to 13ad404 Compare September 30, 2024 14:22
use Doctrine\Tests\Models\ValueObjects\Uuid;

#[Entity]
#[Table(name: 'library')]
Copy link
Member

Choose a reason for hiding this comment

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

Does the table name matter? If not, I'd remove that annotation entirely, let's eliminate unnecessary distractions.

}

public function testSecondWriteWithDifferentValue(): void
public function sameValueProvider(): Generator
Copy link
Member

Choose a reason for hiding this comment

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

Please add a docblock documenting the return type (it should match the signature of testSecondWriteWithSameValue()

@@ -42,7 +43,7 @@ public function setValue(mixed $objectOrValue, mixed $value = null): void

assert(is_object($objectOrValue));

if (parent::getValue($objectOrValue) !== $value) {
if (serialize(parent::getValue($objectOrValue)) !== serialize($value)) {
Copy link
Member

Choose a reason for hiding this comment

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

Another way to do it would be using a loose comparison, although I'm not sure if there are any performance implications. I'd like to know if you've considered it though.

orm/src/UnitOfWork.php

Lines 2319 to 2320 in 982d606

// phpcs:ignore SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator
if ($managedCopyVersion == $entityVersion) {

Copy link
Author

Choose a reason for hiding this comment

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

I didn't try it indeed. I'm not a big fan of loose comparison, which often leads to unexpected consequences in PHP.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I'm undecided myself. Let's see what other maintainers have to say.

Copy link
Member

Choose a reason for hiding this comment

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

this looks extremely expensive to call serialize

Copy link
Author

Choose a reason for hiding this comment

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

this looks extremely expensive to call serialize

"extremely", really?

@@ -43,6 +43,9 @@ public function testSecondWriteWithSameValue(object $entity, string $property, m
self::assertSame($value, $reflection->getValue($entity));
}

/**
Copy link
Member

Choose a reason for hiding this comment

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

This should use array{type of arg1, type of arg2, type of arg3...}

@derrabus
Copy link
Member

My take on this issue is still what I've said in #9505 (comment). Implementing guesswork on when two objects should be considered equal is a very slippery slope. I'm against doing that.

Serialization is especially tricky because it might have side effects and we cannot make the general assumption that every object is serializable.

@garak
Copy link
Author

garak commented Oct 17, 2024

My take on this issue is still what I've said in #9505 (comment). Implementing guesswork on when two objects should be considered equal is a very slippery slope. I'm against doing that.

But we already do that, don't we?
The current code, the one proposed to be changed here, is if (parent::getValue($objectOrValue) !== $value)

@@ -44,9 +44,9 @@ public function testSecondWriteWithSameValue(object $entity, string $property, m
}

/**
* @return Generator<string, array<string, string|object>>
* @return Generator<string, array{entity: object, property: string, value: string|object, sameValue: string|object}>
Copy link
Member

Choose a reason for hiding this comment

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

Oh wow didn't know we could use named arguments here 💡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants